using System.Text;
using FutureCore;
using RedHotRoast;

public class Base64Kit
{
    public static string Encode(string data)
    {
        var key = NetworkManager.DomainRelease;
        var keyMD5 = MD5Kit.MD5String1(key);
        var str = Base64EncodeUtil.Base64Encode(data + keyMD5);

        var bytes = Encoding.UTF8.GetBytes(str);
        for (int i = 0, j = bytes.Length - 1; i < j; i += 1, j -= 1)
        {
            if (i % 2 == 0)
            {
                (bytes[i], bytes[j]) = (bytes[j], bytes[i]);
            }
        }

        var loginData = Encoding.UTF8.GetString(bytes);
        return loginData;
    }

    public static string Encode(string data, bool is_google_pay = false)
    {
        var key = NetworkManager.DomainRelease;
        if (is_google_pay) key = NetworkManager.identifier;

        var keyMD5 = MD5Kit.MD5String1(key);
        var str = Base64EncodeUtil.Base64Encode(data + keyMD5);

        var bytes = Encoding.UTF8.GetBytes(str);
        for (int i = 0, j = bytes.Length - 1; i < j; i += 1, j -= 1)
        {
            if (i % 2 == 0)
            {
                (bytes[i], bytes[j]) = (bytes[j], bytes[i]);
            }
        }

        var loginData = Encoding.UTF8.GetString(bytes);
        return loginData;
    }

    public static string Decode(string data)
    {
        var bytes = Encoding.UTF8.GetBytes(data);
        for (int i = 0, j = bytes.Length - 1; i < j; i += 1, j -= 1)
        {
            if (i % 2 == 0)
            {
                (bytes[i], bytes[j]) = (bytes[j], bytes[i]);
            }
        }

        var str = Encoding.UTF8.GetString(bytes);
        var str1 = Base64EncodeUtil.Base64Decode(str);
        var key = NetworkManager.DomainRelease;
        var keyMD5 = MD5Kit.MD5String1(key);
        var result = str1.Replace(keyMD5, string.Empty);
        return result;
    }

    public static string Decode(string data, string key)
    {
        var bytes = Encoding.UTF8.GetBytes(data);
        for (int i = 0, j = bytes.Length - 1; i < j; i += 1, j -= 1)
        {
            if (i % 2 == 0)
            {
                (bytes[i], bytes[j]) = (bytes[j], bytes[i]);
            }
        }

        var str = Encoding.UTF8.GetString(bytes);
        var str1 = Base64EncodeUtil.Base64Decode(str);
        // var key = NetworkManager.DomainRelease;
        var keyMD5 = MD5Kit.MD5String1(key);
        var result = str1.Replace(keyMD5, string.Empty);
        return result;
    }
}