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

namespace HaloPlugins
{
    public class TagIndex : IMetaNode
    {
        string Name = "TagIndex", Class = "", Tag = "Null Reference";
        int Index = -1, Id = -1;

        public TagIndex(string Name, string Class)
        {
            this.Name = Name;
            this.Class = Class;
        }

        #region MetaNode Members

        public void Read(EndianReader br)
        {
            Tag = (Index = Id = br.ReadInt32()) != -1 ? ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Keys.ElementAt(Index) : "Null Reference";
        }

        public void Read(EndianReader br, int Magic)
        {
            Index = Id = br.ReadInt32();

            // Add
            if (Index != -1)
            {
                Tag = ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Keys.ElementAt(Id & 0xFFFF);
            }
        }

        public void Write(EndianWriter bw)
        {
            // Add
            try { Index = ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"])[Tag]; }
            catch
            {
                Index = ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Count;
                ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Add(Tag, Index);
            }

            bw.Write(Index);
        }

        public object GetValue()
        {
            return Tag;
        }

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

        public int GetSize()
        {
            return 4;
        }

        public void Compile(EndianWriter bw, int Magic)
        {
            bw.Write(Index != -1 ? ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"])[Tag] : Index);
        }

        public void Decompile(EndianWriter bw, int Magic)
        {
            // Add
            if (Tag != "Null Reference")
            {
                try { Index = ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"])[Tag]; }
                catch
                {
                    Index = ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Count;
                    ((Dictionary<string, int>)EngineManager.GetEngine()[EngineManager.EngineId]["TagIds"]).Add(Tag, Index);
                }
            }
            else
                Index = -1;

            bw.Write(Index);
        }

        public string GetName()
        {
            return Name;
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            TagIndex i = new TagIndex(this.Name, this.Class);
            i.Index = this.Index;
            i.Tag = this.Tag;
            i.Id = this.Id;
            return i;
        }

        #endregion

        #region TagIndex Members

        public int GetId()
        {
            return Id & 0xFFFF;
        }

        public string GetFileName()
        {
            return Tag;
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "TagIndex: " + Name + ", Class: " + Class + ", Index: " + Index.ToString() + ", Tag: " + Tag;
        }

        #endregion
    }
}
