namespace ns3
{
    using iQuest;
    using ns2;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Reflection;
    using System.Text;

    internal class Class2
    {
        private BinaryReader binaryReader_0;
        private byte[] byte_0 = new byte[2];
        private byte byte_1 = 12;
        private FileStream fileStream_0;
        public List<Class3> list_0 = new List<Class3>();

        public Class2(string path)
        {
            if (!File.Exists(path))
            {
                throw new Exception("File does not exists");
            }
            this.fileStream_0 = File.Open(path, FileMode.Open);
            this.binaryReader_0 = new BinaryReader(this.fileStream_0);
            this.byte_0 = this.binaryReader_0.ReadBytes(2);
            short num = this.binaryReader_0.ReadInt16();
            for (short i = 0; i < num; i = (short) (i + 1))
            {
                this.method_0();
            }
            this.fileStream_0.Dispose();
        }

        private void method_0()
        {
            Class3 class2 = new Class3();
            this.method_1(class2);
            this.method_6(class2);
            this.method_5(class2);
            this.method_3(class2);
            class2.StartScript = this.method_7();
            class2.ActionScript = this.method_7();
            class2.FinishScript = this.method_7();
            this.list_0.Add(class2);
        }

        private void method_1(Class3 class3_0)
        {
            this.binaryReader_0.ReadUInt32();
            class3_0.ID = this.binaryReader_0.ReadUInt16();
            class3_0.Title = this.binaryReader_0.ReadUInt16();
            class3_0.Description = this.binaryReader_0.ReadUInt16();
            class3_0.dictionary_0.Add(1, this.binaryReader_0.ReadBytes(7));
            class3_0.MinLevel = this.binaryReader_0.ReadByte();
            class3_0.MaxLevel = this.binaryReader_0.ReadByte();
            class3_0.dictionary_0.Add(2, this.binaryReader_0.ReadBytes(1));
            class3_0.StartingNPC = this.binaryReader_0.ReadUInt16();
            class3_0.dictionary_0.Add(3, this.binaryReader_0.ReadBytes(0x18));
            class3_0.Predesessor = this.binaryReader_0.ReadUInt16();
            class3_0.dictionary_0.Add(4, this.binaryReader_0.ReadBytes(2));
            class3_0.MultiQuest = this.binaryReader_0.ReadByte();
            class3_0.NeedClass = this.binaryReader_0.ReadByte();
            class3_0.dictionary_0.Add(5, this.binaryReader_0.ReadBytes(15));
            class3_0.NeedLevel = this.binaryReader_0.ReadByte();
            class3_0.dictionary_0.Add(6, this.binaryReader_0.ReadBytes(6));
        }

        private void method_2(BinaryWriter binaryWriter_0, Class3 class3_0)
        {
            binaryWriter_0.Write(class3_0.DataLen);
            binaryWriter_0.Write(class3_0.ID);
            binaryWriter_0.Write(class3_0.Title);
            binaryWriter_0.Write(class3_0.Description);
            binaryWriter_0.Write(class3_0.dictionary_0[1]);
            binaryWriter_0.Write(class3_0.MinLevel);
            binaryWriter_0.Write(class3_0.MaxLevel);
            binaryWriter_0.Write(class3_0.dictionary_0[2]);
            binaryWriter_0.Write(class3_0.StartingNPC);
            binaryWriter_0.Write(class3_0.dictionary_0[3]);
            binaryWriter_0.Write(class3_0.Predesessor);
            binaryWriter_0.Write(class3_0.dictionary_0[4]);
            binaryWriter_0.Write(class3_0.MultiQuest);
            binaryWriter_0.Write(class3_0.NeedClass);
            binaryWriter_0.Write(class3_0.dictionary_0[5]);
            binaryWriter_0.Write(class3_0.NeedLevel);
            binaryWriter_0.Write(class3_0.dictionary_0[6]);
        }

        private void method_3(Class3 class3_0)
        {
            for (byte i = 0; i < this.byte_1; i = (byte) (i + 1))
            {
                QuestReward item = new QuestReward {
                    isGiven = this.binaryReader_0.ReadByte(),
                    Type = (QuestReward.RewardType) this.binaryReader_0.ReadByte(),
                    unkShort = this.binaryReader_0.ReadUInt16()
                };
                if (item.Type == QuestReward.RewardType.Item)
                {
                    item.Value = this.binaryReader_0.ReadUInt16();
                    item.ItemCount = this.binaryReader_0.ReadUInt16();
                    item.unkDword = this.binaryReader_0.ReadUInt32();
                }
                else
                {
                    item.Amount = this.binaryReader_0.ReadUInt64();
                }
                class3_0.list_1.Add(item);
            }
            Stream baseStream = this.binaryReader_0.BaseStream;
            baseStream.Position += 6L;
            class3_0.RewardData = this.binaryReader_0.ReadBytes(0x12);
        }

        public void method_4(string string_0)
        {
            FileStream output = File.Create(string_0);
            BinaryWriter writer = new BinaryWriter(output);
            writer.Write(this.byte_0);
            writer.Write((short) this.list_0.Count);
            for (int i = 0; i < this.list_0.Count; i++)
            {
                long position = writer.BaseStream.Position;
                this.method_2(writer, this.list_0[i]);
                byte num3 = 0;
                foreach (Class4 class2 in this.list_0[i].list_0)
                {
                    num3 = (byte) (num3 + 1);
                    writer.Write(Convert.ToByte(class2.isActive));
                    writer.Write(Convert.ToByte(class2.isMob));
                    writer.Write(class2.ID);
                    writer.Write(Convert.ToByte(class2.HasToBeKilled));
                    writer.Write(class2.Amount);
                }
                for (int j = 0; j < (5 - num3); j++)
                {
                    this.method_8(writer, 6);
                }
                byte num5 = 0;
                foreach (Class5 class3 in this.list_0[i].list_2)
                {
                    num5 = (byte) (num5 + 1);
                    writer.Write(class3.isActive);
                    writer.Write(class3.Type);
                    writer.Write(class3.ID);
                    writer.Write(class3.Amount);
                }
                for (int k = 0; k < (10 - num5); k++)
                {
                    this.method_8(writer, 6);
                }
                writer.Write(this.list_0[i].ItemData);
                byte num7 = 0;
                long num8 = writer.BaseStream.Position;
                foreach (QuestReward reward in this.list_0[i].list_1)
                {
                    num7 = (byte) (num7 + 1);
                    writer.Write(reward.isGiven);
                    switch (reward.Type)
                    {
                        case QuestReward.RewardType.Exp:
                            writer.Write((byte) 0);
                            break;

                        case QuestReward.RewardType.Money:
                            writer.Write((byte) 1);
                            break;

                        case QuestReward.RewardType.Item:
                            writer.Write((byte) 2);
                            break;

                        case QuestReward.RewardType.Fame:
                            writer.Write((byte) 4);
                            break;
                    }
                    writer.Write(reward.unkShort);
                    if (reward.Type == QuestReward.RewardType.Item)
                    {
                        writer.Write(reward.Value);
                        writer.Write(reward.ItemCount);
                        writer.Write(reward.unkDword);
                    }
                    else
                    {
                        writer.Write(reward.Amount);
                    }
                }
                for (int m = 0; m < (this.byte_1 - num7); m++)
                {
                    this.method_8(writer, 12);
                }
                long num1 = writer.BaseStream.Position - num8;
                writer.Write((ushort) (this.list_0[i].StartScript.Length + 1));
                writer.Write((ushort) (this.list_0[i].FinishScript.Length + 1));
                writer.Write((ushort) (this.list_0[i].ActionScript.Length + 1));
                writer.Write(this.list_0[i].RewardData);
                long num10 = writer.BaseStream.Position - position;
                this.method_9(writer, this.list_0[i].StartScript);
                this.method_9(writer, this.list_0[i].ActionScript);
                this.method_9(writer, this.list_0[i].FinishScript);
            }
            output.Close();
            output.Dispose();
        }

        private void method_5(Class3 class3_0)
        {
            for (byte i = 0; i < 10; i = (byte) (i + 1))
            {
                Class5 item = new Class5 {
                    isActive = this.binaryReader_0.ReadByte(),
                    Type = this.binaryReader_0.ReadByte(),
                    ID = this.binaryReader_0.ReadUInt16(),
                    Amount = this.binaryReader_0.ReadUInt16()
                };
                class3_0.list_2.Add(item);
            }
            class3_0.ItemData = this.binaryReader_0.ReadBytes(0x11c);
        }

        private void method_6(Class3 class3_0)
        {
            for (int i = 0; i < 5; i++)
            {
                Class4 item = new Class4 {
                    isActive = Convert.ToBoolean(this.binaryReader_0.ReadByte()),
                    isMob = Convert.ToBoolean(this.binaryReader_0.ReadByte()),
                    ID = this.binaryReader_0.ReadUInt16(),
                    HasToBeKilled = Convert.ToBoolean(this.binaryReader_0.ReadByte()),
                    Amount = this.binaryReader_0.ReadByte()
                };
                class3_0.list_0.Add(item);
            }
        }

        private string method_7()
        {
            byte num2;
            byte[] array = new byte[0x100];
            int count = 0;
            while ((num2 = this.binaryReader_0.ReadByte()) != 0)
            {
                if (array.Length == count)
                {
                    Array.Resize<byte>(ref array, array.Length * 2);
                }
                array[count++] = num2;
            }
            return Encoding.Default.GetString(array, 0, count);
        }

        private void method_8(BinaryWriter binaryWriter_0, int int_0)
        {
            for (int i = 0; i < int_0; i++)
            {
                binaryWriter_0.Write((byte) 0);
            }
        }

        private void method_9(BinaryWriter binaryWriter_0, string string_0)
        {
            binaryWriter_0.Write(Encoding.Default.GetBytes(string_0));
            binaryWriter_0.Write((byte) 0);
        }

        public Class3 this[ushort ushort_0]
        {
            get
            {
                foreach (Class3 class2 in this.list_0)
                {
                    if (class2.ID == ushort_0)
                    {
                        return class2;
                    }
                }
                return null;
            }
        }
    }
}

