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

namespace Halo3Map.Scripting
{
    #region Read Script Data
    public class ScriptsData
    {        
        public Map.Map map;
        public List<ScriptTable> scripttable;
        public List<Scripts> scripts;
        public List<ScriptSyntax> scryptsyntax;

        public ScriptsData(Map.Map Map)
        {
            map = Map;
            LoadScriptData(map.IO.In);     
        }

        public void LoadScriptData(Halo3Map.IO.EndianReader br)
        {

            br.BaseStream.Position = map.Index.Metas[8].Offset + 992;
            int scriptTableSize = br.ReadInt32();
            br.BaseStream.Position += 8;
            int scriptTableOffset = br.ReadInt32() - map.Magic;

            br.BaseStream.Position = scriptTableOffset;
            scripttable = new List<ScriptTable>();
            for (int i = 0; i < scriptTableSize - 1; i++)
            {
                ScriptTable s = new ScriptTable();
                s.Read(br);
                scripttable.Add(s);
            }

            br.BaseStream.Position = map.Index.Metas[8].Offset + 1012;
            int scriptCount = br.ReadInt32();
            int scriptTrans = br.ReadInt32() - map.Magic;

            scripts = new List<Scripts>();
            br.BaseStream.Position = scriptTrans;
            for (int i = 0; i < scriptCount; i++)
            {
                Scripts s = new Scripts();
                s.Read(br);
                scripts.Add(s);
            }

            br.BaseStream.Position = map.Index.Metas[8].Offset + 1188;
            int scriptSyntaxCount = br.ReadInt32();
            int scriptSyntaxTrans = br.ReadInt32() - map.Magic;
            
            scryptsyntax = new List<ScriptSyntax>();
            br.BaseStream.Position = scriptSyntaxTrans;
            for (int i = 0; i < scriptSyntaxCount; i++)
            {
                ScriptSyntax s = new ScriptSyntax();
                s.Read(br);
                scryptsyntax.Add(s);
            }  
        }
    }
    #endregion

    public enum ScriptType : short
    {
        Startup = 0,
        Dormant = 1,
        Continuous = 2,
        Static = 3,
        Stub = 4,
        Command_Script = 5
    }

    public enum ReturnType : short
    {
       unparsed = 0,
       SpecialForm = 1,
       FunctionName = 2,
       passthrough = 3,
       Void = 4,
       boolean = 5,
       real = 6,
       Short = 7,
       Long = 8,
       String = 9,
       script = 10,
       string_id = 11,
       unit_seat_mapping = 12,
       trigger_volume = 13,
       cutscene_flag = 14,
       cutscene_camera_point = 15,
       cutscene_title = 16,
       cutscene_recording = 17,
       device_group = 18,
       ai     = 19,
       ai_command_list = 20,
       ai_command_script = 21,
       ai_behaviour = 22,
       ai_orders = 23,
       starting_profile = 24,
       conversation = 25,
       structure_bsp = 26,
       navpoint = 27,
       point_refrence = 28,
       style = 29,
       hud_message = 30,
       object_list = 31,
       scenary = 32,
       effect = 33,
       unit = 34,
       looping_sound = 35,
       animation_graph = 36,
       object_definition = 37,
       bitmap = 38,
       shader = 39,
       unknown = 40,
       render_model = 41,
       structure_definition = 42,
       lightmap_definition = 43,
       game_difficulty = 44,
       actor_type = 45,
       hud_corner = 46,
       model_state = 47,
       value = 48,
       network_event = 49,
       value2 = 50,
       unit_name = 51,
       vehicle_name = 52,
       weapon_name = 53,
       device_name = 54,
       scenery_name = 55,
       object_name = 56,
    }

    public class ScriptTable : Chunk
    {
        #region Layout
        public char c;
        #endregion
    }

    public class Scripts : Chunk
    {
        #region Layout
        public string Name;
        public ScriptType scriptType;
        public ReturnType returnType;
        public short ExpIndex;
        public short ChunkNum;
        [Size(12)]
        public byte[] Unused;
        #endregion
    }

    public class ScriptSyntax : Chunk
    {
        #region Layout
        public short ExpressionID;
        public short Indentity;
        public short ValueType;
        public short ExpressionType;
        public short SiblingPointer;
        public short SiblingIndex;
        public int ScriptStringOffset;
        [Size(4)]
        public byte[] Value;
        public short unknown20;
        public short unknown22;
        #endregion
    }
}
