using BingoBrain.Core;
using BingoBrain.HotFix;
using DontConfuse;
using FGUI.ACommon;
using FGUI.webview;
using UnityEngine;

namespace BingoBrain
{
    public class H5UI : BaseUI
    {
        private H5UICtrl ctrl;
        private H5Model model;
        private com_webview ui;

        public H5UI(H5UICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.H5UI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "webview";
            uiInfo.assetName = "com_webview";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期
        protected override void OnInit()
        {
            //model = ModuleManager.Instance.GetModel(ModelConst.H5Model) as H5Model;
        }

        protected override void OnClose()
        {
            WebviewManager.Instance.setInH5View(false);
            WebviewManager.Instance.ShowH5View(false);
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.webview.com_webview;
        }

        protected override void OnOpenBefore(object args)
        {
            WebviewManager.Instance.setInH5View(true);
            // delayedCall = DOVirtual.DelayedCall(0.3f, () =>
            //   {
            WebviewManager.Instance.ShowH5View(true);
            ui.btn_close.SetClick(() =>
            {
                UICtrlDispatcher.Instance.Dispatch(SkinInfo.H5UI_Close);
            });
            InitView();

        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnDisplay(object args)
        {
        }
        #endregion

        #region 消息
        protected override void AddListener()
        {

        }
        protected override void RemoveListener()
        {

        }
        #endregion

        //初始化页面逻辑
        private void InitView()
        {

        }
        public void ClickBtn(string name)
        {
            // // Debug.Log("[UNITY] Click btn: " + name);
            // FGUIHelper.PlayClickSound();
            if (name == "flyBtn")
            {
                OnClickFlyCash();
                NetworkKit.BuriedPoint(BuriedPointEvent.Hall_behavior, BuriedPointEvent.collect_fly_number, 1);
                //NetworkKit.BuriedPoint(BuriedPointEvent.Hall_behavior,BuriedPointEvent.collect_fly_people,1);
            }
            else if (name == "rewardBtn")
            {
                OnClickIcon();

            }
        }
        private void OnClickFlyCash()
        {
            // PreferencesMgr.Instance.AdCashFlyShowTime =
            //     GameHelper.GetNowTime() + ConfigSystem.GetConfig<CommonModel>().flyCD;
            // var rewardData = new RewardData();

            // var singleValue =
            //     (decimal)Math.Round(
            //         UnityEngine.Random.Range(ConfigSystem.GetConfig<CommonModel>().flyReward[0],
            //             ConfigSystem.GetConfig<CommonModel>().flyReward[1]), 2);


            // decimal rewardValue;


            // if (flyClickCount >= flyNeedClickCount)
            // {
            //     flyClickCount = -1;
            //     //var rate = GameHelper.GetVideoRate();
            //     rewardValue = singleValue;
            //     GameHelper.ShowVideoAd("reward_fly", isCompleted =>
            //     {
            //         if (isCompleted)
            //         {
            //             GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);
            //             GameDispatcher.Instance.Dispatch(GameMsg.RefreshMakeupData);
            //         }
            //     });
            // }
            // else
            // {
            //     rewardValue = singleValue;
            // }

            // var startPosition = GameHelper.GetUICenterPosition(ui.com_fly);
            // var endPosition = GameHelper.GetUICenterPosition(ui.com_top.text_num);

            // var rewardSingleData = new RewardSingleData(102, rewardValue, RewardOrigin.H5Fly101)
            // {
            //     startPosition = startPosition,
            //     endPosition = new Vector2(endPosition.x - 150, endPosition.y - 106)
            // };

            // rewardData.AddReward(rewardSingleData);
            // rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            // rewardData.AddCompleted(isSuccess =>
            // {
            //     if (isSuccess)
            //     {
            //         flyClickCount++;
            //     }

            //     SetMakeup();
            // });
            // //GameHelper.addMoney((float)rewardValue);
            // GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);
            // GameDispatcher.Instance.Dispatch(GameMsg.RefreshMakeupData);
            // CheckShowFly();
        }
        private void OnClickIcon()
        {
            //             //WebThroughUtil.WebThroughClick(H5WebThroughType.OnlineH5, ui.btn_icon);
            //             if (iconTime < ConfigSystem.GetConfig<CommonModel>().ThroughRewardCD)
            //             {
            //                 return;
            //             }

            // #if UNITY_IOS && !UNITY_EDITOR
            //                         WebviewManager.Instance.ShowH5View(false);
            // #endif

            //             float[] cash_array = GameHelper.GetRewardValue(2);

            //             // var temp = new { is_success = true, cash_number = cash_array[0], rate = cash_array[1], is_level_success = false, is_h5_reward = true };
            //             SuccessData temp = new SuccessData();
            //             temp.IsWin = true;
            //             temp.cash_number = cash_array[0];
            //             temp.rate = (int)cash_array[1];
            //             temp.IsLevelSuccess = false;
            //             temp.IsH5Reward = true;
            //             UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.LevelSuccessUI_Open, temp);

            //             //UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.RewardUI_Open);
            //             // var rewardData = new RewardData();

            //             // var rewardType = 111;
            //             // decimal rewardValue = (decimal)GameHelper.GetRewardValue(3)[0];

            //             // var rewardSingleData = new RewardSingleData(rewardType, rewardValue, RewardOrigin.H5Icon)
            //             // {
            //             //     startPosition = GameHelper.GetUICenterPosition(ui.btn_icon),
            //             //     endPosition = GameHelper.GetUICenterPosition(ui.com_top.text_num)
            //             // };
            //             // rewardData.AddReward(rewardSingleData);
            //             // rewardData.AddCompleted(isSuccess =>
            //             // {
            //             //     ui.btn_icon.cont_state.selectedIndex = btn_icon.State_none;
            //             //     iconTime = 0;
            //             //     SetMakeup();
            //             // });
            //             // rewardData.condition = RewardCondition.AD;
            //             // rewardData.displayType =
            //             //     RewardDisplayType.Dialog | RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;

            //             // GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);

            //             // GameHelper.addMoney((float)rewardValue);
            //             // GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);
            //             // GameDispatcher.Instance.Dispatch(GameMsg.RefreshMakeupData);


            //             iconTime = 0;
            //             // SetMakeup();
            //             NetworkKit.BuriedPoint(BuriedPointEvent.Hall_behavior, BuriedPointEvent.annular_get_number, 1);
        }
        private void CheckShowFly()
        {
            // var nowTime = GameHelper.GetNowTime();
            // var leftTime = (int)(PreferencesMgr.Instance.AdCashFlyShowTime - nowTime);

            // ui.com_fly.visible = false;
            // ui.com_fly.visible = leftTime <= 0;
            // if (leftTime > 0)
            // {
            //     WebviewManager.Instance.ShowFlyBtn(false);
            //     isFlyShow = false;
            // }
            // else
            // {
            //     //StartFly();
            //     if (!isFlyShow) WebviewManager.Instance.ShowFlyBtn(true);
            //     isFlyShow = true;

            // }
        }
    }
}