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

namespace AD.VosacoSDK
{
    public class VosacoInterAd 
    {
        private AndroidJavaObject _interAd;
        private VosacoInterAdListenerProxy _listenerProxy;

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

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

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

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

            try
            {
                _interAd.Call("Load");

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

        }

        // 显示广告
        public void ShowAd()
        {
            if (!IsValid()) return;
            try
            {
                _interAd.Call("Show");

            }
            catch (Exception e)
            {
                VosacoAdSDK.LogE($"ShowAd failed: {e.Message}"); 
            }

        }

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

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

            return false;
        }

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

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

            try
            {
                _interAd.Call("Destroy");
                _interAd?.Dispose();
                _interAd = 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 AddShowListener(Action callback)
            => _listenerProxy.OnShow += 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 (_interAd == null)
            {
                VosacoAdSDK.LogE("Interstitial ad not initialized");
                return false;
            }
            return true;
        }
    }
}
