using System;
using System.Collections.Generic;
using System.Text;
using Halo3Map.Data;
using Halo3Map.IO;

namespace Halo3Map.Map
{
    public class IndexHeader : Chunk
    {

        #region Layout

        public int tag_type_count;
        public int tag_type_index_offset;
        public int meta_count;
        public int tag_info_offset;
        public int unkown_six;
        public int main_tags;
        public int unkown_zero;
        public int main_tags_2;
        public int unknown;

        [Size(4)]
        public string tags;//36

        #endregion

        private List<MetaClass> metaClasses;
        public List<MetaClass> MetaClasses
        {
            get { return metaClasses; }
        }

        private List<Meta> metas;
        public List<Meta> Metas
        {
            get { return metas; }
        }

        public override void Read(EndianReader br)
        {
            base.Read(br);

            //Read all the meta types
            br.SeekTo(tag_type_index_offset - Map.Magic);
            metaClasses = new List<MetaClass>();
            for (int x = 0; x < tag_type_count; x++)
            {
                MetaClass metaClass = new MetaClass();
                metaClass.Read(br);
                metaClasses.Add(metaClass);
            }

            //Now that we have this info lets read the tags
            br.SeekTo(tag_info_offset - Map.Magic);
            metas = new List<Meta>();
            for (int x = 0; x < meta_count; x++)
            {
                Meta meta = new Meta();
                meta.Map = this.Map;
                meta.MetaIndex = x;
                meta.Read(br);
                metas.Add(meta);
            }
        }

    }

    public class MetaClass : Chunk
    {

        #region Layout

        [String(4)]
        public string type;

        [Size(12)]
        public byte[] unknown;

        #endregion

    }

    public class Meta : Chunk
    {

        #region Layout

        public short tagClassIndex;//0
        public short ident;//2
        public int memoryOffset;//4

        #endregion

        private int mapOffset;
        public int Offset { get { return memoryOffset - Map.Magic; } }

        private int metaIndex = -1;
        public int MetaIndex { get { return metaIndex; } set { metaIndex = value; } }

        public string FilePath { get { return Map.Header.FilePaths[MetaIndex]; } }
        public string TagType { get { return Map.Index.MetaClasses[tagClassIndex].type; } }
        public int ID
        {
            get
            {
                int newIdent = ident;
                newIdent <<= 16;
                newIdent |= MetaIndex;
                return newIdent;
            }
        }
    }
}