using System.Collections.Generic;
using Newtonsoft.Json;
using UnityEngine;


[System.Serializable]
public class InitResult
{
    public string channel;
    public int gameId;
    public int channelId;
}

[System.Serializable]
public class LoginResult
{
    public long uid;
    public string accessToken;
    public string channel;
    public string channelUid;   // 已弃用,默认为0 
    public string serverArea;
    public string extendInfo;   // 扩展参数转 JSON 字符串再传过来
}

[System.Serializable]
public class GoodsInfo
{
    public string currency;
    public string itemId;
    public string itemName;
    public string itemCount;
    public string itemPrice;
    public string showTag;
    public string region;//仅 iOS返回
    public string symbol;//仅 iOS返回
}

public class PlatformCallback : MonoBehaviour
{
    private static IPlatformCallbackListener _listener;
    // 单例实现，确保只有一个接收器实例
    public static PlatformCallback Instance { get; private set; }

    void Awake()
    {
        // 单例模式实现
        if (Instance != null && Instance != this)
        {
            NativeLogger.I("fastsdk_unity_result", "[        ] Destroying duplicate instance");
            Destroy(gameObject);
            return;
        }

        Instance = this;

        // 确保接收器在场景切换中不被销毁
        DontDestroyOnLoad(gameObject);

        // 确保游戏对象名称与 Android 调用匹配
        gameObject.name = "PlatformCallback";

        NativeLogger.I("fastsdk_unity_result", "[PlatformCallback] Initialized with GameObject name: " + gameObject.name);
    }

    public static void SetListener(IPlatformCallbackListener listener)
    {
        _listener = listener;
    }

    public void onInitSuccess(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", "[AndroidBrigsCall] onInitSuccess called with result: " + jsonStr);
        InitResult result = JsonConvert.DeserializeObject<InitResult>(jsonStr);
        if (result != null)
        {
            _listener?.OnInitSuccess(result);
        }
    }

    public void onInitFailed(string reason)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onInitFailed called with reason: " + reason);
        _listener.OnInitFailed(reason);
    }

    //自定义退出界面
    public void onCustomExit()
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onCustomExit called");
        _listener?.OnCustomExit();
    }

    //预留
    public void onUpdate(string data)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onUpdate called with data: " + data);
        _listener?.OnUpdate(data);
    }

    //登录成功
    public void onLoginSuccess(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onLoginSuccess called with result: " + jsonStr);


        //判断 null 或空字符串判断空白字符串（包括空格、制表符等）
        if (string.IsNullOrEmpty(jsonStr) || string.IsNullOrWhiteSpace(jsonStr))
        {
            NativeLogger.I("fastsdk_unity_result", "[Unity] Android returned empty or invalid string");
            return;
        }
        LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(jsonStr);

        if (loginResult.uid != 0)
        {
            _listener?.OnLoginSuccess(loginResult);
        }

    }

    public void onRefreshUser(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onRefreshUser called with result: " + jsonStr);

        //判断 null 或空字符串判断空白字符串（包括空格、制表符等）
        if (string.IsNullOrEmpty(jsonStr) || string.IsNullOrWhiteSpace(jsonStr))
        {
            NativeLogger.I("fastsdk_unity_result", "[Unity] Android returned empty or invalid string");
            return;
        }
        LoginResult loginResult = JsonConvert.DeserializeObject<LoginResult>(jsonStr);

        if (loginResult.uid != 0)
        {
            _listener?.OnRefreshUser(loginResult);
        }
    }

    public void onLoginFailed(string reason)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onLoginFailed called with reason: " + reason);
        _listener?.OnLoginFailed(reason);
    }

    public void onLogout(string message)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onLogout called with message: " + message);
        _listener?.OnLogout(message);
    }

    public void onPayGoodsList(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onPayGoodsList called with message: " + jsonStr);
        List<GoodsInfo> goodsLists = JsonConvert.DeserializeObject<List<GoodsInfo>>(jsonStr);
        NativeLogger.I("fastsdk_unity_result", "[Unity] onPayGoodsList deserialized, count: " + (goodsLists?.Count ?? 0) + ", listener is null: " + (_listener == null));
        if (_listener == null)
        {

            NativeLogger.I("fastsdk_unity_result", "[Unity] onPayGoodsList called_listener is null");
        }
        _listener?.OnPayGoodsList(goodsLists);
        //if (goodsLists != null && goodsLists.Count!=0)
        //{
        //    _listener?.OnPayGoodsList(goodsLists);
        //}
    }

    public void onPaySuccess(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", $"[Unity] onPaySuccess called {jsonStr}");
        _listener?.OnPaySuccess(jsonStr);
    }

    public void onPayFailed(string reason)
    {
        NativeLogger.I("fastsdk_unity_result", "[Unity] onPayFailed called with reason: " + reason);
        _listener?.OnPayFailed(reason);
    }


    //分享，无特殊处理可不实现
    public void onShareSuccess(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", $"[Unity] onShareSuccess called {jsonStr}");
        _listener?.OnShareSuccess(jsonStr);
    }

    public void onShareFailed(string jsonStr)
    {
        NativeLogger.I("fastsdk_unity_result", $"[Unity] onShareFailed called {jsonStr}");
        _listener?.OnShareFailed(jsonStr);
    }

}
