using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using FGUI.G009_video;
using DG.Tweening;
using System;
using System.Linq;
using Roy.Datas;

namespace ScrewsMaster
{
    public class NewTaskUI : BaseUI
    {
        private NewTaskUICtrl ctrl;
        private NewTaskModel model;
        private com_newtask ui;
        List<PassingTask> passing_list = new List<PassingTask>();
        List<Durationtasks> Durationtasks_list = new List<Durationtasks>();
        List<Passportrewards> Passportrewards_list = new List<Passportrewards>();
        private Vector2 start_pos;
        private int reward_number;
        private int task_id = 0;
        private List<int> recored_list;
        private List<int> time_recored_list;
        private int reward_type = 0;

        private const float DefaultProgressWidth = 379;
        private float _defaultTopBgHeight;


        public NewTaskUI(NewTaskUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.NewTaskUI;
            this.ctrl = ctrl;
        }

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

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

        protected override void OnClose()
        {
            GameHelper.showGameUI = true;
            HallManager.Instance.UpdateSecondEvent -= upData;
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.G009_video.com_newtask;
        }

        protected override void OnOpenBefore(object args)
        {
            _defaultTopBgHeight = ui.TopBg.height;
            if (Screen.safeArea.y != 0)
            {//刘海屏
                ui.TopBox.y += Screen.safeArea.y;
                ui.TopBg.height += Screen.safeArea.y;
                ui.pass_com.TopBox.y += Screen.safeArea.y;
                ui.pass_com.pass_list.height -= Screen.safeArea.y;
            }

            if (GameHelper.getGameday() != DateTime.Now.Day)
            {
                PlayerPrefs.SetInt("_time", 0);
                SaveData.GetSaveobject().time_task_record = new List<int>();
                GameHelper.setGameday();
            }

            InitData();

            HallManager.Instance.UpdateSecondEvent += upData;
            InitView();
        }

        protected override void OnOpen(object args)
        {
        }

        protected override void OnHide()
        {
        }

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

        #region 消息
        protected override void AddListener()
        {
            GameDispatcher.Instance.AddListener(GameMsg.GetTaskReward, getRewardSuccess);
            //  GameDispatcher.Instance.AddListener(GameMsg.apple_pay_success, pay_success);
            GameDispatcher.Instance.AddListener(GameMsg.apple_pay_success, pay_success);
            GameDispatcher.Instance.AddListener(GameMsg.Gold_refresh, OnGoldRefresh);
        }
        protected override void RemoveListener()
        {
            GameDispatcher.Instance.RemoveListener(GameMsg.GetTaskReward, getRewardSuccess);
            // GameDispatcher.Instance.RemoveListener(GameMsg.apple_pay_success, pay_success);
            GameDispatcher.Instance.RemoveListener(GameMsg.apple_pay_success, pay_success);
            GameDispatcher.Instance.RemoveListener(GameMsg.Gold_refresh, OnGoldRefresh);
        }

        private void OnGoldRefresh(object args)
        {
            SetMakeup();
        }
        #endregion


        void pay_success(object str)
        {
            string type = (string)str;
            //2024年9月24日A面 包注释
            if (type == MaxPayManager.battle_pass)
            {
                OnPurchasePassSuccess();

            }
        }

        //初始化页面逻辑
        private void InitView()
        {
            ui.money.visible = GameHelper.IsGiftSwitch();

            ui.pass_com.visible = true;
            ui.task_list.visible = false;
            ui.MiddleBg.visible = false;
            ui.com_toptab.btn_battle.SetClick(() => { SwitchPage(0); });
            ui.com_toptab.btn_pass.SetClick(() => { SwitchPage(1); });
            ui.com_toptab.btn_time.SetClick(() => { SwitchPage(2); });
            ui.btn_close.SetClick(() => { UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.NewTaskUI_Close); });

            passing_list = ConfigSystem.GetConfig<PassingTaskModel>().dataList;
            Durationtasks_list = ConfigSystem.GetConfig<DurationtasksModel>().dataList;
            Passportrewards_list = ConfigSystem.GetConfig<PassportrewardsModel>().dataList;
            setPassCom();
            ui.com_toptab.red_0.visible = SaveData.battlepassred();
            ui.com_toptab.red_1.visible = SaveData.passtaskred();
            ui.com_toptab.red_2.visible = SaveData.timetaskred();
            ui.pass_com.btn_showtips.SetClick(() =>
            {
                ui.pass_com.t0.Stop();
                ui.pass_com.t0.Play();
            });
            SetMakeup();

            CheckState();
        }

        private void CheckState()
        {
            if (SaveData.battlepassred())
            {
                SwitchPage(0);
                return;
            }

            if (SaveData.passtaskred())
            {
                SwitchPage(1);
                return;
            }

            if (SaveData.timetaskred())
            {
                SwitchPage(2);
                return;
            }

            SwitchPage(0);

        }

        private void SwitchPage(int page)
        {
            ui.com_toptab.tab.selectedIndex = page;

            var height = _defaultTopBgHeight;
            switch (page)
            {
                case 0:
                    ui.pass_com.visible = true;
                    ui.MiddleBg.visible = false;
                    ui.task_list.visible = false;
                    height = _defaultTopBgHeight + Screen.safeArea.y;
                    break;
                case 1:
                    sortTaskList();
                    ui.pass_com.visible = false;
                    ui.MiddleBg.visible = true;
                    ui.task_list.visible = true;
                    break;
                case 2:
                    sortTimeList();
                    ui.pass_com.visible = false;
                    ui.MiddleBg.visible = true;
                    ui.task_list.visible = true;
                    break;
            }

            ui.TopBg.height = height;
        }

        void SetMakeup(object a = null)
        {
            ui.gold.GetChild("number_text").text = GameHelper.GetGoldNumber().ToString();
            ui.money.GetChild("number_text").text = GameHelper.Get102Str(GameHelper.Get102());

            DOVirtual.Float(0, (float)GameHelper.Get102(), 1,
                value => { ui.money.GetChild("number_text").text = GameHelper.Get102Str((decimal)value); });

            if (GameHelper.IsGiftSwitch())
            {

                var makeupTaskData = PreferencesMgr.Instance.MakeupTaskHistory.Last();
                var vo = ConfigSystem.GetConfig<MakeupModel>().GetData(makeupTaskData.tableId);

                if (vo == null)
                {
                    return;
                }
                var leftCash = (double)Math.Max(vo.item_need - GameHelper.Get102(), 0);

                (ui.money.GetChild("btn_cashout") as GButton).SetClickDownEffect(0.8f, 1);
                ui.money.GetChild("btn_cashout").SetClick(() =>
                 {
                     //      if (GameHelper.CanGuide() && !GuideCtrl.Instance.IsFinishGuide(1))
                     //      {
                     //          GuideCtrl.Instance.Dispatch("ClickButton");
                     // ui.com_box.finger_place.visible = false;
                     //  }
                     GameHelper.showGameUI = false;
                     uiCtrlDispatcher.Dispatch(UICtrlMsg.MakeupConfirmUI_Open, makeupTaskData);
                     //CtrlCloseUI();
                 });
            }
        }

        void upData()
        {
            ui.pass_com.remain_time.text = CommonHelper.TimeFormat(
                  SaveData.GetSaveobject().battle_pass_time + Passportrewards_list[0].Passportrewards_CD * 24 * 3600 - Convert.ToInt32(GameHelper.GetNowTime()),
                  CountDownType.Day);

            if ((SaveData.GetSaveobject().battle_pass_time + Passportrewards_list[0].Passportrewards_CD * 24 * 3600) < GameHelper.GetNowTime())
            {
                SaveData.GetSaveobject().battle_pass_time = (int)GameHelper.GetNowTime();
                SaveData.GetSaveobject().battle_pass_freelist.Clear();
                SaveData.GetSaveobject().battle_pass_paylist.Clear();
                SaveData.GetSaveobject().is_get_battlepass = false;
                GameHelper.ResetGameExp();
                SaveData.saveDataFunc();
                setPassCom();
            }

        }
        void setPassCom()
        {
            if ((SaveData.GetSaveobject().battle_pass_time + Passportrewards_list[0].Passportrewards_CD * 24 * 3600) < GameHelper.GetNowTime())
            {
                SaveData.GetSaveobject().battle_pass_time = (int)GameHelper.GetNowTime();
                GameHelper.ResetGameExp();
                SaveData.GetSaveobject().battle_pass_freelist.Clear();
                SaveData.GetSaveobject().battle_pass_paylist.Clear();
                SaveData.GetSaveobject().is_get_battlepass = false;
                SaveData.saveDataFunc();
            }
            upData();
            ui.pass_com.pass_list.itemRenderer = battle_itemRender;
            // ui.pass_com.pass_list.numItems = Passportrewards_list.Count + 1;

            ui.pass_com.pass_list.numItems = Passportrewards_list.Count;

            ui.pass_com.now_level.text = GameHelper.GetBattleLv().ToString();
            if (GameHelper.GetBattleLv() >= Passportrewards_list.Count)
            {
                ui.pass_com.now_exp.text = GameHelper.GetGameExp().ToString();
                ui.pass_com.progress.width = 328;
            }
            else
            {
                ui.pass_com.now_exp.text = GameHelper.GetGameExp() + "/" + Passportrewards_list[GameHelper.GetBattleLv()].Eliminating_quantity;
                ui.pass_com.progress.width = Mathf.Min((float)GameHelper.GetGameExp() / Passportrewards_list[GameHelper.GetBattleLv()].Eliminating_quantity, 1) * 328;
            }


                ui.pass_com.btn_pay.visible = !SaveData.GetSaveobject().is_get_battlepass;
                ui.pass_com.btn_pay.SetClick(OnClickBuyPass);


        }


        void setItemUrl(GLoader loader, int urltype)
        {
            if (urltype == 0) loader.url = "ui://G009_video/master_pass_jbd2";
            else if (urltype == 4) loader.url = "ui://G009_video/master_pass_jbd1";
            else if (urltype == 1) loader.url = "ui://G009_video/yxjm_bottom_add_hole";
            else if (urltype == 2) loader.url = "ui://G009_video/yxjm_bottom_merge_box";
            else if (urltype == 3) loader.url = "ui://G009_video/yxjm_bottom_clear";
        }
        void seItemClick(com_passreward item, int number, int type)
        {
            var start = GameHelper.GetUICenterPosition(item);
            var end = GameHelper.GetUICenterPosition(ui.gold.GetChild("number_text"));


            if (type == 0)
            {
                reward_data temp = new reward_data() { start = start, end = end, change = number, type = 101 };
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.CoinWnd_newUI_Open, temp);
                GameHelper.AddGoldNumber(number);
                GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                DOVirtual.DelayedCall(1, () =>
                {
                    DOVirtual.Float(0, (float)GameHelper.GetGoldNumber(), 1f,
                value => { ui.gold.GetChild("number_text").text = ((int)value).ToString(); });
                });
            }
            else
            {

                GameHelper.AddItemNumber((ItemEnum)type - 1, number);
                GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);

                // GameHelper.ShowTips("Received successfully");
            }

            ui.pass_com.pass_list.numItems = Passportrewards_list.Count;
            ui.com_toptab.red_0.visible = SaveData.battlepassred();
        }

        void battle_itemRender(int index, GObject obj)
        {
            index = index + 1;
            com_passitem item = (com_passitem)obj;

            item.index_text.text = index.ToString();
            item.free_item.type.selectedIndex = 0;
            item.pay_item.type.selectedIndex = 1;

            var passportReward = Passportrewards_list[index - 1];
            item.free_item.number_text.text = "x" + passportReward.Free_rewards_num;
            item.pay_item.number_text.text = "x" + passportReward.Paid_rewards_num;

            setItemUrl(item.free_item.item_loader,
                passportReward.Free_rewards_type == 0
                    ? 0
                    : passportReward.Paid_rewards_type);

            setItemUrl(item.pay_item.item_loader,
                passportReward.Paid_rewards_type == 0
                    ? 4
                    : passportReward.Paid_rewards_type);

            item.full.selectedIndex = com_passitem.Full_full;

            if (index == 1)
            {
                item.state.selectedIndex = 0;

                if (_passLevel > 0)
                {
                    item.progress_0_top.height = 135;

                    Debug.Log((((float)(GameHelper.GetGameExp() - Passportrewards_list[0].Eliminating_quantity) / ((Passportrewards_list[1].Eliminating_quantity - Passportrewards_list[0].Eliminating_quantity)))) * 2);
                    if (((float)(GameHelper.GetGameExp() - Passportrewards_list[0].Eliminating_quantity) / (Passportrewards_list[1].Eliminating_quantity - Passportrewards_list[0].Eliminating_quantity)) > 0.5)
                    {
                        item.progress_0_down.height = 175;
                    }
                    else
                    {

                        item.progress_0_down.height = (((float)(GameHelper.GetGameExp() - Passportrewards_list[0].Eliminating_quantity) / ((Passportrewards_list[1].Eliminating_quantity - Passportrewards_list[0].Eliminating_quantity)))) * 2 * 175;
                    }

                }
                else
                {
                    item.progress_0_top.height = (float)GameHelper.GetGameExp() / Passportrewards_list[0].Eliminating_quantity * 135;
                    item.progress_0_down.height = 0;

                }
            }
            else if (index == Passportrewards_list.Count)
            {
                item.state.selectedIndex = 2;


                if (((float)(GameHelper.GetGameExp() - Passportrewards_list[index - 2].Eliminating_quantity) / (passportReward.Eliminating_quantity - Passportrewards_list[index - 2].Eliminating_quantity)) > 0.5)
                {
                    float height = (((float)(GameHelper.GetGameExp() - Passportrewards_list[index - 2].Eliminating_quantity) / (passportReward.Eliminating_quantity - Passportrewards_list[index - 2].Eliminating_quantity)) - 0.5f) * 2 * 167;
                    if (height > 167) height = 167;
                    item.progress_2_top.height = height;
                }
                else item.progress_2_top.height = 0;

            }
            else
            {
                item.state.selectedIndex = 1;

                if (_passLevel >= index)
                {
                    item.progress_1_top.height = 167;
                }
                else
                {
                    if (_passLevel == (index - 1))
                    {

                        if (((float)(GameHelper.GetGameExp() - Passportrewards_list[index - 2].Eliminating_quantity) / (passportReward.Eliminating_quantity - Passportrewards_list[index - 2].Eliminating_quantity)) > 0.5)
                        {
                            float height = (((float)(GameHelper.GetGameExp() - Passportrewards_list[index - 2].Eliminating_quantity) / (passportReward.Eliminating_quantity - Passportrewards_list[index - 2].Eliminating_quantity)) - 0.5f) * 2 * 167;
                            if (height > 167) height = 167;
                            item.progress_1_top.height = height;
                        }
                        else item.progress_1_top.height = 0;


                    }
                    else
                    {
                        item.progress_1_top.height = 0;
                    }
                }

                if (_passLevel == index)
                {
                    Debug.Log("-----------------");
                    Debug.Log(((float)(GameHelper.GetGameExp() - passportReward.Eliminating_quantity) / (Passportrewards_list[index].Eliminating_quantity - passportReward.Eliminating_quantity)));
                    if (((float)(GameHelper.GetGameExp() - passportReward.Eliminating_quantity) / (Passportrewards_list[index].Eliminating_quantity - passportReward.Eliminating_quantity)) <= 0.5)
                    {
                        float height = (((float)(GameHelper.GetGameExp() - passportReward.Eliminating_quantity) / (Passportrewards_list[index].Eliminating_quantity - passportReward.Eliminating_quantity))) * 2 * 167;
                        if (height > 167) height = 167;
                        item.progress_1_down.height = height;
                    }
                    else
                    {
                        item.progress_1_down.height = 167;
                    }

                }
                else
                {
                    if (_passLevel > index) item.progress_1_down.height = 167;
                    else item.progress_1_down.height = 0;
                }
            }

            InitItemState(index, item);
            item.free_item.SetClick(() => { OnClickFullItemButton(index, item); });
            item.pay_item.SetClick(() => { OnClickPayItemButton(index, item); });
        }

        #region 新版代码

        private List<int> _freeList;
        private List<int> _payList;
        private int _passLevel;
        private void InitData()
        {
            _passLevel = GameHelper.GetBattleLv();
            _freeList = SaveData.GetSaveobject().battle_pass_freelist;
            _payList = SaveData.GetSaveobject().battle_pass_paylist;

            time_recored_list = SaveData.GetSaveobject().time_task_record;
            recored_list = SaveData.GetSaveobject().pass_task_record;
        }

        private void InitItemState(int index, com_passitem item)
        {
            int fullItemState;
            int payItemState;

           // item.Mask.visible = index > _passLevel;

            if (index <= _passLevel) //等级达到了
            {
                fullItemState = _freeList.Contains(index) ? 2 : 1;

                if (!SaveData.GetSaveobject().is_get_battlepass)//是否有通行证
                {
                    payItemState = 4;
                }
                else
                {
                    payItemState = _payList.Contains(index) ? 2 : 1;
                }
            }
            else
            {
                //等级还没到
                fullItemState = 3;
                payItemState = 3;
            }


            item.free_item.state.selectedIndex = fullItemState;
            item.pay_item.state.selectedIndex = payItemState;
        }

        private void OnClickFullItemButton(int index, com_passitem item)
        {
            if (index <= _passLevel)
            {
                if (!_freeList.Contains(index))
                {
                    _freeList.Add(index);
                    SaveData.saveDataFunc();
                    GameHelper.ShowTips("Successfully claimed");
                    seItemClick(item.free_item, Passportrewards_list[index - 1].Free_rewards_num, Passportrewards_list[index - 1].Free_rewards_type);
                }
            }
            else
            {
                GameHelper.ShowTips("Receiving level not reached");
            }
        }

        private void OnClickPayItemButton(int index, com_passitem item)
        {
            if (index <= _passLevel)
            {
                if (!SaveData.GetSaveobject().is_get_battlepass)
                {
                    if (GameHelper.IsGiftSwitch())
                    {
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PassunlockUI_Open);
                    }
                    return;
                }

                if (!_payList.Contains(index))
                {
                    _payList.Add(index);
                    SaveData.saveDataFunc();
                    GameHelper.ShowTips("Successfully claimed");
                    seItemClick(item.pay_item, Passportrewards_list[index - 1].Paid_rewards_num, Passportrewards_list[index - 1].Paid_rewards_type);
                }
            }
            else
            {
                GameHelper.ShowTips("Receiving level not reached");
            }
        }

        private void OnClickBuyPass()
        {

                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PassunlockUI_Open);

        }

        private void OnPurchasePassSuccess()
        {
            SaveData.GetSaveobject().is_get_battlepass = true;
            int gold = 0;
            int out_ = 0;
            int back_ = 0;
            int refresh_ = 0;

            for (int i = 0; i < Passportrewards_list.Count; i++)
            {
                if (_passLevel > i)
                {
                    switch (Passportrewards_list[i].Paid_rewards_type)
                    {
                        case 0:
                            gold += Passportrewards_list[i].Paid_rewards_num;
                            break;
                        case 1:
                            out_ += Passportrewards_list[i].Paid_rewards_num;
                            break;
                        case 2:
                            back_ += Passportrewards_list[i].Paid_rewards_num;
                            break;
                        case 3:
                            refresh_ += Passportrewards_list[i].Paid_rewards_num;
                            break;
                    }

                    SaveData.GetSaveobject().battle_pass_paylist.Add(i + 1);
                }
                else break;
            }

            if (gold > 0)
            {
                var start = GameHelper.GetUICenterPosition(ui.pass_com.btn_pay);
                var end = GameHelper.GetUICenterPosition(ui.gold.GetChild("number_text"));


                reward_data temp = new reward_data() { start = start, end = end, change = gold, type = 101 };
                UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.CoinWnd_newUI_Open, temp);
                GameHelper.AddGoldNumber(gold);
                GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                DOVirtual.DelayedCall(1, () =>
                {
                    DOVirtual.Float(0, (float)GameHelper.GetGoldNumber(), 1f,
                value => { ui.gold.GetChild("number_text").text = ((int)value).ToString(); });
                });

            }
            GameHelper.AddItemNumber(ItemEnum.AddHole, out_);
            GameHelper.AddItemNumber(ItemEnum.MergeBox, back_);
            GameHelper.AddItemNumber(ItemEnum.ClearHole, refresh_);
            GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
            ui.pass_com.pass_list.numItems = Passportrewards_list.Count;
            ui.pass_com.btn_pay.visible = false;
            SaveData.saveDataFunc();
        }
        #endregion


        void sortTaskList()
        {
            // recored_list = NewTaskModel.getTaskRecord();

            List<PassingTask> templist = new List<PassingTask>();
            passing_list.Sort((x, y) => x.id - y.id);
            for (int i = 0; i < passing_list.Count; i++)
            {
                if (recored_list.Contains(passing_list[i].id))
                {
                    passing_list[i].is_get = true;
                    templist.Add(passing_list[i]);
                    passing_list.Remove(passing_list[i]);
                    i--;
                }
            }
            passing_list.AddRange(templist);

            ui.task_list.itemRenderer = passingTaskrRender;
            ui.task_list.numItems = passing_list.Count;
            ui.com_toptab.red_1.visible = SaveData.passtaskred();
        }
        void sortTimeList()
        {
            //time_recored_list = NewTaskModel.getTimeRecord();
            List<Durationtasks> templist = new List<Durationtasks>();
            Durationtasks_list.Sort((x, y) => x.id - y.id);

            for (int i = 0; i < Durationtasks_list.Count; i++)
            {
                if (time_recored_list.Contains(Durationtasks_list[i].id))
                {
                    Durationtasks_list[i].is_get = true;
                    templist.Add(Durationtasks_list[i]);
                    Durationtasks_list.Remove(Durationtasks_list[i]);
                    i--;
                }
            }
            Durationtasks_list.AddRange(templist);

            ui.task_list.itemRenderer = durationRender;
            ui.task_list.numItems = Durationtasks_list.Count;
            ui.com_toptab.red_2.visible = SaveData.timetaskred();
        }

        void passingTaskrRender(int index, GObject obj)
        {
            task_item item = (task_item)obj;
            // item.content_text.text=string .Format(passing_list[index].text_read,passing_list[index].tol_num);

            item.content_text.text = passing_list[index].text_read;
            item.number_text.text = "x" + passing_list[index].reward_num;

            item.ez_img.visible = item.text_ez.visible = false;
            item.diff_img.visible = item.text_diff.visible = false;
            // if (passing_list[index].id % 3 == 0)
            // {
            //     item.ez_img.visible = item.text_ez.visible = false;
            //     item.diff_img.visible = item.text_diff.visible = true;
            // }
            // else
            // {
            //     item.ez_img.visible = item.text_ez.visible = true;
            //     item.diff_img.visible = item.text_diff.visible = false;
            // }
            if ((GameHelper.GetLevel() - 1) < passing_list[index].tol_num)
            {
                item.progress_text.text = (GameHelper.GetLevel() - 1) + "/" + passing_list[index].tol_num;
                item.progress.width = DefaultProgressWidth * Math.Min((float)(GameHelper.GetLevel() - 1) / passing_list[index].tol_num, 1);
                item.btn_get.state.selectedIndex = 1;
                item.btn_get.progress_text.text = "To complete";
                item.btn_get.SetClick(() =>
                {

                });
            }
            else
            {
                item.progress_text.text = passing_list[index].tol_num + "/" + passing_list[index].tol_num;
                item.progress.width = DefaultProgressWidth;

                item.btn_get.touchable = !passing_list[index].is_get;
                if (!passing_list[index].is_get)
                {
                    item.btn_get.state.selectedIndex = 0;
                    item.btn_get.SetClick(() =>
                    {
                        start_pos = GameHelper.GetUICenterPosition(item.gold_img);
                        reward_number = passing_list[index].reward_num;
                        task_id = passing_list[index].id;
                        reward_type = 1;
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GetTaskRewardUI_Open, passing_list[index].reward_num);
                    });
                    item.btn_get.progress_text.text = "Claim";
                }
                else
                {
                    item.btn_get.progress_text.text = "Claimed";
                    item.btn_get.state.selectedIndex = 1;
                }

            }
        }
        void getRewardSuccess(object a = null)
        {

            if ((bool)a) reward_number *= 2;
            var end = GameHelper.GetUICenterPosition(ui.gold.GetChild("number_text"));

            reward_data temp = new reward_data() { start = start_pos, end = end, change = reward_number, type = 101 };
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.CoinWnd_newUI_Open, temp);
            GameHelper.AddGoldNumber(reward_number);

            if (reward_type == 1)
            {
                recored_list.Add(task_id);
                sortTaskList();
            }
            else
            {
                time_recored_list.Add(task_id);
                sortTimeList();
            }

            SaveData.saveDataFunc();

            DOVirtual.DelayedCall(1, () =>
            {
                DOVirtual.Float(0, (float)GameHelper.GetGoldNumber(), 1f,
    value => { ui.gold.GetChild("number_text").text = ((int)value).ToString(); });
                GameHelper.AddInterAdNumber();

            });

            GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
        }

        void durationRender(int index, GObject obj)
        {
            task_item item = (task_item)obj;

            item.content_text.text = Durationtasks_list[index].text_read;
            item.number_text.text = "x" + Durationtasks_list[index].reward_num;

            item.ez_img.visible = item.text_ez.visible = false;
            item.diff_img.visible = item.text_diff.visible = false;

            if ((GameHelper.getGameTime() / 60) < Durationtasks_list[index].tol_num)
            {
                item.progress_text.text = Math.Round((float)GameHelper.getGameTime() / 60, 2) + "/" + Durationtasks_list[index].tol_num;
                item.progress.width = DefaultProgressWidth *
                                      Math.Min((float)GameHelper.getGameTime() / 60 / Durationtasks_list[index].tol_num,
                                          1);
                item.btn_get.state.selectedIndex = 1;
                item.btn_get.SetClick(() =>
                {

                });
                item.btn_get.progress_text.text = "To complete";
            }
            else
            {
                item.progress_text.text = Durationtasks_list[index].tol_num + "/" + Durationtasks_list[index].tol_num;
                item.progress.width = DefaultProgressWidth;
                item.btn_get.touchable = !Durationtasks_list[index].is_get;
                if (!Durationtasks_list[index].is_get)
                {
                    item.btn_get.state.selectedIndex = 0;
                    item.btn_get.SetClick(() =>
                    {
                        start_pos = GameHelper.GetUICenterPosition(item.gold_img);
                        reward_number = Durationtasks_list[index].reward_num;
                        task_id = Durationtasks_list[index].id;
                        reward_type = 2;
                        UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.GetTaskRewardUI_Open, Durationtasks_list[index].reward_num);
                    });
                    item.btn_get.progress_text.text = "Claim";
                }
                else
                {
                    item.btn_get.progress_text.text = "Claimed";
                    item.btn_get.state.selectedIndex = 1;
                }

            }
        }
    }
}