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

namespace HaloPlugins
{
    public class BlockIndex : IMetaNode
    {
        string Name = "BlockIndex", BlockName = "", FieldName = "";
        object Index;
        Type Type;

        public BlockIndex(string Name, string BlockName, string FieldName, Type Type)
        {
            this.Name = Name;
            this.BlockName = BlockName;
            this.FieldName = FieldName;
            this.Type = Type;
        }

        #region MetaNode Members

        public void Read(EndianReader br)
        {
            if (Type == typeof(byte))
                Index = br.ReadByte();
            else if (Type == typeof(short))
                Index = br.ReadInt16();
            else if (Type == typeof(int))
                Index = br.ReadInt32();
        }

        public void Read(EndianReader br, int Magic)
        {
            if (Type == typeof(byte))
                Index = br.ReadByte();
            else if (Type == typeof(short))
                Index = br.ReadInt16();
            else if (Type == typeof(int))
                Index = br.ReadInt32();
        }

        public void Write(EndianWriter bw)
        {
            if (Type == typeof(byte))
                bw.Write((byte)Index);
            else if (Type == typeof(short))
                bw.Write((short)Index);
            else if (Type == typeof(int))
                bw.Write((int)Index);
        }

        public object GetValue()
        {
            return Index;
        }

        public void SetValue(object Value)
        {
            Index = Value;
        }

        public int GetSize()
        {
            return Type == typeof(byte) ? 1 : Type == typeof(short) ? 2 : 4;
        }

        public void Compile(EndianWriter bw, int Magic)
        {
            if (Type == typeof(byte))
                bw.Write((byte)Index);
            else if (Type == typeof(short))
                bw.Write((short)Index);
            else if (Type == typeof(int))
                bw.Write((int)Index);
        }

        public void Decompile(EndianWriter bw, int Magic)
        {
            if (Type == typeof(byte))
                bw.Write((byte)Index);
            else if (Type == typeof(short))
                bw.Write((short)Index);
            else if (Type == typeof(int))
                bw.Write((int)Index);
        }

        public string GetName()
        {
            return Name;
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            BlockIndex b = new BlockIndex(this.Name, this.BlockName, this.FieldName, this.Type);
            b.Index = this.Index;
            return b;
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "BlockIndex: " + Name + ", BlockName: " + BlockName + ", FieldName: " + FieldName + ", BlockIndex: " + Index.ToString();
        }

        #endregion
    }
}
