// 激励广告
//  RewardedVideoAd.swift
//  flutter_unionad
//
//  Created by 9Y on 2020/9/28.
//

import Foundation
import BUAdSDK

public class RewardedVideoAd : NSObject{
    public static let instance = RewardedVideoAd()
    
    var mCodeId :String?
    //激励广告参数
    private var rewardModel :BURewardedVideoModel?
    
    private var bURewardedVideoAd :BUNativeExpressRewardedVideoAd?
    
    public func loadRewardedVideoAd(params : NSDictionary) {
        LogUtil.logInstance.printLog(message: params)
        let mCodeId = params.value(forKey: "iosCodeId") as? String
        let userID = params.value(forKey: "userID") as? String
        let rewardName = params.value(forKey: "rewardName") as? String
        let rewardAmount = params.value(forKey: "rewardAmount") as? Int
        let mediaExtra = params.value(forKey: "mediaExtra") as? String
        self.rewardModel = BURewardedVideoModel()
        self.rewardModel!.userId = userID
        if rewardName != nil {
            self.rewardModel!.rewardName = rewardName
        }
        if rewardAmount != nil {
            self.rewardModel!.rewardAmount = rewardAmount!
        }
        if mediaExtra != nil {
            self.rewardModel!.extra = mediaExtra
        }
        self.bURewardedVideoAd = BUNativeExpressRewardedVideoAd(slotID: mCodeId!, rewardedVideoModel: self.rewardModel!)
        self.bURewardedVideoAd!.delegate = self
        self.bURewardedVideoAd!.loadData()
    }
    
    public func showRewardedVideoAd(){
        if(self.bURewardedVideoAd == nil){
            let map : NSDictionary = ["adType":"rewardAd",
                                      "onAdMethod":"onUnReady"]
            SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
            return
        }
        UIApplication.shared.setStatusBarHidden(true, with: UIStatusBarAnimation.none)
        self.bURewardedVideoAd!.show(fromRootViewController: MyUtils.getVC())
        LogUtil.logInstance.printLog(message: "激励广告加载成功")
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onShow"]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
}

extension RewardedVideoAd: BUNativeExpressRewardedVideoAdDelegate {
    public func nativeExpressRewardedVideoAdDidLoad(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd) {
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onReady"]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdDidClose(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd) {
        LogUtil.logInstance.printLog(message: "激励广告关闭")
        UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.none)
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onClose"]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
        self.bURewardedVideoAd = nil
    }
    
    public func nativeExpressRewardedVideoAd(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, didFailWithError error: Error?) {
        LogUtil.logInstance.printLog(message: "激励广告加载失败")
        LogUtil.logInstance.printLog(message: error)
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onFail",
                                  "error":String(error.debugDescription)]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdDidClickSkip(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd) {
        LogUtil.logInstance.printLog(message: "激励广告跳过")
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onSkip"]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdViewRenderFail(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, error: Error?) {
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onFail",
                                  "error":String(error.debugDescription)]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdServerRewardDidFail(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, error: Error?) {
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onFail",
                                  "error":String(error.debugDescription)]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdServerRewardDidSucceed(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, verify: Bool) {
        /// handle in close
        LogUtil.logInstance.printLog(message: "激励广告观看成功")
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onVerify",
                                  "rewardVerify":verify,
                                  "rewardAmount":self.rewardModel!.rewardAmount,
                                  "rewardName":self.rewardModel!.rewardName ?? ""]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdDidPlayFinish(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, didFailWithError error: Error?) {
        LogUtil.logInstance.printLog(message: "激励广告完成")
    }
    
    public func nativeExpressRewardedVideoAdDidClick(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd) {
        let map : NSDictionary = ["adType":"rewardAd",
                                  "onAdMethod":"onClick"]
        SwiftFlutterUnionadPlugin.event!.sendEvent(event: map)
    }
    
    public func nativeExpressRewardedVideoAdCallback(_ rewardedVideoAd: BUNativeExpressRewardedVideoAd, with nativeExpressVideoType: BUNativeExpressRewardedVideoAdType) {
    }
}
