using System;
using StarkSDKSpace;
using UnityEngine;

namespace TyphoonUnitySDK
{
    /// <summary>
    /// 抖音banner条广告
    /// </summary>
    public class DouyinBannerAd : MonoBehaviour
    {
        //样式
        private StarkAdManager.BannerStyle _style = null;

        //缓存的加载广告
        private StarkAdManager.BannerAd _banner = null;

        //当前的展示状态
        public bool StateOn = false;


        /*是否在加载中*/
        public bool IsLoading = false;


        /*广告位置*/
        public BannerPosition BannerPosition;


        /*初始化*/
        public void Initialize(BannerPosition position)
        {
            BannerPosition = position;
            switch (position)
            {
                case BannerPosition.Bottom:
                {
                    SdkDebug.Log("Initialize Banner Bottom");
                    //底部居中
                    _style = new StarkAdManager.BannerStyle();
                    _style.width = 320;
                    int h = _style.height;
                    int w = _style.width;
                    var sh = px2dp(Screen.height);
                    int sw = px2dp(Screen.width);
                    _style.top = sh - h;
                    _style.left = sw / 2 - w / 2;
                    SdkDebug.Log($"top:{_style.top}");
                }
                    break;
                case BannerPosition.Top:
                {
                    SdkDebug.Log("Initialize Banner Top");
                    //顶部居中
                    _style = new StarkAdManager.BannerStyle();
                    _style.width = 320;
                    int h = _style.height;
                    int w = _style.width;
                    var sh = px2dp(Screen.height);
                    int sw = px2dp(Screen.width);
                    _style.top = h;
                    _style.left = sw / 2 - w / 2;
                    SdkDebug.Log($"top:{_style.top}");
                }
                    break;
                default:
                    throw new Exception($"未处理的类型：{position}");
            }
        }


        /*加载广告*/
        private void LoadBanner()
        {
            SdkDebug.Log($"LoadBanner {BannerPosition}");
            IsLoading = true;
            _banner = StarkSDK.API.GetStarkAdManager().CreateBannerAd(AppConfig.DouyinBannerId, _style, 60,
                OnAdError, OnLoadedDone);
        }

        /*广告加载失败*/
        private void OnAdError(int code, string err)
        {
            SdkDebug.Log($"Banner OnAdError code:{code} err:{err} ");
            Kill();
        }

        private void Kill()
        {
            SdkDebug.Log($"Kill Banner {BannerPosition}");
            IsLoading = false;
            _banner?.Destroy();
            _banner = null;
            OnStateChanged();
        }

        private void OnLoadedDone()
        {
            SdkDebug.Log($"Banner OnLoadedDone {BannerPosition}");
            IsLoading = false;
            OnStateChanged();
        }

        //设置显示状态
        public void SetState(bool isOn)
        {
            if (StateOn == isOn)
            {
                return;
            }

            StateOn = isOn;
            OnStateChanged();
        }

        //同步展示状态
        private void OnStateChanged()
        {
            if (StateOn)
            {
                //开启
                if (_banner == null)
                {
                    if (IsLoading)
                    {
                        //加载中，跳过
                    }
                    else
                    {
                        //不在加载，开启加载
                        LoadBanner();
                    }
                }
                else
                {
                    if (!IsLoading)
                    {
                        _banner.Show();
                    }
                }
            }
            else
            {
                //关闭
                if (_banner != null && !IsLoading)
                {
                    Kill();
                }
            }
        }


        private int px2dp(int px) => (int)(px * (160 / Screen.dpi));
    }
}