using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HaloMap
{
    public class Unicode
    {
        public static short[] uniDwords = new short[34];
        public static string[] uniTranslations = new string[34];

        public UnicLang[] Languages = new UnicLang[8];

        public Unicode()
        {
            #region Initialize
            //1
            uniDwords[0] = -30575;
            uniDwords[1] = -30319;
            uniDwords[2] = -30063;
            uniDwords[3] = -24700;
            uniDwords[4] = -24956;
            uniDwords[5] = -21884;
            uniDwords[6] = -25212;
            uniDwords[7] = -27260;
            uniDwords[8] = -23164;
            uniDwords[9] = -23420;
            uniDwords[10] = -27516;
            uniDwords[11] = -26748;
            uniDwords[12] = -28028;
            uniDwords[13] = -25724;
            uniDwords[14] = -25980;
            uniDwords[15] = -24444;
            uniDwords[16] = -24188;
            uniDwords[17] = -23932;
            uniDwords[18] = -23676;
            uniDwords[19] = -22908;
            uniDwords[20] = -22396;
            uniDwords[21] = -27772;
            uniDwords[22] = -25468;
            uniDwords[23] = -27004;

            //2
            uniDwords[24] = -23920;
            uniDwords[25] = -29551;
            uniDwords[26] = -28527;
            uniDwords[27] = -26735;
            uniDwords[28] = -26991;
            uniDwords[29] = -24943;
            uniDwords[30] = -28271;
            uniDwords[31] = -29295;
            uniDwords[32] = -32380;
            uniDwords[33] = -31100;

            uniTranslations[0] = "(X)";
            uniTranslations[1] = "(Y)";
            uniTranslations[2] = "(B)";
            uniTranslations[3] = "[Energy Sword]";
            uniTranslations[4] = "[Plasma Rifle]";
            uniTranslations[5] = "[Brute Plasma Rifle]";
            uniTranslations[6] = "[Plasma Pistol]";
            uniTranslations[7] = "[Covenant Carbine]";
            uniTranslations[8] = "[Distintigrader]";
            uniTranslations[9] = "[Beam Rifle]";
            uniTranslations[10] = "[Brute Shot]";
            uniTranslations[11] = "[Fuel Rod Cannon]";
            uniTranslations[12] = "[Battle Rifle]";
            uniTranslations[13] = "[Needler]";
            uniTranslations[14] = "[Magnum]";
            uniTranslations[15] = "[Rocket Launcher]";
            uniTranslations[16] = "[Shotgun]";
            uniTranslations[17] = "[SMG]";
            uniTranslations[18] = "[Sniper Rifle]";
            uniTranslations[19] = "[Sentinel Beam]";
            uniTranslations[20] = "[SGrenade Launcher]";
            uniTranslations[21] = "[BOMB]";
            uniTranslations[22] = "[ODD]";
            uniTranslations[23] = "[FLAG]";

            //2
            uniTranslations[24] = "[Num]";
            uniTranslations[25] = "(LT)";
            uniTranslations[26] = "(LAS)";
            uniTranslations[27] = "(RAS Up Down)";
            uniTranslations[28] = "(LAS2)";
            uniTranslations[29] = "(LT2)";
            uniTranslations[30] = "(RAS Left Right)"; //?
            uniTranslations[31] = "(RT)";
            uniTranslations[32] = "[BansheeBoost]";
            uniTranslations[33] = "(LT3)";
            #endregion
        }

        public void Read(Map map)
        {
            for (int i = 0; i < 8; i++)
            {
                // Goto Matg
                map.br.BaseStream.Position = map.Tags[0].Offset + 400 + (i * 28);

                // Read Info
                Languages[i] = new UnicLang();
                Languages[i].StringCount = map.br.ReadInt32();
                Languages[i].TableSize = map.br.ReadInt32();
                Languages[i].IndexOffset = map.br.ReadInt32();
                Languages[i].TableOffset = map.br.ReadInt32();

                map.br.BaseStream.Position = Languages[i].IndexOffset;
                for (int x = 0; x < Languages[i].StringCount; x++)
                {
                    map.br.BaseStream.Position = Languages[i].IndexOffset + (x * 8);
                    UnicString s = new UnicString();
                    s.LinkIndex = map.br.ReadInt16();
                    s.Link = map.SIDs[s.LinkIndex];
                    map.br.BaseStream.Position += 2;
                    s.Offset = map.br.ReadInt32();

                    // Unicode
                    map.br.BaseStream.Position = Languages[i].TableOffset + s.Offset;
                    byte b;
                    List<byte> str = new List<byte>();
                    while ((b = map.br.ReadByte()) != 0)
                    {
                        str.Add(b);
                    }
                    s.String = str.ToArray();
                    Languages[i].Strings.Add(s);
                }
            }
        }
    }

    public class UnicLang
    {
        public int StringCount, TableSize, TableOffset, IndexOffset;
        public List<UnicString> Strings = new List<UnicString>();
    }

    public class UnicString
    {
        public short LinkIndex;
        public string Link;
        public int Offset;
        public byte[] String;
    }
}
