using SGModule.Common.Base;
using SGModule.Common.Helper;
using SGModule.Net;

namespace SGModule.NetKit {
    public class NetGmTool : SingletonMonoBehaviour<NetGmTool> {
        public void Init() {
            GMTool.Instance.AddItem(new GMToolItem(GUIType.Separator, () => "网络模块"));
            GMTool.Instance.AddItem(
                new GMToolItem(GUIType.Label, () => $"当前网络状态：{NetChecker.Instance.GetConnectionStatus()}"));
            GMTool.Instance.AddItem(new GMToolItem(GUIType.Label,
                () => $"loginModel uid：{(LoginKit.Instance.LoginModel == null ? "null" : LoginKit.Instance.LoginModel.Uid)}"));
            GMTool.Instance.AddItem(new GMToolItem(GUIType.Label,
                () =>
                    $"Token过期时间：{TimeHelper.ConvertToLocalTime(TokenManager.Instance.CachedExpiresAt)}"));

            GMTool.Instance.AddItem(new GMToolItem(GUIType.Label,
                () => {
                    var str = "";
                    foreach (var record in Net.TokenManager.Instance.GetTokenRecord()) {
                        str += $"Token: {record} " + "\n";
                    }

                    return str;
                })
            );
            GMTool.Instance.AddItem(new GMToolItem(GUIType.Button, () => "Token刷新测试",
                s => {
                    VerifyTokenExpiryImminent();
                }));
            GMTool.Instance.AddItem(new GMToolItem(GUIType.Button, () => "Token过期测试",
                s => {
                    VerifyTokenExpirationBehavior();
                }));
        }

        /// <summary>
        ///     Token即将过期（测试功能用）
        /// </summary>
        public void VerifyTokenExpiryImminent() {
            TokenManager.Instance.CachedExpiresAt -= 85400;
        }

        /// <summary>
        ///     token过期（测试功能用）
        /// </summary>
        public void VerifyTokenExpirationBehavior() {
            TokenManager.Instance.CachedExpiresAt -= 86400;
        }
    }
}
