using System;
using System.Collections.Generic;
using System.Linq;
using ChillConnect;
using IgnoreOPS;
using SGModule.ApplePay;
using SGModule.Net;
using SGModule.NetKit;
using UnityEngine;

public enum PayType
{
    buy_one,
    buy_one_off,
    buy_gold_1,
    buy_gold_2,
    buy_gold_3,
    buy_gold_4,
    buy_gold_5,
    remove_ad,
    battle_pass,
    pack_reward,
    fail_pack,
    three_days_gift
}

public class PurchasingManager
{
    private static readonly List<ApplePay> PayConfig = ConfigSystem.GetConfig<ApplePayModel>().DataList;
    // private static readonly List<ProductConfig> PayConfig2 = ConfigSystem.GetConfig<ApplePayModel2>().DataList;

    public static string GetPaySku(PayType key)
    {
        string keys = "";

        if (PayConfig != null)
        {
            foreach (var data in PayConfig)
            {
                if (data.payKey != key.ToString()) continue;
                keys = data.sku;
                break;
            }
        }

        return keys;
    }


    public static void Purchase(ApplePayData payData)
    {
        //本地测试
        // TestIOSPay(payData);
        // return;


        Debug.Log($"[Apple Pay] unity Purchase--0-----: {payData.sku}");
        // ApplePayManager.Instance.Purchase(payData.sku, ApplePaySuccessCallback(payData), message =>
        // {
        //     Debug.Log("purchase fail------- reason: " + message);
        // });
    }

    public static Action<ApplePayBackType, AppleResponseData> ApplePaySuccessCallback(ApplePayData payData)
    {
        return (backType, AppleResponseData) =>
        {
            Debug.Log($"[Apple Pay] unity Purchase--1-----: {backType.ToString()}");
            switch (backType)
            {
                case ApplePayBackType.Create:
                    Debug.Log("[Apple Pay] Create");
                    var sku0 = SetSku(payData);
                    Debug.Log($"[Apple Pay] Create---11: {sku0}");
                    SendEventClickByName(sku0, "click");
                    break;
                case ApplePayBackType.Check:
                    UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.PayloadingUI_Close);

                    // if (AppleResponseData != null && AppleResponseData.sku.Contains(".sub"))
                    // {
                    //     DataMgr.VipExpirationTime.Value =  Math.Max(DataMgr.VipExpirationTime.Value, AppleResponseData.expires_time);
                    //     var level = GetVipLvFormConfig(AppleResponseData.sku);
                    //     DataMgr.VipLevel.Value = level;
                    //     payData.sku = AppleResponseData.sku;
                    //     payData.shopName = "vip_club" + (level - 1);
                    // }

                    var sku = SetSku(payData);

                    Debug.Log($"[Apple Pay] Check sku===2===== {sku}");
                    GameDispatcher.Instance.Dispatch(GameMsg.apple_pay_success, sku);
                    Debug.Log($"[Apple Pay] Check sku===3===== {sku}");

                    SendEventClickByName(sku, "open");

                    SendEventClickByName(sku, "success");
                    break;
                case ApplePayBackType.Cancel:
                    Debug.Log("[Apple Pay] Cancel");
                    var sku1 = SetSku(payData);
                    SendEventClickByName(sku1, "open");
                    break;
            }
        };
    }
    private static string SetSku(ApplePayData data)
    {
        string sku = "";
        if (data.type != null && data.type == GetPaySku(PayType.remove_ad))
        {
            sku = data.type;
        }
        else if (data.sku.Contains("shop"))
        {
            sku = data.shopName;
        }
        else
        {
            sku = data.sku;
        }

        return sku;
    }


    private static void SendEventClickByName(string name, string type)
    {
        if (name == null)
        {
            return;
        }

        string clickTrackKey = null;
        string successTrackKey = null;
        string openTrackKey = null;

        int suffix = 1;
        if (name == GetPaySku(PayType.pack_reward))
        {
            clickTrackKey = Property.FirstPackClick;
            successTrackKey = Property.FirstPackSuccess;
            openTrackKey = Property.FirstPackShow;
        }
        else if (name == GetPaySku(PayType.remove_ad))
        {
            clickTrackKey = Property.RemoveAdClick;
            successTrackKey = Property.RemoveAdSuccess;
            openTrackKey = Property.RemoveAdShow;
        }
        else if (name == GetPaySku(PayType.battle_pass))
        {
            clickTrackKey = Property.PassClick;
            successTrackKey = Property.PassSuccess;
            openTrackKey = Property.PassShow;
        }
        else if (name == GetPaySku(PayType.buy_one))
        {
            clickTrackKey = Property.BuyOneClick;
            successTrackKey = Property.BuyOneSuccess;
            openTrackKey = Property.BuyOneShow;
        }
        else if (name == GetPaySku(PayType.buy_one_off))
        {
            clickTrackKey = Property.BuyOneOffClick;
            successTrackKey = Property.BuyOneOffSuccess;
            openTrackKey = Property.BuyOneOffShow;
        }
        else if (name == GetPaySku(PayType.fail_pack))
        {
            clickTrackKey = Property.FailGiftClick;
            successTrackKey = Property.FailGiftSuccess;
            openTrackKey = Property.FailGiftShow;
        }
        else if (name == GetPaySku(PayType.three_days_gift))
        {
            clickTrackKey = Property.ThreeDaysGiftClick;
            successTrackKey = Property.ThreeDaysGiftBuySuccess;
            openTrackKey = Property.ThreeDaysGiftShow;
        }

        if (name.StartsWith("buy_gold"))
        {
            int startIndex = "buy_gold".Length;
            suffix = Int32.Parse(name[startIndex..]); // 截取 "gold" 后的所有字符
            clickTrackKey = Property.GoldClick;
            successTrackKey = Property.GoldSuccess;
            openTrackKey = Property.GoldOpen;
        }

        if (type == "success" && successTrackKey != null)
        {
            TrackKit.SendEvent(Property.IOS_Pay_Event, successTrackKey, suffix);
        }
        else if (type == "click" && clickTrackKey != null)
        {
            TrackKit.SendEvent(Property.IOS_Pay_Event, clickTrackKey, suffix);
        }
        else if (type == "open" && openTrackKey != null)
        {
            TrackKit.SendEvent(Property.IOS_Pay_Event, openTrackKey, suffix);
        }
    }
}
