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

import UIKit
import SmaatoSDKCore
import GoogleMobileAds

class AdMobBannerAdapterViewController: UIViewController {
    private var adView: GADBannerView!
    public var expectedSize: CGSize?

    @IBOutlet private weak var adapterVersionLabel: UILabel!
    @IBOutlet private weak var adUnitIDLabel: UILabel!
    @IBOutlet private weak var showButton: UIButton!

    @IBOutlet private weak var bannerDidReceiveAdLabel: UILabel!
    @IBOutlet private weak var bannerDidFailToReceiveAdLabel: UILabel!
    @IBOutlet private weak var bannerWillPresentScreenLabel: UILabel!
    @IBOutlet private weak var bannerWillDismissScreenLabel: UILabel!
    @IBOutlet private weak var bannerDidDismissScreenLabel: UILabel!
    @IBOutlet private weak var bannerWillLeaveAppLabel: UILabel!

    @IBOutlet private weak var adBlockHeightConstraint: NSLayoutConstraint!
    @IBOutlet private weak var adBlockWidthConstraint: NSLayoutConstraint!
    @IBOutlet private weak var adBlock: AdBlockView!

    var adMobModel: AdMobModel!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "AdMob Banner"
        adapterVersionLabel.text = "Adapter version \(SMAAdMobSmaatoBannerAdapter.version() ?? "")"
        adUnitIDLabel.text = adMobModel?.adUnitId
        adBlock.tip = MediationBannerAdsTip.TryLoadTip.rawValue
        updateAdBlockSize()

        adView = GADBannerView(adSize: adSize)
        adView.adUnitID = adUnitIDLabel.text
        adView.rootViewController = self
        adView.delegate = self
    }

    var adSize: GADAdSize {
        get {
            switch adMobModel?.adMobsize {
            case .bannerSize?: return kGADAdSizeBanner
            case .largeBannerSize?: return kGADAdSizeLargeBanner
            case .medRectSize?: return kGADAdSizeMediumRectangle
            case .leaderBoardSize?: return kGADAdSizeLeaderboard
            case .skyscraperSize?: return kGADAdSizeSkyscraper
            case .none: return kGADAdSizeBanner
            }
        }
    }

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

        let request = GADRequest()
        request.testDevices = [kGADSimulatorID]
        adView.load(request)
    }

    @IBAction func showBannerTapped(_ sender: UIButton) {
        showButton.isEnabled = false
        adBlock.addSubview(adView)
    }

    @IBAction private func enableAutoUpdateDidSwitch(_ sender: UISwitch) {
        if sender.isOn {
            adView.isAutoloadEnabled = true
        } else {
            adView.isAutoloadEnabled = false
        }
    }

    private func updateAdBlockSize() {
        adBlockWidthConstraint.constant = adSize.size.width
        adBlockHeightConstraint.constant = adSize.size.height
    }

    private func resetLabels() {
        bannerDidReceiveAdLabel.alpha = 0.2
        bannerDidFailToReceiveAdLabel.alpha = 0.2
        bannerWillPresentScreenLabel.alpha = 0.2
        bannerWillDismissScreenLabel.alpha = 0.2
        bannerDidDismissScreenLabel.alpha = 0.2
        bannerWillLeaveAppLabel.alpha = 0.2
    }
}

extension AdMobBannerAdapterViewController: GADBannerViewDelegate {
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        resetLabels()
        showButton.isEnabled = true
        adBlock.tip = MediationBannerAdsTip.LoadedTip.rawValue
        bannerDidReceiveAdLabel.alpha = 1
        NSLog("\(#function)")
    }

    func adView(_ bannerView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) {
        bannerDidFailToReceiveAdLabel.alpha = 1
        NSLog("\(#function) \(error.localizedDescription)")
    }

    func adViewWillPresentScreen(_ bannerView: GADBannerView) {
        bannerWillPresentScreenLabel.alpha = 1
        NSLog("\(#function)")
    }

    func adViewWillDismissScreen(_ bannerView: GADBannerView) {
        bannerWillDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }

    func adViewDidDismissScreen(_ bannerView: GADBannerView) {
        bannerDidDismissScreenLabel.alpha = 1
        NSLog("\(#function)")
    }

    func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
        bannerWillLeaveAppLabel.alpha = 1
        NSLog("\(#function)")
    }
}

