//
//  AdMobRewardedInterstitialAdapterViewController.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 SmaatoSDKRewardedAds

class AdMobRewardedInterstitialAdapterViewController: UIViewController {

    @IBOutlet private weak var showButton: UIButton!
    @IBOutlet private weak var adBlock: AdBlockView!
    @IBOutlet private weak var adUnitIDLabel: UILabel!
    @IBOutlet private weak var adapterVersionLabel: UILabel!
    @IBOutlet private weak var scrollView: UIScrollView!

    @IBOutlet private weak var rewardedAdDidLoadLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidFailLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidOpenLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidStartLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidCompleteLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidCloseLabel: UILabel!
    @IBOutlet private weak var rewardedAdWillLeaveAppLabel: UILabel!
    @IBOutlet private weak var rewardedAdDidRewardLabel: UILabel!

    var adMobModel: AdMobModel!

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        GADRewardBasedVideoAd.sharedInstance().delegate = self
    }

    @IBAction func loadButtonTapped(_ sender: UIButton) {
        resetLabels()
        showButton.isEnabled = false
        adBlock.tip = MediationRewardedAdsTip.LoadingTip.rawValue
        let request = GADRequest()
        GADRewardBasedVideoAd.sharedInstance().load(request, withAdUnitID: adUnitIDLabel.text!)
    }

    @IBAction func showButtonTapped(_ sender: UIButton) {
        showButton.isEnabled = false
        if GADRewardBasedVideoAd.sharedInstance().isReady {
            GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self)
        }
    }

    func setupUI() {
        self.title = "AdMob Rewarded"
        adBlock.tip = MediationRewardedAdsTip.TryLoadTip.rawValue
        adUnitIDLabel.text = adMobModel?.adUnitId
        adapterVersionLabel.text = "Adapter version \(SMAAdMobSmaatoRewardedVideoAdapter.version())"

        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
            automaticallyAdjustsScrollViewInsets = false
        }
    }

    func resetLabels() {
        rewardedAdDidLoadLabel.alpha = 0.2
        rewardedAdDidFailLabel.alpha = 0.2
        rewardedAdDidOpenLabel.alpha = 0.2
        rewardedAdDidStartLabel.alpha = 0.2
        rewardedAdDidCompleteLabel.alpha = 0.2
        rewardedAdDidCloseLabel.alpha = 0.2
        rewardedAdWillLeaveAppLabel.alpha = 0.2
        rewardedAdDidRewardLabel.alpha = 0.2
    }
}

extension AdMobRewardedInterstitialAdapterViewController: GADRewardBasedVideoAdDelegate {
    func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        resetLabels()
        adBlock.tip = MediationRewardedAdsTip.LoadedTip.rawValue
        showButton.isEnabled = true
        rewardedAdDidLoadLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) {
        rewardedAdDidRewardLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) {
        rewardedAdDidFailLabel.alpha = 1
        adBlock.tip = MediationRewardedAdsTip.ErrorTip.rawValue
        NSLog("\(#function) \(error.localizedDescription)")
    }

    func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        rewardedAdDidStartLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        rewardedAdDidOpenLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAdDidCompletePlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        rewardedAdDidCompleteLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        rewardedAdDidCloseLabel.alpha = 1
        NSLog("\(#function)")
    }

    func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) {
        rewardedAdWillLeaveAppLabel.alpha = 1
        NSLog("\(#function)")
    }
}
