using System.Collections.Generic;
using DG.Tweening;
using FairyGUI;
using FGUI.Common_01;
using FGUI.rank;
using IgnoreOPS;
using SGModule.Net;
using SGModule.NetKit;
using UnityEngine;

namespace ChillConnect
{
    public class RankRewardUI : BaseUI
    {
        private RankRewardUICtrl ctrl;
        private RankRewardModel model;
        private com_rank_reward ui;

        public btn_watchAd btn_WatchAd;

        List<RankRewardData> rewardList = new List<RankRewardData>();
        rankType curType = rankType.hour;
        int selfRank = 0;

        public RankRewardUI(RankRewardUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.RankRewardUI;
            this.ctrl = ctrl;
        }


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

        #region 生命周期
        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            AdRedeemManager.Instance.Destroy();
        }

        protected override void OnBind()
        {
            ui = baseUI as com_rank_reward;
        }

        protected override void OnOpenBefore(object args)
        {
            if (Screen.safeArea.y != 0)
            {
                ui.group_top.y += 68;
            }
            rewardList = SaveData.GetRankData().rankRewardData;
            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()
        {
            ui.btn_close.SetClick(() =>
            {
                GameHelper.addInterAdnumber();
                CtrlCloseUI();
            });

            ui.lab_pot.text = $"{SaveData.GetSaveObject().saveingpot_ch:N}";
            var head = ui.head_1 as FGUI.rank.btn_head;
            GameHelper.SetSelfAvatar(head.load_avatar);
            head = ui.head_2 as FGUI.rank.btn_head;
            TextureHelper.SetAvatarToLoader(2, head.load_avatar);
            head = ui.head_3 as FGUI.rank.btn_head;
            TextureHelper.SetAvatarToLoader(3, head.load_avatar);

            ui.list_rank.itemRenderer = refreshItem;
            ui.list_rank.numItems = rewardList.Count;

            float rewardNum = 0;
            for (int i = 0; i < rewardList.Count; i++)
            {
                rewardNum += rewardList[i].bonus;
            }

            ui.lab_total.text = rewardNum.ToString();

            if (GameHelper.IsGiftSwitch() && ConfigSystem.GetConfig<CommonModel>().PiggyBankSwitch == 1)
            {
                ui.btn_collect.GetChild("img_saveingpot").visible = true;
            }

            ui.btn_collect.SetClick(() =>
            {

                // NetworkKit.BuriedPoint(BuriedPointEvent.Apple_AD_event, BuriedPointEvent.gold_click_ad,1);
               // TrackKit.SendEvent(Property.GoldClickAd);
                GameHelper.ShowVideoAd("CollectRankReward", issuccess =>
                {
                    if (issuccess)
                    {
                        DOVirtual.DelayedCall(0.01f, () =>
                        {
                            SaveData.GetSaveObject().saveingpot_ch += rewardNum;
                            GetReward(rewardNum);

                            SaveData.GetRankData().rankRewardData.Clear();
                            GameDispatcher.Instance.Dispatch(GameMsg.RefreshRedDot);
                        });

                    }
                });
            });

        }

        private void refreshItem(int index, GObject obj)
        {
            if (rewardList.Count <= index)
            {
                return;
            }
            rank_item2 item = (rank_item2)obj;
            item.lab_ranking.text = rewardList[index].rank.ToString();

            // DateTime utcTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(rewardList[index].time);
            // DateTime LocalTime = utcTime.ToLocalTime();


            if (rankType.hour == rewardList[index].type)
            {
                item.lab_type.text = "Hour";
            }
            else if (rankType.day == rewardList[index].type)
            {
                item.lab_type.text = "Day";
            }
            else if (rankType.week == rewardList[index].type)
            {
                item.lab_type.text = "Week";
            }
            item.lab_pot.text = rewardList[index].bonus.ToString();
            item.lab_time.text = rewardList[index].time;
            // item.lab_time.text = LocalTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void GetReward(float awardNum)
        {
            var rewardData = new RewardData();

            int curr_type = 112;
            var end_ = GameHelper.GetUICenterPosition(ui.lab_pot, true);
            if (GameHelper.GetCommonModel().PiggyBankSwitch != 1)
                curr_type = 101;
            var rewardSingleData = new RewardSingleData(curr_type, (decimal)awardNum, RewardOrigin.Play)
            {
                startPosition = GameHelper.GetUICenterPosition(ui.btn_collect, true),
                endPosition = new Vector2(end_.x - 110, end_.y - 110),
            };

            rewardData.AddReward(rewardSingleData);
            rewardData.displayType = RewardDisplayType.RewardFly | RewardDisplayType.ValueChange;
            rewardData.AddCompleted(success =>
            {
                if (success)
                {
                    // DataMgr.Ticket.Value -= (decimal)awardNum;
                    if (GameHelper.GetCommonModel().PiggyBankSwitch != 1) DataMgr.Coin.Value -= (int)awardNum;
                    GameDispatcher.Instance.Dispatch(GameMsg.Update102);
                    DOVirtual.DelayedCall(0.7f, () =>
                    {
                        CtrlCloseUI();
                    });
                }
            });

            GameDispatcher.Instance.Dispatch(GameMsg.GetReward, rewardData);
            DOVirtual.DelayedCall(1, () =>
            {
                DOVirtual.Float(0, SaveData.GetSaveObject().saveingpot_ch, 1,
                    value => { ui.lab_pot.text = ((decimal)value).ToString("0.00"); });
            });
        }

    }
}
