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

    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: [.native], 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? GADNativeAdView 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: GADAdLoaderDelegate {
    func adLoader(_ adLoader: GADAdLoader, didFailToReceiveAdWithError error: Error) {
        NSLog("\(String(describing: view)) did fail to load")
        adBlockView.tip = MediationNativeAdsTip.ErrorTip.rawValue
        nativeDidFailLabel.alpha = 1
    }
    
    func adLoaderDidFinishLoading(_ adLoader: GADAdLoader) {
        resetLabels()
        showButton.isEnabled = true
        adBlockView.tip = MediationNativeAdsTip.LoadedTip.rawValue
        nativeDidLoadLabel.alpha = 1
    }
    
}

extension AdMobNativeAdapterViewController: GADNativeAdLoaderDelegate {
    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
        self.nativeAd = nativeAd
    }
}
