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

namespace Halo3Map.Map
{

    public enum version : int
    {
        halo_3_beta = 9,
        halo_3_retail = 11
    }

    public enum Map_Type : short
    {
        single_player = 0,
        multi_player = 1,
        mainmenu = 2,
        shared = 3,
        single_player_shared = 4
    }

    public class Header : Chunk
    {

        #region Layout

        [String(4)]
        public string head; //0

        public version build; //4
        public int map_size; //8

        [Size(4)]
        public byte[] unused1;//12

        public int memory_modifier; //16
        public int index_header_offset; //20

        [Size(260)]
        public byte[] unknown1;//24

        [String(32)]
        public string time_stamp; //284

        public Map_Type map_type; //316

        [Size(26)]
        public byte[] unknown2;//318

        public int string_count; //344
        public int string_table_size; //348
        public int string_table_index_offset; //352
        public int string_table_offset; //356

        [Size(36)]
        public byte[] unknown3;//360

        [String(32)]
        public string map_name; //396

        [Size(4)]
        public byte[] unknown4;//428

        [String(256)]
        public string scenario_name; //432

        [Size(4)]
        public byte[] unknown5;//688

        public int file_table_count; //692
        public int file_table_offset; //696
        public int file_index_offset; //700
        public int file_index_strings_offset; //704

        public int checksum; //708

        [Size(32)]
        public byte[] unknown6;//712

        public int base_address; //744

        [Size(388)]
        public byte[] unknown8;//748

        public int raw_data_offset; //1136

        [Size(20)]
        public byte[] unknown10;//1140

        public int raw_data_size; //1160

        [Size(11120)]
        public byte[] unknown12;//1164

        [String(4)]
        public string foot; //12284

        #endregion

        /// <summary>
        /// This is a magic that is applied to the header
        /// </summary>
        public int HeaderMagic { get { return string_table_index_offset - 0x3000; } }

        private List<string> filePaths;
        public List<string> FilePaths { get { return filePaths; } }

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

            //Make sure this isnt a shared map before we read the tags
            if (map_type == Map_Type.shared ||
                map_type == Map_Type.single_player_shared)
                return;

            //Read all the file paths
            br.SeekTo(file_table_offset - HeaderMagic);
            filePaths = new List<string>();
            for (int x = 0; x < file_table_count; x++)
            {
                string temp = br.ReadNullTerminatedString();
                if (temp == "") temp = "{NO NAME}";
                filePaths.Add(temp);
            }
        }

    }
}