using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IniParser;
using IniParser.Parser;
using IniParser.Model;

using System.Globalization;
using System.IO;
using FiestaBot.Utilities;

namespace FiestaBot.Configuration
{
    internal class Config
    {
        //Connection
        public string IP;
        public int Port;

        //Client
        public string CryptoFile;
        public ushort VersionYear;
        public ushort Version;
        public string Hash;

        //Account
        public string Token;

        //World
        public bool AutoSelectWorld;
        public byte AutoSelectWorldID;
        public bool AutoSelectCharacter;
        public byte AutoSelectCharacterSlot;

        //Zone
        public string SHNHashes;

        public Config(string ConfigFile)
        {
            if(!File.Exists(ConfigFile))
            {
                IniData NewData = new IniData();

                //Connection
                NewData["Connection"]["IP"] = "192.168.11.128";
                NewData["Connection"]["Port"] = "9010";

                //Client
                NewData["Client"]["CryptoFile"] = "Crypto.txt";
                NewData["Client"]["VersionYear"] = "2006";
                NewData["Client"]["Version"] = "6916";
                NewData["Client"]["Hash"] = "33B543B0CA6E7C41E5D1D0651307";

                //Account
                NewData["Account"]["Token"] = "y4z65SQFeK7KBWfkDqymqXq3qRB8ju4EKbyDFhPcRj2kr7sVdN";

                //World
                NewData["World"]["AutoSelectWorld"] = "False";
                NewData["World"]["AutoSelectWorldID"] = "0";
                NewData["World"]["AutoSelectCharacter"] = "False";
                NewData["World"]["AutoSelectCharacterSlot"] = "0";

                //Zone
                NewData["Zone"]["SHNHashes"] = "null";

                new FileIniDataParser().WriteFile("Config.ini", NewData);
            }

            FileIniDataParser p = new FileIniDataParser();
            IniData ConfigData = p.ReadFile(ConfigFile);

            //Connection
            IP = ConfigData["Connection"]["IP"];
            Port = Convert.ToInt32(ConfigData["Connection"]["Port"]);

            //Client
            CryptoFile = ConfigData["Client"]["CryptoFile"];
            VersionYear = Convert.ToUInt16(ConfigData["Client"]["VersionYear"]);
            Version = Convert.ToUInt16(ConfigData["Client"]["Version"]);
            Hash = ConfigData["Client"]["Hash"];
            foreach (var CryptoValue in File.ReadAllLines(string.Format("{0}\\{1}", AppDomain.CurrentDomain.BaseDirectory, CryptoFile))[0].Split(new string[] { ", " }, StringSplitOptions.None)) { Program.CryptoTable.Add(byte.Parse(CryptoValue, NumberStyles.HexNumber)); }

            //Account
            Token = ConfigData["Account"]["Token"];

            //World
            AutoSelectWorld = Convert.ToBoolean(ConfigData["World"]["AutoSelectWorld"]);
            AutoSelectWorldID = Convert.ToByte(ConfigData["World"]["AutoSelectWorldID"]);
            AutoSelectCharacter = Convert.ToBoolean(ConfigData["World"]["AutoSelectCharacter"]);
            AutoSelectCharacterSlot = Convert.ToByte(ConfigData["World"]["AutoSelectCharacterSlot"]);

            //Zone
            SHNHashes = ConfigData["Zone"]["SHNHashes"];

            Logger.Log(LogType.SHN, "Loaded config succesfully");
        }
    }
}
