import UIKit
import AdViewSDK

fileprivate let storedImpDisplayRewarded = "POSIDxdsaplpm4ar1"

class InAppDisplayRewardedViewController: InterstitialBaseViewController, RewardedAdUnitDelegate {
    
    // adview
    private var rewardedAdUnit: RewardedAdUnit!

    override func loadView() {
        super.loadView()
        
        createAd()
    }
    
    func createAd() {
        // 1. Create a RewardedAdUnit
        rewardedAdUnit = RewardedAdUnit(configID: storedImpDisplayRewarded)
        rewardedAdUnit.delegate = self
        
        // 2. Load the rewarded ad
        rewardedAdUnit.loadAd()
    }
    
    // MARK: - RewardedAdUnitDelegate
    
    func rewardedAdDidReceiveAd(_ rewardedAd: RewardedAdUnit) {
        if rewardedAd.isReady {
            rewardedAd.show(from: self)
        }
    }
    
    func rewardedAd(_ rewardedAd: RewardedAdUnit, didFailToReceiveAdWithError error: Error?) {
        AdViewDemoLogger.shared.error("Rewarded ad unit did fail to receive ad: \(error?.localizedDescription ?? "")")
    }
    
    func rewardedAdUserDidEarnReward(_ rewardedAd: RewardedAdUnit, reward: AdViewReward) {
        AdViewDemoLogger.shared.info("User did earn reward: type - \(reward.type ?? ""), count - \(reward.count ?? 0)")
    }
}
