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

using SHN;
using System.Data;
using FiestaBot.Utilities;

namespace FiestaBot.Configuration
{
    internal class ItemInfo
    {
        public ushort ItemID { get; set; }
        public string ItemInx { get; set; }
        public string ItemName { get; set; }
    }

    internal class MobInfo
    {
        public ushort MobID { get; set; }
        public string MobInx { get; set; }
        public string MobName { get; set; }
    }


    internal class MapInfo
    {
        public ushort MapID { get; set; }
        public string MapInx { get; set; }
        public string MapName { get; set; }
    }

    internal class HairInfo
    {
        public byte HairID { get; set; }
        public string HairName { get; set; }
    }

    internal class HairColorInfo
    {
        public byte HairColorID { get; set; }
        public string HairColorName { get; set; }
    }
    
    internal class FaceInfo
    {
        public byte FaceID { get; set; }
        public string FaceName { get; set; }
    }

    internal class ResSystem
    {
        public List<ItemInfo> ItemInfo = new List<ItemInfo>();
        public List<MobInfo> MobInfo = new List<MobInfo>();
        public List<MapInfo> MapInfo = new List<MapInfo>();
        public List<HairInfo> HairInfo = new List<HairInfo>();
        public List<HairColorInfo> HairColorInfo = new List<HairColorInfo>();
        public List<FaceInfo> FaceInfo = new List<FaceInfo>();

        public ResSystem()
        {
            try
            {
                //ItemInfo.shn
                SHNFile ItemFile = new SHNFile(".\\ressystem\\ItemInfo.shn");

                if (ItemFile.Type == SHNType.TextData) { ItemFile.SHNEncoding = Encoding.ASCII; }
                else { ItemFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                ItemFile.Read();

                foreach (DataRow Row in ItemFile.Table.Rows)
                {
                    ItemInfo NewItem = new ItemInfo()
                    {
                        ItemID = Convert.ToUInt16(Row.ItemArray[0]),
                        ItemInx = Convert.ToString(Row.ItemArray[1]),
                        ItemName = Convert.ToString(Row.ItemArray[2])
                    };
                    ItemInfo.Add(NewItem);
                }

                //MobInfo.shn
                SHNFile MobFile = new SHNFile(".\\ressystem\\MobInfo.shn");

                if (MobFile.Type == SHNType.TextData) { MobFile.SHNEncoding = Encoding.ASCII; }
                else { MobFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                MobFile.Read();

                foreach (DataRow Row in MobFile.Table.Rows)
                {
                    MobInfo NewMob = new MobInfo()
                    {
                        MobID = Convert.ToUInt16(Row.ItemArray[0]),
                        MobInx = Convert.ToString(Row.ItemArray[1]),
                        MobName = Convert.ToString(Row.ItemArray[2])
                    };
                    MobInfo.Add(NewMob);
                }

                //MapInfo.shn
                SHNFile MapFile = new SHNFile(".\\ressystem\\MapInfo.shn");

                if (MapFile.Type == SHNType.TextData) { MapFile.SHNEncoding = Encoding.ASCII; }
                else { MapFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                MapFile.Read();

                foreach (DataRow Row in MapFile.Table.Rows)
                {
                    MapInfo NewMap = new MapInfo()
                    {
                        MapID = Convert.ToUInt16(Row.ItemArray[0]),
                        MapInx = Convert.ToString(Row.ItemArray[1]),
                        MapName = Convert.ToString(Row.ItemArray[2])
                    };
                    MapInfo.Add(NewMap);
                }

                //HairInfo.shn
                SHNFile HairFile = new SHNFile(".\\ressystem\\HairInfo.shn");

                if (HairFile.Type == SHNType.TextData) { HairFile.SHNEncoding = Encoding.ASCII; }
                else { HairFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                HairFile.Read();

                foreach (DataRow Row in HairFile.Table.Rows)
                {
                    HairInfo NewHair = new HairInfo()
                    {
                        HairID = Convert.ToByte(Row.ItemArray[0]),
                        HairName = Convert.ToString(Row.ItemArray[2])
                    };
                    HairInfo.Add(NewHair);
                }

                //HairColorInfo.shn
                SHNFile HairColorFile = new SHNFile(".\\ressystem\\HairColorInfo.shn");

                if (HairColorFile.Type == SHNType.TextData) { HairColorFile.SHNEncoding = Encoding.ASCII; }
                else { HairColorFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                HairColorFile.Read();

                foreach (DataRow Row in HairColorFile.Table.Rows)
                {
                    HairColorInfo NewHairColor = new HairColorInfo()
                    {
                        HairColorID = Convert.ToByte(Row.ItemArray[0]),
                        HairColorName = Convert.ToString(Row.ItemArray[2])
                    };

                    HairColorInfo.Add(NewHairColor);
                }

                //FaceInfo.shn
                SHNFile FaceFile = new SHNFile(".\\ressystem\\FaceInfo.shn");

                if (FaceFile.Type == SHNType.TextData) { FaceFile.SHNEncoding = Encoding.ASCII; }
                else { FaceFile.SHNEncoding = Encoding.GetEncoding("ISO-8859-1"); }

                FaceFile.Read();

                foreach (DataRow Row in FaceFile.Table.Rows)
                {
                    FaceInfo NewFace = new FaceInfo()
                    {
                        FaceID = Convert.ToByte(Row.ItemArray[0]),
                        FaceName = Convert.ToString(Row.ItemArray[1])
                    };

                    FaceInfo.Add(NewFace);
                }

                Logger.Log(LogType.SHN, "Loaded ResSystem succesfully:\n" +
                                  "ItemInfo.shn      | {0} values\n" +
                                  "MobInfo.shn       | {1} values\n" +
                                  "MapInfo.shn       | {2} values\n" +
                                  "HairInfo.shn      | {3} values\n" +
                                  "HairColorInfo.shn | {4} values\n" +
                                  "FaceInfo.shn      | {5} values", ItemInfo.Count, MobInfo.Count, MapInfo.Count, HairInfo.Count, HairColorInfo.Count, FaceInfo.Count);
            }
            catch(Exception ex) { Logger.Log(LogType.Error, ex.Message); }
        }
    }
}
