using System.Collections;
using System.Collections.Generic;
using KwaiAds.Scripts.Api;
using KwaiAds.Scripts.Api.Interstitial;
using UnityEngine;

public class KwaiInterAd 
{
    private IInterstitialAdController _kwaiInter;
    private string _unitId;
    private IKwaiAdListener _kwaiAdListener;

    public IKwaiAdListener KwaiAdListener => _kwaiAdListener;

    public KwaiInterAd(string unitId, IKwaiAdListener adListener)
    {
        _unitId = unitId;
        _kwaiAdListener = adListener;
    }

    public void Load(string ecpmPrice = "0.01")
    {
        // 获取每次load都需要获取新的KwaiInterstitialAdController
        if (_kwaiInter != null)
        {
            _kwaiInter.Destroy();
            _kwaiInter = null;
        }
        _kwaiInter = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getInterstitialAdController();
        // 构建KwaiInterstitialAdRequest
        KwaiInterstitialAdRequest interstitialRewardAdRequest = new KwaiInterstitialAdRequest(_unitId); // tagId必填
                                                                                                                       // 选填， 可以设置低价 单位是$(美元，ecpm)
        interstitialRewardAdRequest.ExtParams[Constants.Request.BID_FLOOR_PRICE] = ecpmPrice;
        // 加载过程接受三个参数，
        // - KwaiInterstitialAdRequest 配置请求参数
        // - IInterstitialAdListener 回调为插页点击、曝光状态
        // - IInterstitialAdLoadListener 回调为插页加载状态
        _kwaiInter.Load(interstitialRewardAdRequest, new InterstitialAdListener(this), new InterstitialAdLoadListener(this));
    }

    public void Show()
    {
        if (IsReady())
        {
            _kwaiInter.Show();
        }
    }


    public bool IsReady()
    {
        return _kwaiInter != null && _kwaiInter.IsReady();
    }

    public void NotifyWin(string minWinPrice)
    {
        //_kwaiInter?.NotifyWin();//报错暂时屏蔽
        _kwaiInter?.NotifyWin(minWinPrice);
    }

    public void NotifyLoss(string winPrice)
    {
        //_kwaiInter?.NotifyLoss();
        _kwaiInter?.NotifyLoss(winPrice);
    }

    private class InterstitialAdListener : IInterstitialAdListener
    {
        private KwaiInterAd _kwaiInterAd;

        public InterstitialAdListener(KwaiInterAd interAd)
        {
            _kwaiInterAd = interAd;
        }

        public void OnAdClick()
        {
            KwaiLog.Log($"#Kwai OnAdClick");
            _kwaiInterAd?.KwaiAdListener?.OnClick();
        }

        public void OnAdClose()
        {
            KwaiLog.Log($"#Kwai OnAdClose");
            _kwaiInterAd?.KwaiAdListener?.OnClosed();
        }

        public void OnAdPlayComplete()
        {
            KwaiLog.Log($"#Kwai OnAdPlayComplete");
        }

        public void OnAdShow()
        {
            KwaiLog.Log($"#Kwai OnAdShow");
            _kwaiInterAd?.KwaiAdListener?.OnShow();
        }

        public void OnAdShowFailed(int code, string msg)
        {
            KwaiLog.Log($"#Kwai OnAdShowFailed code = {code}, msg = {msg}");
            _kwaiInterAd?.KwaiAdListener?.OnShowFailed(code, msg);
        }
    }

    private class InterstitialAdLoadListener : IInterstitialAdLoadListener
    {
        private KwaiInterAd _kwaiInterAd;
        public InterstitialAdLoadListener(KwaiInterAd interAd)
        {
            _kwaiInterAd = interAd;
        }

        public void OnAdLoadFailed(string trackId, int code, string msg)
        {
            KwaiLog.Log($"#Kwai OnAdLoadFailed trackId = {trackId}, code = {code}, msg = {msg}");
            _kwaiInterAd?.KwaiAdListener?.OnLoadFailed(trackId, code, msg);
        }

        public void OnAdLoadStart(string trackId)
        {
            KwaiLog.Log($"#Kwai OnAdLoadStart trackId = {trackId}");
        }

        public void OnAdLoadSuccess(string trackId, string price)
        {
            KwaiLog.Log($"#Kwai OnAdLoadSuccess trackId = {trackId}, price = {price}");
            _kwaiInterAd?.KwaiAdListener?.OnLoaded(trackId, price);
        }
    }


}
