using System.Collections.Generic;
using System.Text;
using BingoBrain.Core;
using BingoBrain.HotFix;
using FGUI.ACommon;
using FGUI.JMall;
using Newtonsoft.Json;
using UnityEngine;

namespace BingoBrain
{
    public class ExchangeUI : BaseUI
    {
        private ExchangeUICtrl ctrl;
        private ExchangeModel model;
        private FGUI.JMall.com_exchange ui;

        public ExchangeUI(ExchangeUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.ExchangeUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "JMall";
            uiInfo.assetName = "com_exchange";
            uiInfo.layerType = UILayerType.Normal;
            uiInfo.isNeedOpenAnim = false;
            uiInfo.isNeedCloseAnim = false;
            uiInfo.isNeedUIMask = true;
        }

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

        protected override void OnClose()
        {
        }

        protected override void OnBind()
        {
            ui = baseUI as FGUI.JMall.com_exchange;
        }

        protected override void OnOpenBefore(object args)
        {
            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 int card_buy_num = 1;
        private int amazon_buy_num = 1;

        //初始化页面逻辑
        private void InitView()
        {
            Debug.Log(GameHelper.GetCommonModel().ExchangeCard);
            Debug.Log(GameHelper.GetCommonModel().ExchangeAmazon);
            ui.text_amazon_ch.text = (GameHelper.isRDExchangeMode() ? "" : "$") + GameHelper.GetCommonModel().ExchangeAmazon[0].ToString();
            SetGold();
            ui.btn_addcard.SetClick(() =>
            {
                if (card_buy_num >= 10) return;
                card_buy_num++;
                SetGold();
            });
            ui.btn_subcard.SetClick(() =>
            {
                if (card_buy_num <= 1) return;
                card_buy_num--;
                SetGold();
            });

            ui.btn_addamazon.SetClick(() =>
            {
                if (amazon_buy_num >= 10) return;
                amazon_buy_num++;
                SetGold();
            });
            ui.btn_subamazon.SetClick(() =>
            {
                if (amazon_buy_num <= 1) return;
                amazon_buy_num--;
                SetGold();
            });

            ui.btn_exchange_card.SetClick(() =>
            {
                if (card_buy_num * GameHelper.GetCommonModel().ExchangeCard < PreferencesMgr.Instance.Currency101)
                {
                    PreferencesMgr.Instance.CardBoardSum = PreferencesMgr.Instance.CardBoardSum + card_buy_num;
                    PreferencesMgr.Instance.Currency101 = PreferencesMgr.Instance.Currency101 - card_buy_num * GameHelper.GetCommonModel().ExchangeCard;
                    GameHelper.ShowTips("Exchange successful");

                }
                else
                {
                    GameHelper.ShowTips("There aren't enough gold");
                }
            });

            ui.btn_exchange_amazon.SetClick(() =>
            {
                if (amazon_buy_num * GameHelper.GetCommonModel().ExchangeAmazon[1] < PreferencesMgr.Instance.Currency101)
                {
                    SaveingPotHelper.TestingClearTime();
                    SaveData.GetSaveobject().saveingpot_ch = SaveData.GetSaveobject().saveingpot_ch + amazon_buy_num * GameHelper.GetCommonModel().ExchangeAmazon[0];
                    PreferencesMgr.Instance.Currency101 = PreferencesMgr.Instance.Currency101 - amazon_buy_num * GameHelper.GetCommonModel().ExchangeAmazon[1];
                    GameHelper.ShowTips("Exchange successful");
                }
                else
                {
                    GameHelper.ShowTips("There aren't enough gold");
                }
            });
            SetMakeup();
        }

        private void SetGold()
        {
            ui.text_card_num.text = card_buy_num.ToString();
            ui.text_amazon_num.text = amazon_buy_num.ToString();
            ui.btn_exchange_card.title = (card_buy_num * GameHelper.GetCommonModel().ExchangeCard).ToString();
            ui.btn_exchange_amazon.title = (amazon_buy_num * GameHelper.GetCommonModel().ExchangeAmazon[1]).ToString();
        }

        private void SetMakeup()
        {

            List<com_makeup> btn_list = new List<com_makeup>() { ui.com_makeup0, ui.com_makeup1, ui.com_makeup2 };

            List<makeup> makeup_list = ConfigSystem.GetConfig<MakeupModel>().dataList;
            Debug.Log(JsonConvert.SerializeObject(PreferencesMgr.Instance.MakeupTaskHistory));
            for (int i = 0; i < btn_list.Count; i++)
            {
                MakeupTaskData makeupTaskData = null;
                for (int j = PreferencesMgr.Instance.MakeupTaskHistory.Count - 1; j >= 0; j--)
                {
                    if (PreferencesMgr.Instance.MakeupTaskHistory[j].tableId == (i + 1))
                    {
                        makeupTaskData = PreferencesMgr.Instance.MakeupTaskHistory[j];
                        // now_selectindex = index;
                        break;
                    }
                }
                if (makeupTaskData == null)
                {
                    makeup makeupVo = makeup_list[i];

                    makeupTaskData = new MakeupTaskData();
                    makeupTaskData.tableId = i + 1;
                    makeupTaskData.id = PreferencesMgr.Instance.MakeupTaskHistory.Count + 1;
                    makeupTaskData.amountStr = $"{makeupVo.item_need}";
                    makeupTaskData.amount = makeupVo.item_need;
                    makeupTaskData.orderID = GetRandomNum(8);
                    makeupTaskData.SetStatus(MakeupTaskStatus.None);
                    PreferencesMgr.Instance.MakeupTaskHistory.Add(makeupTaskData);
                    PreferencesMgr.Instance.SaveMakeupTaskHistory();

                }

                btn_list[i].btn_makeup.SetClick(() =>
                {
                    UICtrlDispatcher.Instance.Dispatch(SkinInfo.MakeupConfirmUI_Open, makeupTaskData);
                });
                btn_list[i].text_makeup.text = (GameHelper.isRDExchangeMode() ? "<img src='ui://pmf3wbji112epi7'/>" : "") +
                 GameHelper.Get102Str_new((decimal)makeupTaskData.amount);
                if (makeupTaskData.status > MakeupTaskStatus.None)
                {
                    btn_list[i].text_progress.text = $"{makeupTaskData.amount:N}" + "/" + $"{makeupTaskData.amount:N}";
                    btn_list[i].progress_makeup.value = 100;
                }
                else
                {
                    btn_list[i].text_progress.text = $"{PreferencesMgr.Instance.Currency102:N}" + "/" + $"{makeupTaskData.amount:N}";
                    btn_list[i].progress_makeup.value = (double)(PreferencesMgr.Instance.Currency102 / makeupTaskData.amount) * 100;
                }



            }
            //  List<level_btn> btn_list = new List<level_btn>() { ui.com_chlevel.btn_0, ui.com_chlevel.btn_1, ui.com_chlevel.btn_2 };

            //             List<makeup> makeup_list = ConfigSystem.GetConfig<MakeupModel>().dataList;
            //             now_selectindex = makeupTaskData.tableId - 1;
            //             for (int i = 0; i < btn_list.Count; i++)
            //             {
            //                 btn_list[i].text_level0.text = "Level " + makeup_list[i].levels_need;
            //                 btn_list[i].text_level1.text = "Level " + makeup_list[i].levels_need;
            //                 btn_list[i].text_level2.text = "Level " + makeup_list[i].levels_need;
            //                 if (i < ch_index)
            //                 {
            //                     btn_list[i].img_check.visible = true;
            //                     // btn_list[i].text_level0.y = 4;
            //                     //  btn_list[i].text_level1.y = 4;

            //                 }
            //                 else
            //                 {
            //                     btn_list[i].img_check.visible = false;
            //                     //  btn_list[i].text_level0.y = 38;
            //                     //  btn_list[i].text_level1.y = 38;
            //                 }

            //                 if (i == now_selectindex)
            //                 {
            //                     btn_list[i].btn_state.selectedIndex = 1;
            //                 }
            //                 else if (i > ch_index)
            //                 {
            //                     btn_list[i].btn_state.selectedIndex = 2;
            //                     int _temp_index = i;
            //                     btn_list[i].SetClick(() =>
            //                     {
            //                         GameHelper.ShowTips("Complete level " + makeup_list[_temp_index].levels_need + " to unlock.");
            //                     });
            //                 }
            //                 else
            //                 {
            //                     btn_list[i].btn_state.selectedIndex = 0;
            //                     int index = i;
            //                     btn_list[i].SetClick(() =>
            //                     {
            //                         for (int i = PreferencesMgr.Instance.MakeupTaskHistory.Count - 1; i >= 0; i--)
            //                         {
            //                             if (PreferencesMgr.Instance.MakeupTaskHistory[i].tableId == index + 1)
            //                             {
            //                                 makeupTaskData = PreferencesMgr.Instance.MakeupTaskHistory[i];
            //                                 now_selectindex = index;
            //                                 break;
            //                             }
            //                         }

            //                         InitView();
            //                     });
            //                 }
            //             }


        }
        public static string GetRandomNum(int count)
        {
            var resultStr = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                resultStr.Append(UnityEngine.Random.Range(0, 10));
            }

            return resultStr.ToString();
        }


    }
}