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

namespace Cartographer.Scripts
{
    public class Scripts
    {
        public EndianReader br;
        public int scnrOffset;
        public const int StringTableSizeOffset = 992;
        public const int StringTableOffset = 1004;
        public const int ScriptsOffset = 1012;
        public const int GlobalsOffset = 1024;
        public const int SyntaxOffset = 1188;

        public int stringTableSize;
        public int scriptCount;
        public int scriptsPos;
        public int globalCount;
        public int globalsPos;
        public int syntaxCount;
        public int syntaxPos;

        public ScriptDatabase database;
        public ScriptList scripts;
        public GlobalList globals;

        public const bool logDatabase = true;
        public const bool logValueTypes = true;

        public Scripts(Map map, EndianReader br)
        {
            this.br = br;
            scnrOffset = map.Index.Metas[8].Offset;

            br.BaseStream.Position = scnrOffset + StringTableSizeOffset;
            stringTableSize = br.ReadInt32();
            br.BaseStream.Position = scnrOffset + StringTableOffset;
            int stringTablePos = br.ReadInt32() - (int)map.Magic;
            br.BaseStream.Position = scnrOffset + ScriptsOffset;
            scriptCount = br.ReadInt32();
            scriptsPos = br.ReadInt32() - (int)map.Magic;
            br.BaseStream.Position = scnrOffset + GlobalsOffset;
            globalCount = br.ReadInt32();
            globalsPos = br.ReadInt32() - (int)map.Magic;
            br.BaseStream.Position = scnrOffset + SyntaxOffset;
            syntaxCount = br.ReadInt32();
            syntaxPos = br.ReadInt32() - (int)map.Magic;

            br.BaseStream.Position = stringTablePos;
            ScriptStrings.Load(br, stringTableSize);
            scripts = new ScriptList(this);
            globals = new GlobalList(this);
        }
    }
}
