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

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

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

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

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

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

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

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

    public void Track()
    {
        MYp0ZVTT2QSDKHelper.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 = MYp0ZVTT2QSDKHelper.Instance.GetCountryCode();
        Debug.Log($"country : {code}");
    }

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

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

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

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

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

    private string withDrawSceneId = "";
    public void GetWithDrawConfigs()
    {
        MYp0ZVTT2QSDKHelper.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 = "";
        string payAccount = "tom@gmail.com";
        string accountType = "E";
        PaymentTypeCode payCode = PaymentTypeCode.GOPAY;
        //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;
        MYp0ZVTT2QSDKHelper.Instance.CreateWithDrawOrder(withDrawSceneId, payCode, payAccount, accountType, "testName", taxNo, b =>
        {
            Debug.Log($"CreateWithDrawOrder result : {b}");
        });

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

    public void GetWithDrawOrders()
    {
        MYp0ZVTT2QSDKHelper.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()
    {
        MYp0ZVTT2QSDKHelper.Instance.RegistIosParam((i =>
        {
            Debug.Log($"ios ab param : {i}");
        }));

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

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

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