using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace RBXLegacyLauncher
{
	public class LauncherFuncs
	{
		public LauncherFuncs()
		{
		}
		
		public static void ReadConfigValues(string cfgpath)
		{
			string line1;
			string Decryptline1, Decryptline2, Decryptline3, Decryptline4, Decryptline5, Decryptline6, Decryptline7, Decryptline8, Decryptline9, Decryptline10, Decryptline11, Decryptline12, Decryptline13, Decryptline14, Decryptline15, Decryptline16, Decryptline17, Decryptline18, Decryptline19, Decryptline20, Decryptline21, Decryptline22, Decryptline23, Decryptline24, Decryptline25, Decryptline26, Decryptline27, Decryptline28, Decryptline29, Decryptline30, Decryptline31, Decryptline32, Decryptline33, Decryptline34, Decryptline35, Decryptline36;

			using(StreamReader reader = new StreamReader(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\config.txt")) 
			{
    			line1 = reader.ReadLine();
			}
			
			if (!SecurityFuncs.IsBase64String(line1))
				return;
			
			string ConvertedLine = SecurityFuncs.Base64Decode(line1);
			string[] result = ConvertedLine.Split('|');
			Decryptline1 = SecurityFuncs.Base64Decode(result[0]);
    		Decryptline2 = SecurityFuncs.Base64Decode(result[1]);
    		Decryptline3 = SecurityFuncs.Base64Decode(result[2]);
    		Decryptline4 = SecurityFuncs.Base64Decode(result[3]);
    		Decryptline5 = SecurityFuncs.Base64Decode(result[4]);
    		Decryptline6 = SecurityFuncs.Base64Decode(result[5]);
    		Decryptline7 = SecurityFuncs.Base64Decode(result[6]);
    		Decryptline8 = SecurityFuncs.Base64Decode(result[7]);
    		Decryptline9 = SecurityFuncs.Base64Decode(result[8]);
    		Decryptline10 = SecurityFuncs.Base64Decode(result[9]);
    		Decryptline11 = SecurityFuncs.Base64Decode(result[10]);
    		Decryptline12 = SecurityFuncs.Base64Decode(result[11]);
    		Decryptline13 = SecurityFuncs.Base64Decode(result[12]);
    		Decryptline14 = SecurityFuncs.Base64Decode(result[13]);
    		Decryptline15 = SecurityFuncs.Base64Decode(result[14]);
    		Decryptline16 = SecurityFuncs.Base64Decode(result[15]);
    		Decryptline17 = SecurityFuncs.Base64Decode(result[16]);
    		Decryptline18 = SecurityFuncs.Base64Decode(result[17]);
    		Decryptline19 = SecurityFuncs.Base64Decode(result[18]);
    		Decryptline20 = SecurityFuncs.Base64Decode(result[19]);
    		Decryptline21 = SecurityFuncs.Base64Decode(result[20]);
    		Decryptline22 = SecurityFuncs.Base64Decode(result[21]);
    		Decryptline23 = SecurityFuncs.Base64Decode(result[22]);
    		Decryptline24 = SecurityFuncs.Base64Decode(result[23]);
    		Decryptline25 = SecurityFuncs.Base64Decode(result[24]);
    		Decryptline26 = SecurityFuncs.Base64Decode(result[25]);
    		Decryptline27 = SecurityFuncs.Base64Decode(result[26]);
    		Decryptline28 = SecurityFuncs.Base64Decode(result[27]);
    		Decryptline29 = SecurityFuncs.Base64Decode(result[28]);
    		Decryptline30 = SecurityFuncs.Base64Decode(result[29]);
    		Decryptline31 = SecurityFuncs.Base64Decode(result[30]);
    		Decryptline32 = SecurityFuncs.Base64Decode(result[31]);
    		Decryptline33 = SecurityFuncs.Base64Decode(result[32]);
    		Decryptline34 = SecurityFuncs.Base64Decode(result[33]);
    		Decryptline35 = SecurityFuncs.Base64Decode(result[34]);
    		Decryptline36 = SecurityFuncs.Base64Decode(result[35]);
			
			bool bline1 = Convert.ToBoolean(Decryptline1);
			GlobalVars.CloseOnLaunch = bline1;
			
			int iline2 = Convert.ToInt32(Decryptline2);
			GlobalVars.UserID = iline2;
			
			GlobalVars.PlayerName = Decryptline3;
			
			GlobalVars.SelectedClient = Decryptline4;
			
			GlobalVars.Map = Decryptline5;
			
			int iline6 = Convert.ToInt32(Decryptline6);
			GlobalVars.ServerPort = iline6;
			
			GlobalVars.Custom_Hat1ID_Offline = Decryptline7;
			GlobalVars.Custom_Hat2ID_Offline = Decryptline8;
			GlobalVars.Custom_Hat3ID_Offline = Decryptline9;
			
			int iline10 = Convert.ToInt32(Decryptline10);
			GlobalVars.HeadColorID = iline10;
			
			int iline11 = Convert.ToInt32(Decryptline11);
			GlobalVars.TorsoColorID = iline11;
			
			int iline12 = Convert.ToInt32(Decryptline12);
			GlobalVars.LeftArmColorID = iline12;
			
			int iline13 = Convert.ToInt32(Decryptline13);
			GlobalVars.RightArmColorID = iline13;
			
			int iline14 = Convert.ToInt32(Decryptline14);
			GlobalVars.LeftLegColorID = iline14;
			
			int iline15 = Convert.ToInt32(Decryptline15);
			GlobalVars.RightLegColorID = iline15;
			
			GlobalVars.ColorMenu_HeadColor = Decryptline16;
			GlobalVars.ColorMenu_TorsoColor = Decryptline17;
			GlobalVars.ColorMenu_LeftArmColor = Decryptline18;
			GlobalVars.ColorMenu_RightArmColor = Decryptline19;
			GlobalVars.ColorMenu_LeftLegColor = Decryptline20;
			GlobalVars.ColorMenu_RightLegColor = Decryptline21;
			
			int iline22 = Convert.ToInt32(Decryptline22);
			GlobalVars.PlayerLimit = iline22;

			int iline23 = Convert.ToInt32(Decryptline23);
			GlobalVars.Custom_TShirt = iline23;
			int iline24 = Convert.ToInt32(Decryptline24);
			GlobalVars.Custom_Shirt = iline24;
			int iline25 = Convert.ToInt32(Decryptline25);
			GlobalVars.Custom_Pants = iline25;
			
			GlobalVars.Custom_IconType = Decryptline26;
			
			GlobalVars.FaceID = Decryptline27;
			GlobalVars.HeadID = Decryptline28;
			GlobalVars.TorsoID = Decryptline29;
			GlobalVars.LeftArmID = Decryptline30;
			GlobalVars.RightArmID = Decryptline31;
			GlobalVars.LeftLegID = Decryptline32;
			GlobalVars.RightLegID = Decryptline33;
			
			GlobalVars.Custom_Gear1 = Decryptline34;
			GlobalVars.Custom_Gear2 = Decryptline35;
			GlobalVars.Custom_Gear3 = Decryptline36;
		}
		
		public static void WriteConfigValues(string cfgpath)
		{
			string[] lines = { 
				SecurityFuncs.Base64Encode(GlobalVars.CloseOnLaunch.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.UserID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.PlayerName.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.SelectedClient.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Hat1ID_Offline.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Hat2ID_Offline.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Hat3ID_Offline.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.HeadColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.TorsoColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.LeftArmColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.RightArmColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.LeftLegColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.RightLegColorID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_HeadColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_TorsoColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_LeftArmColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_RightArmColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_LeftLegColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ColorMenu_RightLegColor.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_TShirt.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Shirt.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Pants.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_IconType.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.FaceID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.HeadID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.TorsoID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.LeftArmID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.RightArmID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.LeftLegID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.RightLegID.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Gear1.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Gear2.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.Custom_Gear3.ToString())
			};
			File.WriteAllText(cfgpath, SecurityFuncs.Base64Encode(string.Join("|",lines)));
		}
		
		public static void WriteServerPrefs(string cfgpath)
		{
			string[] lines = { 
				SecurityFuncs.Base64Encode(GlobalVars.Map.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ServerPort.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.DefaultRobloxPort.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.PlayerLimit.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.RespawnTime.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.IsPersonalServer.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.melee.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.navigation.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.social.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.powerup.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.explosives.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.transport.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ranged.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.musical.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.building.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.navigation.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.social.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.explosives.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.transport.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.ranged.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.musical.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.building.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist1.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist2.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist3.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist4.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist5.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist6.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist7.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.blacklist8.ToString()),
				SecurityFuncs.Base64Encode(GlobalVars.respawntime.ToString())
			};
			File.WriteAllText(cfgpath, SecurityFuncs.Base64Encode(string.Join("|",lines)));
		}
		
		public static void ResetConfigValues()
		{
			string line1;
			string Decryptline2;
			using(StreamReader reader = new StreamReader(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\info.txt"))
			{
    			line1 = reader.ReadLine();
			}
			
			if (!SecurityFuncs.IsBase64String(line1))
				return;
			string ConvertedLine = SecurityFuncs.Base64Decode(line1);
			string[] result = ConvertedLine.Split('|');
    		Decryptline2 = SecurityFuncs.Base64Decode(result[1]);
    		
			GlobalVars.CloseOnLaunch = false;
			GlobalVars.UserID = 0;
			GlobalVars.PlayerName = "Player";
    		GlobalVars.SelectedClient = Decryptline2;
			GlobalVars.ServerPort = 53640;
			GlobalVars.Custom_Hat1ID_Offline = "NoHat.rbxm";
			GlobalVars.Custom_Hat2ID_Offline = "NoHat.rbxm";
			GlobalVars.Custom_Hat3ID_Offline = "NoHat.rbxm";
			GlobalVars.HeadColorID = 24;
			GlobalVars.TorsoColorID = 23;
			GlobalVars.LeftArmColorID = 24;
			GlobalVars.RightArmColorID = 24;
			GlobalVars.LeftLegColorID = 119;
			GlobalVars.RightLegColorID = 119;
			GlobalVars.ColorMenu_HeadColor = "Color [A=255, R=245, G=205, B=47]";
			GlobalVars.ColorMenu_TorsoColor = "Color [A=255, R=13, G=105, B=172]";
			GlobalVars.ColorMenu_LeftArmColor = "Color [A=255, R=245, G=205, B=47]";
			GlobalVars.ColorMenu_RightArmColor = "Color [A=255, R=245, G=205, B=47]";
			GlobalVars.ColorMenu_LeftLegColor = "Color [A=255, R=164, G=189, B=71]";
			GlobalVars.ColorMenu_RightLegColor = "Color [A=255, R=164, G=189, B=71]";
			GlobalVars.Custom_TShirt = 0;
			GlobalVars.Custom_Shirt = 0;
			GlobalVars.Custom_Pants = 0;
			GlobalVars.Custom_IconType = "NBC";
		}
		
		public static void ResetServerPrefs()
		{
			GlobalVars.Map = "Baseplate.rbxl";
			GlobalVars.ServerPort = 53640;
			GlobalVars.DefaultRobloxPort = 53640;
			GlobalVars.PlayerLimit = 12;
			GlobalVars.RespawnTime = 5;
			GlobalVars.IsPersonalServer = false;
			GlobalVars.upnp = true;
			GlobalVars.melee = false;
			GlobalVars.navigation = false;
			GlobalVars.social = false;
			GlobalVars.powerup = false;
			GlobalVars.explosives = false;
			GlobalVars.transport = false;
			GlobalVars.ranged = false;
			GlobalVars.musical = false;
			GlobalVars.building = false;
			GlobalVars.blacklist1 = 0;
			GlobalVars.blacklist2 = 0;
			GlobalVars.blacklist3 = 0;
			GlobalVars.blacklist4 = 0;
			GlobalVars.blacklist5 = 0;
			GlobalVars.blacklist6 = 0;
			GlobalVars.blacklist7 = 0;
			GlobalVars.blacklist8 = 0;
			GlobalVars.respawntime = 5;
		}
		
		public static void ReadClientValues(string clientpath)
		{
			string line1;
			string Decryptline1, Decryptline2, Decryptline3, Decryptline4, Decryptline5, Decryptline6, Decryptline7, Decryptline8;

			using(StreamReader reader = new StreamReader(clientpath)) 
			{
    			line1 = reader.ReadLine();
			}
			
			if (!SecurityFuncs.IsBase64String(line1))
				return;
			
			string ConvertedLine = SecurityFuncs.Base64Decode(line1);
			string[] result = ConvertedLine.Split('|');
			Decryptline1 = SecurityFuncs.Base64Decode(result[0]);
    		Decryptline2 = SecurityFuncs.Base64Decode(result[1]);
    		Decryptline3 = SecurityFuncs.Base64Decode(result[2]);
    		Decryptline4 = SecurityFuncs.Base64Decode(result[3]);
    		Decryptline5 = SecurityFuncs.Base64Decode(result[4]);
    		Decryptline6 = SecurityFuncs.Base64Decode(result[5]);
    		Decryptline7 = SecurityFuncs.Base64Decode(result[6]);
    		Decryptline8 = SecurityFuncs.Base64Decode(result[7]);
			
			bool bline1 = Convert.ToBoolean(Decryptline1);
			GlobalVars.UsesPlayerName = bline1;
			
			bool bline2 = Convert.ToBoolean(Decryptline2);
			GlobalVars.UsesID = bline2;
			
			bool bline3 = Convert.ToBoolean(Decryptline3);
			GlobalVars.LoadsAssetsOnline = bline3;
			
			bool bline4 = Convert.ToBoolean(Decryptline4);
			GlobalVars.LegacyMode = bline4;
			
			bool bline5 = Convert.ToBoolean(Decryptline5);
			GlobalVars.HasRocky = bline5;
			
			GlobalVars.SelectedClientMD5 = Decryptline6;
			
			int iline7 = Convert.ToInt32(Decryptline7);
			GlobalVars.SelectedClientVersion = iline7;
			
			GlobalVars.SelectedClientDesc = Decryptline8;
			
			GlobalVars.MD5 = GlobalVars.SelectedClientMD5;
		}
		
		public static void ReadClientValuesBCC(string ClientName)
		{
			string clientpath = GlobalVars.ClientDir + @"\\" + ClientName + @"\\clientinfo.txt";
			
			if (!File.Exists(clientpath))
			{
				GlobalVars.SelectedClient = "2008";
			}
			
			ReadClientValues(clientpath);
		}
		
		public static void GeneratePlayerID()
		{
			CryptoRandom random = new CryptoRandom();
			int randomID = 0;
			int randIDmode = random.Next(0,7);
			if (randIDmode == 0)
			{
				randomID = random.Next(0, 99);
			}
			else if (randIDmode == 1)
			{
				randomID = random.Next(0, 999);
			}
			else if (randIDmode == 2)
			{
				randomID = random.Next(0, 9999);
			}
			else if (randIDmode == 3)
			{
				randomID = random.Next(0, 99999);
			}
			else if (randIDmode == 4)
			{
				randomID = random.Next(0, 999999);
			}
			else if (randIDmode == 5)
			{
				randomID = random.Next(0, 9999999);
			}
			else if (randIDmode == 6)
			{
				randomID = random.Next(0, 99999999);
			}
			else if (randIDmode == 7)
			{
				randomID = random.Next();
			}
			//2147483647 is max id.
			GlobalVars.UserID = randomID;
		}
	}
}
