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

namespace HaloObjects
{
    public class Bitmask : MetaNode
    {
        int Value = 0, BitCount = 8;
        string[] Options = new string[0];
        string Name = "Bitmask";

        public Bitmask(string Name, string[] Options, int BitCount)
        {
            this.Name = Name;
            this.Options = Options;
            this.BitCount = BitCount;
        }

        #region MetaNode Members

        public void Read(System.IO.BinaryReader br)
        {
            if (BitCount == 8)
                Value = br.ReadByte();
            else if (BitCount == 16)
                Value = br.ReadInt16();
            else if (BitCount == 32)
                Value = br.ReadInt32();
        }

        public void Read(System.IO.BinaryReader br, int Magic)
        {
            if (BitCount == 8)
                Value = br.ReadByte();
            else if (BitCount == 16)
                Value = br.ReadInt16();
            else if (BitCount == 32)
                Value = br.ReadInt32();
        }

        public void Write(System.IO.BinaryWriter bw)
        {
            if (BitCount == 8)
                bw.Write((byte)Value);
            else if (BitCount == 16)
                bw.Write((short)Value);
            else if (BitCount == 32)
                bw.Write(Value);
        }

        public object GetValue()
        {
            return Value;
        }

        public void SetValue(object Value)
        {
            if (BitCount == 8)
                this.Value = (byte)Value;
            else if (BitCount == 16)
                this.Value = (short)Value;
            else if (BitCount == 32)
                this.Value = (int)Value;
        }

        public int GetSize()
        {
            return BitCount / 8;
        }

        public void Compile(System.IO.BinaryWriter bw, int Magic)
        {
            if (BitCount == 8)
                bw.Write((byte)Value);
            else if (BitCount == 16)
                bw.Write((short)Value);
            else if (BitCount == 32)
                bw.Write(Value);
        }

        public void Decompile(System.IO.BinaryWriter bw, int Magic)
        {
            if (BitCount == 8)
                bw.Write((byte)Value);
            else if (BitCount == 16)
                bw.Write((short)Value);
            else if (BitCount == 32)
                bw.Write(Value);
        }

        public string GetName()
        {
            return Name;
        }

        #endregion

        public string[] GetOptions()
        {
            return Options;
        }

        public int GetBitCount()
        {
            return BitCount;
        }

        #region ICloneable Members

        public object Clone()
        {
            Bitmask b = new Bitmask(this.Name, this.Options, this.BitCount);
            b.Value = Value;
            return b;
        }

        #endregion
    }
}
