using System.Collections;
using System.Collections.Generic;
using SGModule.Common;
using SGModule.Common.Helper;
using SGModule.Net;
using UnityEngine.Events;

namespace SGModule.NetKit {
    public class NetKit : NetCore<NetKit> {
        public void Init() {
            Log.NetKit.Info("NetworkKit Init！！！！！");
        }

        protected override string Encrypt(string text, string key) {
            return ConfigManager.GameConfig.isRelease ? Cryptor.Encrypt(text, key) : text;
        }

        protected override string Decrypt(string text, string key) {
            return ConfigManager.GameConfig.isRelease ? Cryptor.Decrypt(text, key) : text;
        }

        protected override TokenManager.RefreshTokenDelegate RefreshTokenHandler() {
            return onCompleted => PostAsync<RequestTokenData>(
                "tokenRefresh",
                onCompleted: response => {
                    if (response.IsSuccess) {
                        var token = response.Data.Token;
                        var expiresAt = response.Data.ExpiresAt;
                        onCompleted?.Invoke(token, expiresAt);
                    }
                    else {
                        onCompleted?.Invoke(null, 0);
                    }
                });
        }

        protected override TokenManager.ReauthenticateDelegate ReauthenticateHandler() {
            return onCompleted => LoginKit.Instance.ReauthenticateOnExpiration(success => {
                if (success) {
                    var model = LoginKit.Instance.LoginModel;
                    onCompleted?.Invoke(model.Token, model.ExpiresAt);
                }
                else {
                    onCompleted?.Invoke(null, 0);
                }
            });
        }

        protected override NetCoreModel ProvideConfig() {
            var netConf = ConfigManager.NetworkConfig;
            return new NetCoreModel {
                RequestHost = ConfigManager.GameConfig.isRelease ? netConf.releaseHost : netConf.debugHost,
                ConnMode = netConf.connectionMode
            };
        }

        /// <summary>
        ///     Post请求(异步)
        /// </summary>
        /// <param name="path">请求路径</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="onCompleted">请求回调</param>
        /// <param name="withToken">是否带Token</param>
        /// <param name="headers">额外的请求头</param>
        public new IEnumerator PostAsync<T>(string path, object requestData = null, UnityAction<ResponseData<T>> onCompleted = null, bool withToken = true, Dictionary<string, string> headers = null) {
            return base.PostAsync(path, requestData, onCompleted, withToken, headers);
        }

        /// <summary>
        ///     Post请求
        /// </summary>
        /// <param name="path">请求路径</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="onCompleted">请求回调</param>
        /// <param name="withToken">是否带Token</param>
        /// <param name="useCoroutine">是否使用协程</param>
        /// <param name="headers">额外的请求头</param>
        public void Post(string path, object requestData = null, UnityAction<ResponseData<object>> onCompleted = null, bool withToken = true, bool useCoroutine = true, Dictionary<string, string> headers = null) {
            base.Post(path, requestData, onCompleted, withToken, useCoroutine, headers);
        }

        /// <summary>
        ///     Post请求(泛型)
        /// </summary>
        /// <param name="path">请求路径</param>
        /// <param name="requestData">请求参数</param>
        /// <param name="onCompleted">请求回调</param>
        /// <param name="withToken">是否带Token</param>
        /// <param name="useCoroutine">是否使用协程</param>
        /// <param name="headers">额外的请求头</param>
        /// <typeparam name="T">返回数据中Data的类型</typeparam>
        public new void Post<T>(string path, object requestData = null, UnityAction<ResponseData<T>> onCompleted = null, bool withToken = true, bool useCoroutine = true, Dictionary<string, string> headers = null) {
            base.Post(path, requestData, onCompleted, withToken, useCoroutine, headers);
        }
    }
}
