//
//  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 private weak var adUnitIDLabel: UILabel!
    @IBOutlet private weak var showButton: UIButton!
    @IBOutlet private weak var adapterVersionLabel: UILabel!
    @IBOutlet private weak var adBlock: AdBlockView!
    @IBOutlet private 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()
    }

    @IBAction func loadAdButtonTouchUpInside(_ sender: Any) {
        resetLabels()
        adBlock.tip = MediationInterstitialAdsTip.LoadingTip.rawValue

        let gadInterstitial = GADInterstitial(adUnitID: adMobModel.adUnitId)
        interstitial = gadInterstitial
        gadInterstitial.delegate = self
        let request = GADRequest()
        request.testDevices = [kGADSimulatorID]
        gadInterstitial.load(request)
    }

    @IBAction func showBannerTapped(_ sender: UIButton) {
        guard let gadInterstitial = interstitial,
              gadInterstitial.isReady else {
            NSLog("\(#function): An interstitial is not ready yet")
            return
        }
        showButton.isEnabled = false
        gadInterstitial.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.adUnitId
        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
    }
}
