using System;
using System.Data;
using System.Text;
using System.Reflection;
using System.Collections.Generic;

using CS;
using SHN;

namespace FilterAPI.CustomCurrency
{
    public class CurrencyLoader
    {
        public List<Currency> Currencies = new List<Currency>();
        public List<CurrencyItem> CurrencyItemIDs = new List<CurrencyItem>();

        public CurrencyLoader(Authentication Auth)
        {
            MethodInfo CryptoMethod = CSFile.LoadCode(Auth.ConfigValues["SHNCryptoFile"], "SHN.SHNCrypto", Auth.ConfigValues["SHNCryptoName"]);

            SHNFile Currencys = new SHNFile(String.Format("{0}Currencys.shn", Auth.ConfigValues["FilterSHNPath"]), CryptoMethod);

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

            if (Currencys.Type != SHNType.QuestData) { Currencys.Read(); }
            else { Currencys.ReadQuest(); }

            Currencys.DisallowRowChanges();

            foreach (DataRow Row in Currencys.Table.Rows)
            {
                Currency NewCurrency = new Currency()
                {
                    CurrencyID = Convert.ToByte(Row.ItemArray[0]),
                    CurrencyName = Convert.ToString(Row.ItemArray[1]),
                    TableName = Convert.ToString(Row.ItemArray[2]),
                    IsCharacter = Convert.ToBoolean(Row.ItemArray[3]),
                    IsDateSpecific = Convert.ToBoolean(Row.ItemArray[4]),
                    StartDate = Convert.ToString(Row.ItemArray[5]),
                    EndDate = Convert.ToString(Row.ItemArray[6]),
                    IsTimeSpecific = Convert.ToBoolean(Row.ItemArray[7]),
                    StartTime = Convert.ToString(Row.ItemArray[8]),
                    EndTime = Convert.ToString(Row.ItemArray[9]),
                    IsQuestSpecific = Convert.ToBoolean(Row.ItemArray[10]),
                    QuestID = Convert.ToInt16(Row.ItemArray[11]),
                    HasDiscount = Convert.ToBoolean(Row.ItemArray[12]),
                    Discount = Convert.ToByte(Row.ItemArray[13]),
                    HasMaximum = Convert.ToBoolean(Row.ItemArray[14]),
                    Maximum = Convert.ToInt16(Row.ItemArray[15])
                };

                Currencies.Add(NewCurrency);
            }

            SHNFile CurrencyItems = new SHNFile(String.Format("{0}CurrencyItems.shn", Auth.ConfigValues["FilterSHNPath"]), CryptoMethod);

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

            if (CurrencyItems.Type != SHNType.QuestData) { CurrencyItems.Read(); }
            else { CurrencyItems.ReadQuest(); }

            CurrencyItems.DisallowRowChanges();

            foreach (DataRow Row in CurrencyItems.Table.Rows)
            {
                CurrencyItem NewCurrencyItem = new CurrencyItem()
                {
                    ItemID = Convert.ToUInt32(Row.ItemArray[0]),
                    IsMainHandle = Convert.ToBoolean(Row.ItemArray[1]),
                    IsCoin = Convert.ToBoolean(Row.ItemArray[2]),
                    IsAutoAdd = Convert.ToBoolean(Row.ItemArray[3]),
                    IsAbstate = Convert.ToBoolean(Row.ItemArray[4]),
                    AbstateIndex = Convert.ToString(Row.ItemArray[5]),
                    AbstateDuration = Convert.ToInt32(Row.ItemArray[6]),
                    AbstateEffect = Convert.ToByte(Row.ItemArray[7]),
                    IsMob = Convert.ToBoolean(Row.ItemArray[8]),
                    MobIndex = Convert.ToString(Row.ItemArray[9]),
                    MobAmount = Convert.ToByte(Row.ItemArray[10]),
                    IsTrigger = Convert.ToBoolean(Row.ItemArray[11]),
                    TriggerID = Convert.ToByte(Row.ItemArray[12]),
                    CurrencyID = Convert.ToByte(Row.ItemArray[13]),
                    CurrencyWorth = Convert.ToInt32(Row.ItemArray[14]),
                    IsCharacterCreated = Convert.ToBoolean(Row.ItemArray[15]),
                    HasUpgrade = Convert.ToBoolean(Row.ItemArray[16]),
                    Upgrade = Convert.ToByte(Row.ItemArray[17])
                };

                CurrencyItemIDs.Add(NewCurrencyItem);
            }
        }
    }
}
