using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AD.VosacoSDK
{
    public class VosacoRewardAd
    {
        private AndroidJavaObject _rewardAd;
        private VosacoRewardAdListenerProxy _listenerProxy;

        public VosacoRewardAd(string unitId)
        {
            if (Application.platform != RuntimePlatform.Android)
                return;

            try
            {
                // 创建Java插屏广告对象
                _rewardAd = new AndroidJavaObject("com.rixengine.unity_plugin.RewardAd", unitId);

                // 创建并设置监听器
                _listenerProxy = new VosacoRewardAdListenerProxy();
                _rewardAd.Call("SetListener", _listenerProxy);
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"RewardAd creation failed: {e.Message}");
            }
        }

        // 加载广告
        public void LoadAd()
        {
            if (!IsValid()) return;

            try
            {
                _rewardAd.Call("Load");
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"LoadAd failed: {e.Message}");
            }

        }

        // 显示广告
        public void ShowAd()
        {
            if (!IsValid()) return;
            try
            {
                _rewardAd.Call("Show");
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"ShowAd failed: {e.Message}");
            }

        }

        // 检查广告是否就绪
        public bool IsAdReady()
        {
            if (!IsValid()) return false;

            try
            {
                return _rewardAd.Call<bool>("IsReady");
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"IsAdReady error :{e.Message}");
            }

            return false;
        }

        // 获取广告价格
        public double GetAdPrice()
        {
            if (!IsValid()) return 0;
            try
            {
                return _rewardAd.Call<double>("GetPrice");
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"GetAdPrice error :{e.Message}");
            }
            return 0;
        }

        // 销毁广告对象
        public void Destroy()
        {
            if (!IsValid()) return;

            try
            {
                _rewardAd.Call("Destroy");
                _rewardAd?.Dispose();
                _rewardAd = null;
            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"Destroy error :{e.Message}");
            }

        }

        // 注册事件监听器
        public void AddLoadedListener(Action callback)
            => _listenerProxy.OnLoaded += callback;

        public void AddLoadFailedListener(Action callback)
            => _listenerProxy.OnLoadFailed += callback;

        public void AddClickedListener(Action callback)
            => _listenerProxy.OnClicked += callback;

        public void AddRewardListener(Action callback)
            => _listenerProxy.OnReward += callback;

        public void AddCloseListener(Action callback)
            => _listenerProxy.OnClose += callback;

        public void AddVideoStartListener(Action callback)
            => _listenerProxy.OnVideoStart += callback;

        public void AddVideoEndListener(Action callback)
            => _listenerProxy.OnVideoEnd += callback;

        public void AddVideoErrorListener(Action callback)
            => _listenerProxy.OnVideoError += callback;


        private bool IsValid()
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                VosacoAdSDK.LogD("Interstitial ads only supported on Android");
                return false;
            }
            if (_rewardAd == null)
            {
                VosacoAdSDK.LogE("Interstitial ad not initialized");
                return false;
            }
            return true;
        }
    }
}
