

using System.Collections.Generic;
using AppsFlyerSDK;
using UnityEngine;

namespace ScrewsMaster
{
    public class SheepPlayUICtrl : BaseUICtrl
    {
        private SheepPlayUI ui;
        private SheepPlayModel model;

        private uint openUIMsg = UICtrlMsg.SheepPlayUI_Open;
        private uint closeUIMsg = UICtrlMsg.SheepPlayUI_Close;

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

        protected override void OnDispose()
        {
        }

        public override void OpenUI(object args = null)
        {
            if (ui == null)
            {
                ui = new SheepPlayUI(this);
                ui.Open(args);
            }
        }

        public override void CloseUI(object args = null)
        {
            if (ui != null && !ui.isClose)
            {
                ui.Close();
            }
            ui = null;
        }
        #endregion

        #region 消息
        public override uint GetOpenUIMsg(string uiName)
        {
            return openUIMsg;
        }
        public override uint GetCloseUIMsg(string uiName)
        {
            return closeUIMsg;
        }

        protected override void AddListener()
        {
            uiCtrlDispatcher.AddListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.AddListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.AddListener(GameMsg.Update101, OnUpdateDollar);
            GameDispatcher.Instance.AddListener(GameMsg.apple_pay_success, pay_success);
            AppDispatcher.Instance.AddListener(AppMsg.App_Focus_False, (a) =>
            {
                SaveData.saveDataFunc();
            });
        }
        protected override void RemoveListener()
        {
            uiCtrlDispatcher.RemoveListener(openUIMsg, OpenUI);
            uiCtrlDispatcher.RemoveListener(closeUIMsg, CloseUI);
            GameDispatcher.Instance.RemoveListener(GameMsg.Update101, OnUpdateDollar);
            GameDispatcher.Instance.RemoveListener(GameMsg.apple_pay_success, pay_success);
        }
        private bool CheckIsOpen(string name)
        {
            var isOpen = UIManager.Instance.IsExistUI(name);

            return isOpen;
        }
        void pay_success(object str)
        {
            string type = (string)str;
            string purch_number = "";
            Debug.Log("jiantinggggggggggggggggggggggg");
            if (type.Contains("buy_gold"))
            {
                if (type.StartsWith("buy_gold"))
                {
                    int startIndex = "buy_gold".Length;
                    string suffix = type[startIndex..]; // 截取 "gold" 后的所有字符
                    int suffix_num = int.Parse(suffix);
                    purch_number = ConfigSystem.GetConfig<PaidcoinsModel>().dataList[suffix_num].Payment_amount2.ToString();

                    if (!CheckIsOpen(UIConst.BuygoldUI))
                    {
                        switch (suffix_num)
                        {
                            case 0:
                                SaveData.GetSaveobject()._goldtime0 = (int)GameHelper.GetNowTime();
                                break;
                            case 1:
                                SaveData.GetSaveobject()._goldtime1 = (int)GameHelper.GetNowTime();
                                break;
                            case 2:
                                SaveData.GetSaveobject()._goldtime2 = (int)GameHelper.GetNowTime();
                                break;
                            case 3:
                                SaveData.GetSaveobject()._goldtime3 = (int)GameHelper.GetNowTime();
                                break;
                            case 4:
                                SaveData.GetSaveobject()._goldtime4 = (int)GameHelper.GetNowTime();
                                break;
                            case 5:
                                SaveData.GetSaveobject()._goldtime5 = (int)GameHelper.GetNowTime();
                                break;
                            case 6:
                                SaveData.GetSaveobject()._goldtime6 = (int)GameHelper.GetNowTime();
                                break;
                            default:
                                return;
                        }

                        SaveData.saveDataFunc();

                        var list = ConfigSystem.GetConfig<PaidcoinsModel>().dataList;
                        GameHelper.AddGoldNumber(list[suffix_num].Actual_coins);
                    }
                }

            }
            else if (type == MaxPayManager.remove_ad)
            {
                SaveData.GetSaveobject().is_get_removead = true;
                SaveData.saveDataFunc();
                purch_number = ConfigSystem.GetConfig<PaidgiftModel>().dataList[1].Paid_price2.ToString();
                ui?.SetBtnAds();

                if (!CheckIsOpen(UIConst.PackrewardUI))
                {
                    var list = ConfigSystem.GetConfig<PaidgiftModel>().dataList;
                    GameHelper.AddGoldNumber(list[1].coins_quantity);


                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.AdcomingUI_Close);

                    GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                    GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                    GameDispatcher.Instance.Dispatch(GameMsg.BuyRemoveAdPack);
                }
            }
            else if (type == MaxPayManager.pack_reward)
            {
                List<Paidgift> list = ConfigSystem.GetConfig<PaidgiftModel>().dataList;

                //  setBtnAds();
                purch_number = ConfigSystem.GetConfig<PaidgiftModel>().dataList[0].Paid_price2.ToString();
                ui?.SetBtnAds();

                SaveData.GetSaveobject().is_get_packreward = true;
                SaveData.saveDataFunc();
                if (!CheckIsOpen(UIConst.PackrewardUI))
                {
                    int gold_num = list[0].coins_quantity;
                    int back_num = list[0].props_quantity[1];
                    int out_num = list[0].props_quantity[0];
                    int refresh_num = list[0].props_quantity[2];
                    GameHelper.AddGoldNumber(gold_num);
                    GameHelper.AddItemNumber(ItemEnum.AddHole, out_num);
                    GameHelper.AddItemNumber(ItemEnum.MergeBox, back_num);
                    GameHelper.AddItemNumber(ItemEnum.ClearHole, refresh_num);

                    GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                    GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                    GameDispatcher.Instance.Dispatch(GameMsg.BuyPack);
                }
            }
            else if (type == MaxPayManager.battle_pass)
            {


                purch_number = ConfigSystem.GetConfig<CommonModel>().Passportgift2.ToString();

                if (!CheckIsOpen(UIConst.NewTaskUI))
                {
                    SaveData.GetSaveobject().is_get_battlepass = true;
                    int gold = 0;
                    int out_ = 0;
                    int back_ = 0;
                    int refresh_ = 0;
                    var Passportrewards_list = ConfigSystem.GetConfig<PassportrewardsModel>().dataList;
                    var _passLevel = GameHelper.GetBattleLv();
                    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)
                    {

                        GameHelper.AddGoldNumber(gold);
                        GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                    }
                    GameHelper.AddItemNumber(ItemEnum.AddHole, out_);
                    GameHelper.AddItemNumber(ItemEnum.MergeBox, back_);
                    GameHelper.AddItemNumber(ItemEnum.ClearHole, refresh_);
                    GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                    SaveData.saveDataFunc();
                }
            }
            else if (type == MaxPayManager.fail_pack)
            {
                List<Paidgift> list = ConfigSystem.GetConfig<PaidgiftModel>().dataList;
                purch_number = ConfigSystem.GetConfig<PaidgiftModel>().dataList[2].Paid_price2.ToString();

                if (!CheckIsOpen(UIConst.ResurgenceUI))
                {

                    int back_num = list[2].props_quantity[1];
                    int out_num = list[2].props_quantity[0];
                    int refresh_num = list[2].props_quantity[2];
                    // PreferencesMgr.Instance.Currency101 -= list[1].coins_quantity;

                    GameHelper.AddGoldNumber(list[2].coins_quantity);
                    GameHelper.AddItemNumber(ItemEnum.AddHole, out_num);
                    GameHelper.AddItemNumber(ItemEnum.MergeBox, back_num);
                    GameHelper.AddItemNumber(ItemEnum.ClearHole, refresh_num);
                    GameDispatcher.Instance.Dispatch(GameMsg.Sheep_item_refresh);
                    GameDispatcher.Instance.Dispatch(GameMsg.Gold_refresh);
                    GameObject.Find("GameStage").GetComponent<ShowScrews>().ClearHoleBtnClick();
                }

            }
            else if (type == MaxPayManager.three_days_gift)
            {
                SaveData.GetSaveobject().is_get_ThreeDaysGift = true;
                SaveData.saveDataFunc();
                purch_number = ConfigSystem.GetConfig<MultigiftModel>().dataList[0].Paid_price2.ToString();
            }
            else if (type == MaxPayManager.buy_one)
            {
                SaveData.GetSaveobject().is_get_ThreeDaysGift = true;
                SaveData.saveDataFunc();
                purch_number = GameHelper.GetCommonModel().addspace2.ToString();
                if (!CheckIsOpen(UIConst.PropUI))
                {
                    ShowScrews.Instance.OnSuccessPayBox1(0);
                }

            }
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);

            //付费收益上报AF，要排除广告兑换
            if (!GameHelper.IsAdModelOfPay() && !string.IsNullOrEmpty(purch_number) && decimal.TryParse(purch_number, out decimal revenue))
            {
                // Debug.Log("付费收益上报AF----------- " + revenue.ToString());
                adCallbackInfo.Clear();
                adCallbackInfo.Add("appsflyer_id", AppsFlyer.getAppsFlyerId());
                adCallbackInfo.Add("customer_user_id", GameHelper.GetLoginModel().uid.ToString());
                adCallbackInfo.Add("af_currency", "USD");
                adCallbackInfo.Add("af_revenue", revenue.ToString());
                AppsFlyer.sendEvent("af_purchase", adCallbackInfo);

                GameHelper.sendRevenueToServer("af_purchase", "af_revenue", (int)(revenue * 10000));
            }

        }
        public static Dictionary<string, string> adCallbackInfo = new Dictionary<string, string>();
        protected override void AddServerListener()
        {

        }
        protected override void RemoveServerListener()
        {

        }


        void OnUpdateDollar(object obj)
        {
            if (obj != null)
            {
                ui.SetMoneyCurr();
            }
        }
        #endregion
    }
}