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

using CS;
using SHN;

namespace FilterAPI.Portals
{
    public class PortalLoader
    {
        public List<MultiPortal> MultiPortals = new List<MultiPortal>();

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

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

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

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

            MultiPortal.DisallowRowChanges();

            foreach (DataRow Row in MultiPortal.Table.Rows)
            {
                MultiPortal NewMultiPortal = new MultiPortal()
                {
                    MapIndex = Convert.ToString(Row.ItemArray[0]),
                    MobID = Convert.ToUInt16(Row.ItemArray[1]),
                    Question = Convert.ToString(Row.ItemArray[2]),
                    Map1Text = Convert.ToString(Row.ItemArray[3]),
                    Map1Index = Convert.ToString(Row.ItemArray[4]),
                    Map1X = Convert.ToInt32(Row.ItemArray[5]),
                    Map1Y = Convert.ToInt32(Row.ItemArray[6]),
                    Map2Text = Convert.ToString(Row.ItemArray[7]),
                    Map2Index = Convert.ToString(Row.ItemArray[8]),
                    Map2X = Convert.ToInt32(Row.ItemArray[9]),
                    Map2Y = Convert.ToInt32(Row.ItemArray[10]),
                    Map3Text = Convert.ToString(Row.ItemArray[11]),
                    Map3Index = Convert.ToString(Row.ItemArray[12]),
                    Map3X = Convert.ToInt32(Row.ItemArray[13]),
                    Map3Y = Convert.ToInt32(Row.ItemArray[14])
                };

                MultiPortals.Add(NewMultiPortal);
            }
        }
    }
}
