using System;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using TCJPLYRM1xJTSDK;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public class TCJPLYRM1xJTSDKDemo : MonoBehaviour
{
    public void ShowReward()
    {
        TCJPLYRM1xJTSDKManager.Instance.ShowRewardVideo("TAG", b =>
        {
            Debug.LogError($"reward result = {b}");
        },(() =>
        {
            Debug.LogError($"reward close!!!");
        }));
    }

    public void ShowInter()
    {
        TCJPLYRM1xJTSDKManager.Instance.ShowInter("TAG", () =>
        {
            Debug.LogError("inter hide");
        });
    }

    public void ShowAdmobInter()
    {
        TCJPLYRM1xJTSDKManager.Instance.ShowAdmobInter("TAG", () =>
        {
            Debug.LogError("inter hide");
        });
    }

    public void ShowSplash()
    {
        TCJPLYRM1xJTSDKManager.Instance.ShowSplash();
    }

    public void CheckReward(Image btnImg)
    {
        var isReady = TCJPLYRM1xJTSDKManager.Instance.IsVideoReady();
        btnImg.color = isReady ? Color.green : Color.white;
        Debug.Log($"Reward : {isReady}");
    }

    public void CheckInter(Image btnImg)
    {
        var isReady = TCJPLYRM1xJTSDKManager.Instance.IsInterReady();
        btnImg.color = isReady ? Color.green : Color.white;
        Debug.Log($"Inter : {isReady}");
    }

    public void CheckSplash(Image btnImg)
    {
        var isReady = TCJPLYRM1xJTSDKManager.Instance.IsSplashReady();
        btnImg.color = isReady ? Color.green : Color.white;
        Debug.Log($"Splash : {isReady}");
    }

    public void Track()
    {
        TCJPLYRM1xJTSDKManager.Instance.Track("test", new Dictionary<string, string>()
        {
            {"evt1", "1"},
            {"evt2", "2"},
            {"evt3", "3"},
            {"evt4", "4"},
            {"evt5", "5"},
            {"evt6", "6"},
            {"evt7", "7"},
            {"evt8", "8"},
            {"evt9", "9"},
        });


    }

    public void GetCountryCode()
    {

        var code = TCJPLYRM1xJTSDKManager.Instance.GetCountryCode();
        Debug.Log($"country : {code}");
    }

    public void ShowH5()
    {
        TCJPLYRM1xJTSDKManager.Instance.H5.ShowH5((() =>
        {
            Debug.Log("H5 close");
        }), () =>
        {
            Debug.Log($"H5 show failed!");
        });
    }

    public void ShowH5WebView()
    {
        TCJPLYRM1xJTSDKManager.Instance.H5.ShowWebView((() =>
        {
            Debug.Log("H5 close");
        }));
    }

    public void ShowH5(RectTransform rectTransform)
    {
        TCJPLYRM1xJTSDKManager.Instance.H5.ShowH5(rectTransform);
    }

    public void HideH5()
    {
        TCJPLYRM1xJTSDKManager.Instance.H5.HideH5();
    }

    public void CheckShowH5(Image btnImg)
    {
        var show = TCJPLYRM1xJTSDKManager.Instance.H5.IsShowH5();
        btnImg.color = show ? Color.green : Color.red;
        Debug.Log($"CheckShowH5 : {show}");
    }

    private int _level = 1;
    public void TrackLevel()
    {
        TCJPLYRM1xJTSDKManager.Instance.TrackLevelUp(_level);
        if (Random.Range(0, 100) < 50)
        {
            _level++;
        }
    }

    private string withDrawSceneId = "";
    public void GetWithDrawConfigs()
    {
        TCJPLYRM1xJTSDKManager.Instance.GetWithDrawConfigs(((b, s) =>
        {
            Debug.Log($"GetWithDrawConfigs result : {b}, data : {s}");
            if (b)
            {
                var cfgs = JsonConvert.DeserializeObject<List<WithDrawConfig>>(s);
                int idx = 0;
                foreach (WithDrawConfig config in cfgs)
                {
                    Debug.Log($"index = {idx}, {config.ToString()}");
                    if (idx == 0)
                        withDrawSceneId = config.SecneId;
                    idx++;
                }
            }

        }));
    }

    private int payIndex = 0;
    public void CreateWithDrawOrder()
    {
        string taxNo = "99999999999";
        string payAccount = "tom@gmail.com";
        string accountType = "E";
        string payeeName = "testName";
        PaymentTypeCode payCode = PaymentTypeCode.PAYPAL;
        ////GOPAY DANA 收款账号需要为电话号码
        //if (payIndex == 1 || payIndex == 0)
        //{
        //    payCode = payIndex == 1 ? PaymentTypeCode.DANA : payCode;
        //    payAccount = "0881234567890";
        //    accountType = "P";
        //}
        //if (payIndex == 2)
        //{
        //    // PIX 需要填写税号
        //    payCode = PaymentTypeCode.PIX;
        //    taxNo = "99999999999";
        //}
        //if (payIndex == 3) payCode = PaymentTypeCode.MERCADOPAGO;
        TCJPLYRM1xJTSDKManager.Instance.CreateWithDrawOrder(withDrawSceneId, payCode, payAccount, accountType, payeeName, taxNo, b =>
        {
            Debug.Log($"CreateWithDrawOrder result : {b}");
        });

        payIndex++;
        payIndex = payIndex > 3 ? 0 : payIndex;
    }

    public void GetWithDrawOrders()
    {
        TCJPLYRM1xJTSDKManager.Instance.GetWithDrawOrders(((b, s) =>
        {
            Debug.Log($"GetWithDrawOrders result : {b}, data : {s}");
            if (b)
            {
                var cfgs = JsonConvert.DeserializeObject<List<WithDrawOrder>>(s);
                int idx = 0;
                foreach (WithDrawOrder config in cfgs)
                {
                    Debug.Log($"index = {idx}, {config.ToString()}");
                    idx++;
                }
            }
        }));
    }

    // Start is called before the first frame update
    void Start()
    {
        Init();

        Invoke("ShowSplash", 5);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void Init()
    {
        TCJPLYRM1xJTSDKManager.Instance.RegistIosParam((i =>
        {
            Debug.Log($"ios ab param : {i}");
        }));

        void GameConfig(bool result, string config)
        {
            Debug.Log($"************* game config result : {result}, config : {config}");
        }

        TCJPLYRM1xJTSDKManager.Instance.Init(null, "app_config", GameConfig);
    }

    public static string GetSdkVersion()
    {
        return TCJPLYRM1xJTSDKManager.SdkVersion;
    }
}
