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

public class KwaiVideoAd 
{
    private KwaiAds.Scripts.Api.Reward.IRewardAdController _kwaiReward;
    private string _unitId;
    private IKwaiAdListener _kwaiAdListener;

    public IKwaiAdListener KwaiAdListener => _kwaiAdListener;

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

    public void Load(string ecpmPrice = "0.01")
    {
        if(_kwaiReward != null)
        {
            _kwaiReward.Destroy();
            _kwaiReward = null;
        }

        _kwaiReward = KwaiAds.Scripts.Api.KwaiAdsSdk.SDK.getRewardAdController();

        // 构建KwaiRewardAdRequest
        KwaiRewardAdRequest kwaiRewardAdRequest = new KwaiRewardAdRequest(_unitId);
        // 选填， 可以设置低价 单位是$(美元，ecpm)
        kwaiRewardAdRequest.ExtParams[Constants.Request.BID_FLOOR_PRICE] = ecpmPrice;
        // 加载过程接受三个参数，
        // - KwaiRewardAdRequest 配置请求参数
        // - IRewardAdListener实现类，回调为激励
        _kwaiReward.Load(kwaiRewardAdRequest, new RewardAdListener(this), new RewardAdLoadListener(this));

    }

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

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

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

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


    private class RewardAdListener : IRewardAdListener
    {
        private KwaiVideoAd _kwaiVideoAd;

        public RewardAdListener(KwaiVideoAd videoAd)
        {
            _kwaiVideoAd = videoAd;
        }

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

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

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

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

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

        public void OnRewardEarned()
        {
            KwaiLog.Log($"#Kwai OnRewardEarned");
            _kwaiVideoAd.KwaiAdListener.OnReward();
        }
    }

    private class RewardAdLoadListener : IRewardAdLoadListener
    {
        private KwaiVideoAd _kwaiVideoAd;

        public RewardAdLoadListener(KwaiVideoAd videoAd)
        {
            _kwaiVideoAd = videoAd;
        }

        public void OnAdLoadFailed(string trackId, int code, string msg)
        {
            KwaiLog.Log($"#Kwai OnAdLoadFailed, trackId = {trackId}, code = {code}, msg = {msg}");
            _kwaiVideoAd.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}");
            _kwaiVideoAd.KwaiAdListener.OnLoaded(trackId, price);
        }
    }
}
