import UIKit
import AdViewSDK

fileprivate let storedImpDisplayInterstitial = "POSIDzuqwln00ml38"

class InAppDisplayInterstitialViewController: InterstitialBaseViewController, InterstitialAdUnitDelegate {

    private var renderingInterstitial: InterstitialRenderingAdUnit!
    private var didStartLoading = false

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        guard !didStartLoading else { return }
        didStartLoading = true
        createAd()
    }

    func createAd() {
        renderingInterstitial = InterstitialRenderingAdUnit(configID: storedImpDisplayInterstitial)
        renderingInterstitial.adFormats = [.banner]
        renderingInterstitial.delegate = self
        renderingInterstitial.loadAd()
    }

    func interstitialDidReceiveAd(_ interstitial: InterstitialRenderingAdUnit) {
        DispatchQueue.main.async {
            interstitial.show(from: self)
        }
    }

    func interstitial(_ interstitial: InterstitialRenderingAdUnit, didFailToReceiveAdWithError error: Error?) {
        AdViewDemoLogger.shared.error("\(error?.localizedDescription ?? "")")
    }
}
