using System;
using System.Collections;
using System.Collections.Generic;
using tysdk;
using UnityEngine;
using UnityEngine.UI;

public class UnbindTest : MonoBehaviour
{
    [SerializeField]
    private Text message;
    [SerializeField]
    private Button cleanBtn;

    [SerializeField]
    private Button initBtn;

    [SerializeField]
    private Dropdown accoutTypeDropdown;

    [SerializeField]
    private Button loginBtn;

    [SerializeField]
    private Button logoutBtn;

    [SerializeField]
    private Button getSnsBtn;

    [SerializeField]
    private Button checkSnsBtn;

    [SerializeField]
    private Button bindBtn;

    [SerializeField]
    private Button unbindBtn;

    private EAccoutType accoutType;

    private const string Cls = "com.unity3d.player.SnsTest";

    // Start is called before the first frame update
    void Start()
    {
        Screen.sleepTimeout = SleepTimeout.NeverSleep;
        message.text = string.Empty;
        accoutType = (EAccoutType)accoutTypeDropdown.value;

        cleanBtn.onClick.AddListener(() => message.text = string.Empty);
        initBtn.onClick.AddListener(Init);
        accoutTypeDropdown.onValueChanged.AddListener((index) =>
        {
            accoutType = (EAccoutType)index;
            message.text += $"[Account Type] {accoutType.ToString()}\r\n";
        }); loginBtn.onClick.AddListener(OnLogin);
        logoutBtn.onClick.AddListener(OnLogout);
        getSnsBtn.onClick.AddListener(OnGetSns);
        checkSnsBtn.onClick.AddListener(OnCheckSns);
        bindBtn.onClick.AddListener(OnBind);
        unbindBtn.onClick.AddListener(OnUnbind);
        Debug.Log("========== Init =============");
        TYSdkFacade.Instance.Init();
        Debug.Log($"AccoutType {accoutType}");
    }

    private void Init()
    {
        UnityBridgeFunc.UnityResetServerUrl("https://128-hwsfsdk-sdk-test01.qijihdhk.com");
    }

    private void OnLogout()
    {
        Debug.Log("========== Logout =============");
        TYSdkFacade.Instance.Logout();
    }

    private LoginInfo loginInfo;
    private async void OnLogin()
    {
        Debug.Log("========== Login =============");
        message.text += $"Login {accoutType}\r\n";
        var info = await TYSdkFacade.Instance.Login(accoutType);
        message.text = $"Login Success  {info.isSuccess}\r\n";
        if(info.isSuccess)
        {
          loginInfo = info;
        }
    }

    private void OnGetSns()
    {
        Debug.Log("========== Get SNS =============");
        using var t = new AndroidJavaClass(Cls);
        t.CallStatic("GetSns", accoutType.ToString());
    }

    private async void OnCheckSns()
    {
        try
        {
            Debug.Log("========== Login by sns =============");
            message.text += $"Login {accoutType}\r\n";
            var info = await TYSdkFacade.Instance.LoginBySns();
            message.text = $"sns Login Success  {info.isSuccess}\r\n";
            if(info.isSuccess)
            {
                loginInfo = info;
            }
        }
        catch (Exception e)
        {
            Debug.LogException(e);
        }
    }

    private async void OnBind()
    {
        try
        {
            Debug.Log("========== Bind =============");
            var linkResult = await TYSdkFacade.Instance.LinkAccount(accoutType);
            Debug.Log($"Bind Result Code:{linkResult.code}, UserId:{linkResult.userId}, bindInfo:{linkResult.bindInfo}");
            if(linkResult.code == -1)
            {
                var info = await PlayFabTool.GetPFPlayerInfoBySDKId(linkResult.userId, "6DC6D");
                Debug.Log(info?.ToString() ?? "[UnbindTest::OnBind] info is null");
            }
        }
        catch (Exception e)
        {
           Debug.LogException(e); 
        }
    }

    public void BindCallback(string message)
    {

    }

    private void OnUnbind()
    {
        using (var t = new AndroidJavaClass(Cls))
        {
          t.CallStatic("Unbind");
        }
    }

    public void UnbindCallback(string message)
    {

    }
}
