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

namespace HaloPlugins
{
    public class StringId : IMetaNode
    {
        int Index = 0;
        byte Length = 0;
        string Name = "StringId";
        string Id = "";
        HaloEngine Engine = HaloEngine.Halo2;

        public StringId(string Name)
        {
            this.Name = Name;
        }

        public StringId(string Name, HaloEngine Engine)
        {
            this.Name = Name;
            this.Engine = Engine;
        }

        #region MetaNode Members

        public void Read(EndianReader br)
        {
            if (Engine == HaloEngine.Halo3)
            {
                Index = br.ReadInt32();
            }
            else
            {
                Index = br.ReadInt16();
                Length = br.ReadBytes(2)[1];
            }

            Id = ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"])[Index];
        }

        public void Read(EndianReader br, int Magic)
        {
            if (Engine == HaloEngine.Halo3)
            {
                Index = br.ReadInt32();
            }
            else
            {
                Index = br.ReadInt16();
                Length = br.ReadBytes(2)[1];
            }

            Id = ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"])[Index];
        }

        public void Write(EndianWriter bw)
        {
            // Add To String List
            ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"]).Add(Id);
            Index = ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"]).Count - 1;

            if (Engine == HaloEngine.Halo3)
            {
                bw.Write(Index);
            }
            else
            {
                bw.Write((short)Index);
                bw.Write(new byte[] { (byte)0, (byte)Id.Length });
            }
        }

        public object GetValue()
        {
            return Id;
        }

        public void SetValue(object Value)
        {
            Id = (string)Value;
        }

        public int GetSize()
        {
            return 4;
        }

        public void Compile(EndianWriter bw, int Magic)
        {
            // Get New Index
            Index = ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"]).IndexOf(Id);

            if (Engine == HaloEngine.Halo3)
            {
                bw.Write(Index);
            }
            else
            {
                bw.Write(Index == -1 ? (short)0 : (short)Index);
                if (Index != -1) bw.Write(new byte[] { (byte)0, (byte)((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"])[Index].Length });
            }
        }

        public void Decompile(EndianWriter bw, int Magic)
        {
            // Add To String List
            ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"]).Add(Id);
            Index = ((List<string>)EngineManager.GetEngine()[EngineManager.EngineId]["StringIds"]).Count - 1;

            if (Engine == HaloEngine.Halo3)
            {
                bw.Write(Index);
            }
            else
            {
                bw.Write(Index == -1 ? (short)0 : (short)Index);
                bw.Write(new byte[] { (byte)0, Length });
            }
        }

        public string GetName()
        {
            return Name;
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            StringId s = new StringId(this.Name);
            s.Index = Index;
            s.Length = Length;
            s.Id = Id;
            s.Engine = Engine;
            return s;
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "StringId: " + Name + ", Id: " + Id;
        }

        #endregion
    }
}
