
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System;

public static class Encryptor
{
    private static string hash = "@419$";
    public static PaddingMode paddingMode = PaddingMode.PKCS7;
    public static CipherMode cipherMode = CipherMode.ECB;

    private static string charPool = "AKFLDJAHSPIWUROCNMZX";

    public static string encrypt(string input)
    {
        byte[] data = UTF8Encoding.UTF8.GetBytes(input);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
            using (TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider() {  Key=key, Mode = cipherMode, Padding = paddingMode })
            {
                ICryptoTransform ict = tds.CreateEncryptor();
                byte[] results = ict.TransformFinalBlock(data, 0, data.Length);
                return Convert.ToBase64String(results, 0, results.Length);
            }
        }
    }

    public static string decrypt(string input)
    {
        byte[] data = Convert.FromBase64String(input);
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(hash));
            using (TripleDESCryptoServiceProvider tds = new TripleDESCryptoServiceProvider() { Key = key, Mode = cipherMode, Padding = paddingMode })
            {
                ICryptoTransform ict = tds.CreateDecryptor();
                byte[] results = ict.TransformFinalBlock(data, 0, data.Length);
                return UTF8Encoding.UTF8.GetString(results);
            }
        }
    }

    public static string intToString(int input){
        char[] inputBytes = input.ToString().ToCharArray();
        string output = "";
        foreach(char c in inputBytes){
            output += charPool[int.Parse(c.ToString())];
        }
        int luckyNumber = GetLuckyNumber(input);
        
        string lastLetter = charPool.ToCharArray()[luckyNumber].ToString();

        return output+lastLetter;
    }

    public static int stringToInt(string input){
        char[] inputBytes = input.Remove(input.Length-1).ToCharArray();
        string output = "";
        foreach(char c in inputBytes){
            for(int i=0; i < charPool.Length; i++){
                if(charPool.ToCharArray()[i] == c){
                    output += i.ToString();
                    break;
                }
            }
        }

        char lastchar = input.ToCharArray()[input.Length-1];

        int outputInt  = int.Parse(output);
        char luckyChar = charPool.ToCharArray()[GetLuckyNumber(outputInt)];
        bool validated = luckyChar == lastchar;
        

        if(validated){
            return outputInt;
        }else{
            return -1;
        }
    }


    public static int GetLuckyNumber(int input){
        int luckyNumber=input;
        while(luckyNumber >= 10){
            int val = 0;
            for(int i=0; i < luckyNumber.ToString().Length; i++){
                val += int.Parse(luckyNumber.ToString().ToCharArray()[i].ToString());
            }

            luckyNumber = val;
        }

        return luckyNumber;
    }

}
