//
//  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: GADInterstitialAd?

    var adMobModel: AdMobModel!

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

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

        let request = GADRequest()
        GADInterstitialAd.load(withAdUnitID:adMobModel.adUnitId,
                               request: request,
                               completionHandler: { (ad, error) in
                                if let error = error {
                                    print("Failed to load interstitial ad with error: \(error.localizedDescription)")
                                    return
                                }
                                self.interstitial = ad
                                self.interstitial?.fullScreenContentDelegate = self
                                
                                self.resetLabels()
                                self.interstitialDidReceiveLabel.alpha = 1
                                self.showButton.isEnabled = true
                                self.adBlock.tip = MediationInterstitialAdsTip.LoadedTip.rawValue
                               }
        )
        
    }

    @IBAction func showBannerTapped(_ sender: UIButton) {
        if let ad = interstitial {
            ad.present(fromRootViewController: self)
          } else {
            print("\(#function): An interstitial is not ready yet")
            return
          }
        
        showButton.isEnabled = false
    }

    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: GADFullScreenContentDelegate {
    func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) {
        showButton.isEnabled = false
        NSLog("\(#function)")
    }
    
    func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) {
        interstitialDidFailToReceiveLabel.alpha = 1
        adBlock.tip = MediationInterstitialAdsTip.ErrorTip.rawValue
        NSLog("\(#function) \(error.localizedDescription)")
    }
    
    func adDidPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        interstitialWillPresentScreenLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        interstitialDidDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }
    
    func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        interstitialWillDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }

}
