//
//  AdMobInterstitialAdapterViewController.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 03/03/2019.
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

import Foundation
import SmaatoSDKCore
import GoogleMobileAds
import SmaatoSDKInterstitial

class AdMobInterstitialAdapterViewController: UIViewController {
    
    @IBOutlet weak var adUnitIDLabel: UILabel!
    @IBOutlet weak var showButton: UIButton!
    @IBOutlet weak var adapterVersionLabel: UILabel!
    @IBOutlet weak var adBlock: AdBlockView!
    @IBOutlet weak var scrollView: UIScrollView!
    
    @IBOutlet private weak var interstitialDidReceiveLabel: UILabel!
    @IBOutlet private weak var interstitialDidFailToReceiveLabel: UILabel!
    @IBOutlet private weak var interstitialWillPresentScreenLabel: UILabel!
    @IBOutlet private weak var interstitialWillDismissScreenLabel: UILabel!
    @IBOutlet private weak var interstitialDidDismissScreenLabel: UILabel!
    @IBOutlet private weak var interstitialWillLeaveAppLabel: UILabel!
    @IBOutlet private weak var interstitialDidFailToPresentScreenLabel: UILabel!

    private var interstitial: GADInterstitial!
    
    var adMobModel: AdMobModel!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

        interstitial = GADInterstitial(adUnitID: adMobModel.adSpaceId)
        interstitial.delegate = self
    }
    
    @IBAction func loadAdButtonTouchUpInside(_ sender: Any) {
        resetLabels()
        adBlock.tip = MediationInterstitialAdsTip.LoadingTip.rawValue

        let request = GADRequest()
        request.testDevices = [ kGADSimulatorID ]
        interstitial.load(request)
    }
    
    @IBAction func showBannerTapped(_ sender: UIButton) {
        showButton.isEnabled = false
        interstitial.present(fromRootViewController: self)
    }
    
    func resetLabels() {
        interstitialDidReceiveLabel.alpha = 0.2
        interstitialDidFailToReceiveLabel.alpha = 0.2
        interstitialWillPresentScreenLabel.alpha = 0.2
        interstitialWillDismissScreenLabel.alpha = 0.2
        interstitialDidDismissScreenLabel.alpha = 0.2
        interstitialWillLeaveAppLabel.alpha = 0.2
    }
    
    func setupUI() {
        self.title = "AdMob Interstitial"
        adapterVersionLabel.text = "Adapter version \(SMAAdMobSmaatoInterstitialAdapter.version() ?? "")"
        adUnitIDLabel.text = adMobModel.adSpaceId
        adBlock.tip = MediationInterstitialAdsTip.TryLoadTip.rawValue
        
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
            automaticallyAdjustsScrollViewInsets = false
        }
    }
}

extension AdMobInterstitialAdapterViewController : GADInterstitialDelegate
{
    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        resetLabels()
        interstitialDidReceiveLabel.alpha = 1
        showButton.isEnabled = true
        adBlock.tip = MediationInterstitialAdsTip.LoadedTip.rawValue
        NSLog("\(#function)")
    }
    
    func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
        interstitialDidFailToReceiveLabel.alpha = 1
        adBlock.tip = MediationInterstitialAdsTip.ErrorTip.rawValue
        NSLog("\(#function) \(error.localizedDescription)")
    }
    
    func interstitialWillPresentScreen(_ ad: GADInterstitial) {
        interstitialWillPresentScreenLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func interstitialWillDismissScreen(_ ad: GADInterstitial) {
        interstitialWillDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        interstitialDidDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
        interstitialWillLeaveAppLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
        interstitialDidFailToPresentScreenLabel.alpha = 1
    }
}
