import UIKit
import UnityAds

class UnityVideoAds:   CDVPlugin, UnityAdsInitializationDelegate, UnityAdsLoadDelegate, UnityAdsShowDelegate
{

    var promise: CommandPromise?
    var callbackId: String?
    var delegate: CDVCommandDelegate?
    //var lastState =  LastState()
    
    init(gameID: String, testMode: Bool, isDebug: Bool, prom: CommandPromise){
        
        promise = prom
         
        debugPrint("[UnityAds3][Videos Init]", promise ?? "PROMISE:NULL", prom)
        
        super.init()
        
        UnityAds.initialize(gameID, testMode: testMode, initializationDelegate: self)
        UnityAds.setDebugMode(isDebug)
    }
    
    
    func show(viewController: UIViewController, placementId: String,  prom: CommandPromise ){
       
        promise = prom
        callbackId = prom.callback
        
        self.viewController = viewController
        UnityAds.load(placementId, loadDelegate: self)
        
    }
    
    
    //NEW VIDEOADS SHOW EVENTS - Implement the delegate methods:
    //MARK: : UnityAdsShowDelegate
    
    func unityAdsShowComplete(_ placementId: String, withFinish state: UnityAdsShowCompletionState) {
        
        debugPrint("[UnityAds3][unityAdsShowComplete]")
        
       
        if let nPromise = self.promise {
            if state == .showCompletionStateSkipped{
                LastState.shared.LAST_STATUS = "SKIPPED"
                debugPrint("[UnityAds3][unityAdsShowComplete]", placementId, "SKIPPED")
                nPromise.resolve(msg:  String(format: "[\"%@\",\"%@\"]", placementId, "SKIPPED") )
                promise = nil
             }
            else if state == .showCompletionStateCompleted {
                LastState.shared.LAST_STATUS = "COMPLETED"
                 debugPrint("[UnityAds3][unityAdsShowComplete]", placementId, "COMPLETED")
                 nPromise.resolve(msg:  String(format: "[\"%@\",\"%@\"]", placementId, "COMPLETED"))
                 promise = nil
             }
        }

    }
    
    func unityAdsShowFailed(_ placementId: String, withError error: UnityAdsShowError, withMessage message: String) {
        debugPrint("[UnityAds3][unityAdsShowFailed]", placementId, message )
        
        if let nPromise = promise {
            if error == .showErrorAlreadyShowing{
               nPromise.resolve(msg:  "SHOW_ERROR_ALREADY_SHOWING")
            }
            else if error == .showErrorInvalidArgument {
                nPromise.resolve(msg:  "SHOW_ERROR_INVALID_ARGUMENT")
            }
            else if error == .showErrorNoConnection {
                nPromise.resolve(msg:  "SHOW_ERROR_NO_CONNECTION")
            }
            else if error == .showErrorNotReady {
                nPromise.resolve(msg:  "SHOW_ERROR_NOT_READY")
            }
            else if error == .showErrorVideoPlayerError {
                nPromise.resolve(msg:  "SHOW_ERROR_VIDEO_PLAYER_ERROR")
            }
            else {
               nPromise.resolve(msg:  "SHOW_ERROR_INTERNAL_ERROR")
            }
            
            promise = nil

        }
    }
    
    func unityAdsShowStart(_ placementId: String) {
        debugPrint("[UnityAds3][unityAdsShowStart]", placementId)
        LastState.shared.LAST_STATUS = "AD_SHOWING"
        
        if let nPromise = promise {
            nPromise.resolve(msg: String(format: "[\"%@\",\"%@\"]", placementId, "SHOWING"))
        }
    }
    
    func unityAdsShowClick(_ placementId: String) {
        debugPrint("[UnityAds3][unityAdsShowClick]", placementId)
        LastState.shared.LAST_STATUS = "AD_CLICKED"
        if let nPromise = promise {
            nPromise.resolve(msg: String(format: "[\"%@\",\"%@\"]", placementId, "SHOWN_AD_CLICK"))
        }
    }
    
    //NEW VIDEOADS INITIALIZATION EVENTS - Implement the delegate methods:
    //MARK: UnityAdsInitializationDelegate
    func initializationComplete() {
        debugPrint("[UnityAds3][initializationComplete][unityAdsReady]")
        LastState.shared.LAST_STATUS = "AD_CLICKED"
        if let nPromise = promise {
            nPromise.resolve(msg: String(format: "[\"%@\",\"%@\"]", "UNITYADS", "READY"))
        }
    }
    
    func initializationFailed(_ error: UnityAdsInitializationError, withMessage message: String) {
        debugPrint("[UnityAds3][initializationFailed]")
        
        if let nPromise = promise {
            
            debugPrint("[UnityAds3][unityAdsDidError]", message)

            
            if error == .initializationErrorAdBlockerDetected {
               nPromise.resolve(msg:  "INIT_FAIL_AD_BLOCKER_DETECTED")
            }
            else if error == .initializationErrorInvalidArgument {
                nPromise.resolve(msg:  "INIT_FAIL_INVALID_ARGUMENT")
            }
            else {
               nPromise.resolve(msg:  "INIT_FAIL_INTERNAL_ERROR")
            }
            
            promise = nil

        }
    }

    //NEW FROM V4 version and up
    //MARK: UnityAdsLoadDelegate
    func unityAdsAdLoaded(_ placementId: String) {
        debugPrint("[UnityAds3][UnityAdLoaded]", placementId)
        
        if let nPromise = promise {
            nPromise.resolve(msg: String(format: "[\"%@\",\"%@\"]", "UNITYADS", "AD_LOADED"))
        }
        
        UnityAds.show(self.viewController, placementId: placementId, showDelegate: self)

    }
    func unityAdsAdFailedToLoad(_ error: UnityAdsLoadError, withMessage message: String){
        debugPrint("[UnityAds3][UnityAdsAdFailedToLoad]", error, message)
    }
    func unityAdsAdFailed(toLoad placementId: String, withError error: UnityAdsLoadError, withMessage message: String) {
        debugPrint("[UnityAds3][unityAdsAdFailed]", error, message)
    }
    
    
}
