//
//  AdMobNativeAdapterViewController.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 16.04.20.
//  Copyright © 2020 Smaato Inc. All rights reserved.
//

import UIKit
import SmaatoSDKCore
import GoogleMobileAds

class AdMobNativeAdapterViewController: UIViewController {

    @IBOutlet private weak var adBlockView: AdBlockView!
    @IBOutlet private weak var adUnitIDLabel: UILabel!
    @IBOutlet private weak var showButton: UIButton!
    @IBOutlet private weak var adapterVersionLabel: UILabel!
    
    @IBOutlet private weak var nativeDidLoadLabel: UILabel!
    @IBOutlet private weak var nativeDidFailLabel: UILabel!
    @IBOutlet private weak var nativeWillPresentScreenLabel: UILabel!
    @IBOutlet private weak var nativeWillDismissScreenLabel: UILabel!
    @IBOutlet private weak var nativeDidDismissScreenLabel: UILabel!
    @IBOutlet private weak var nativeWillLeaveAppLabel: UILabel!

    var adMobModel: AdMobModel!
    private var adLoader: GADAdLoader?
    private var nativeAd: GADUnifiedNativeAd?

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

    //MARK: Actions

    @IBAction private func buttonLoadTapped(_ sender: UIButton) {
        resetLabels()
        adBlockView.tip = MediationNativeAdsTip.LoadingTip.rawValue
        
        let options = GADNativeAdImageAdLoaderOptions()
        options.disableImageLoading = false
        
        adLoader = GADAdLoader(adUnitID: adMobModel.adUnitId, rootViewController: self, adTypes: [.unifiedNative], options: [options])
        adLoader?.delegate = self

        let request = GADRequest()
        adLoader?.load(request)
    }

    @IBAction private func buttonShowTapped(_ sender: UIButton) {
        
        guard let nativeAd = nativeAd else { return }
        
        showButton.isEnabled = false

        let nibView = Bundle.main.loadNibNamed("AdMobNativeAdapterAdView", owner: nil, options: nil)?.first
        guard let nativeAdView = nibView as? GADUnifiedNativeAdView else { return }
        
        nativeAdView.frame = self.adBlockView.bounds
        for view in self.adBlockView.subviews {
            view.removeFromSuperview()
        }
        self.adBlockView.addSubview(nativeAdView)
        
        nativeAdView.nativeAd = nativeAd
        
        (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
        (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
        (nativeAdView.callToActionView as? UILabel)?.text =  nativeAd.callToAction
        nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
        (nativeAdView.imageView as? GADMediaView)?.mediaContent?.mainImage = nativeAd.images?.first?.image
        (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
    }

    //MARK: Private
    
    private func setupUI() {
        adUnitIDLabel.text = adMobModel.adUnitId
        adapterVersionLabel.text = "Adapter version \(SMAAdMobSmaatoNativeAdapter.version() ?? "")"
        adBlockView.tip = MediationNativeAdsTip.TryLoadTip.rawValue
    }

    private func resetLabels() {
        nativeDidLoadLabel.alpha = 0.2
        nativeDidFailLabel.alpha = 0.2
        nativeWillPresentScreenLabel.alpha = 0.2
        nativeWillDismissScreenLabel.alpha = 0.2
        nativeDidDismissScreenLabel.alpha = 0.2
        nativeWillLeaveAppLabel.alpha = 0.2
    }
}

extension AdMobNativeAdapterViewController: GADUnifiedNativeAdLoaderDelegate {
    
    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
        resetLabels()
        self.nativeAd = nativeAd
        self.nativeAd?.delegate = self
        showButton.isEnabled = true
        adBlockView.tip = MediationNativeAdsTip.LoadedTip.rawValue
        nativeDidLoadLabel.alpha = 1
        NSLog("\(String(describing: view))) did load")
    }
    
    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: GADRequestError) {
        NSLog("\(String(describing: view)) did fail to load")
        adBlockView.tip = MediationNativeAdsTip.ErrorTip.rawValue
        nativeDidFailLabel.alpha = 1
    }
}

extension AdMobNativeAdapterViewController: GADUnifiedNativeAdDelegate {
    
    func nativeAdDidRecordImpression(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(#function)")
    }
    
    func nativeAdDidRecordClick(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(#function)")
    }
    
    func nativeAdWillPresentScreen(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(String(describing: view)) did will present screen")
        nativeWillPresentScreenLabel.alpha = 1
    }
    
    func nativeAdWillDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(String(describing: view)) will dismiss screen")
        nativeWillDismissScreenLabel.alpha = 1
    }
    
    func nativeAdDidDismissScreen(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(String(describing: view)) did dismiss screen")
        nativeDidDismissScreenLabel.alpha = 1
    }
    
    func nativeAdWillLeaveApplication(_ nativeAd: GADUnifiedNativeAd) {
        NSLog("\(String(describing: view)) will leave application")
        nativeWillLeaveAppLabel.alpha = 1
    }
}
