namespace Texture_Tool
{
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    public class DOAHeader
    {
        public DOAHeader(FileStream fs)
        {
            this.BaseOffset = (int)fs.Position;
            var br = new BinaryReader(fs, new ASCIIEncoding());
            this.Name = new string(br.ReadChars(8)).TrimEnd(char.MinValue);
            this.Flag1 = br.ReadInt32();
            this.HeaderSize = br.ReadInt32();
            this.Size = br.ReadInt32();
            this.Count1 = br.ReadInt32();
            this.Count2 = br.ReadInt32();
            this.Count3 = br.ReadInt32();
            this.Offset1 = br.ReadInt32();
            this.Offset2 = br.ReadInt32();
            this.Offset3 = br.ReadInt32();
            this.Flag2 = br.ReadInt32();
            if (this.Offset1 > 0)
            {
                this.Table1 = new List<int>();
                br.BaseStream.Position = this.BaseOffset + this.Offset1;
                for (int i = 0; i < this.Count1; i++)
                {
                    this.Table1.Add(br.ReadInt32());
                }
            }

            if (this.Offset2 > 0)
            {
                this.Table2 = new List<int>();
                br.BaseStream.Position = this.BaseOffset + this.Offset2;
                for (int i = 0; i < this.Count1; i++)
                {
                    this.Table2.Add(br.ReadInt32());
                }
            }
        }

        public int BaseOffset { get; set; }

        public string Name { get; set; }

        public int Flag1 { get; set; }

        public int HeaderSize { get; set; }

        public int Size { get; set; }

        public int Count1 { get; set; }

        public int Count2 { get; set; }

        public int Count3 { get; set; }

        public int Offset1 { get; set; }

        public int Offset2 { get; set; }

        public int Offset3 { get; set; }

        public int Flag2 { get; set; }

        public List<int> Table1 { get; set; }

        public List<int> Table2 { get; set; }

        public byte[] Buffer { get; set; }
    }

    public class Texture
    {
        public Texture(Stream s, string texID, int texSize)
        {
            byte[] byteBuffer = new byte[texSize];
            s.Read(byteBuffer, 0, texSize);
            this.Buffer = new MemoryStream(byteBuffer, 0, texSize, false, true);
            var br = new BinaryReader(this.Buffer, new ASCIIEncoding());
            this.RelativeID = texID;
            br.BaseStream.Position = 84;
            this.Type = new string(br.ReadChars(4)).TrimEnd(char.MinValue);
            br.BaseStream.Position = 12;
            this.Height = br.ReadInt32();
            this.Width = br.ReadInt32();
            this.Size = texSize;
        }

        public int AbsoluteID { get; set; }

        public string RelativeID { get; set; }

        public string Type { get; set; }

        public int Height { get; set; }

        public int Width { get; set; }

        public int Size { get; set; }

        public MemoryStream Buffer { get; private set; }
    }
}