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

namespace HaloPlugins
{
    public enum StringType
    {
        Asci,
        Unicode
    }

    public class ShortString : IMetaNode
    {
        string Value = "", Name = "ShortString";
        StringType Type;

        public ShortString(string Name, StringType Type)
        {
            this.Name = Name;
            this.Type = Type;
        }

        #region MetaNode Members

        public void Read(EndianReader br)
        {
            if (Type == StringType.Asci)
                Value = new string(br.ReadChars(32));
            else
                Value = br.ReadUnicodeString(32);
        }

        public void Read(EndianReader br, int Magic)
        {
            if (Type == StringType.Asci)
                Value = new string(br.ReadChars(32));
            else
                Value = br.ReadUnicodeString(32);
        }

        public void Write(EndianWriter bw)
        {
            if (Type == StringType.Asci)
            {
                bw.Write(Value.ToCharArray());
                bw.Write(new byte[32 - Value.Length]);
            }
            else
                bw.WriteUnicodeString(Value, 32);
        }

        public object GetValue()
        {
            return Value;
        }

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

        public int GetSize()
        {
            return Type == StringType.Asci ? 32 : 64;
        }

        public void Compile(EndianWriter bw, int Magic)
        {
            if (Type == StringType.Asci)
            {
                bw.Write(Value.ToCharArray());
                bw.Write(new byte[32 - Value.Length]);
            }
            else
                bw.WriteUnicodeString(Value, 32);
        }

        public void Decompile(EndianWriter bw, int Magic)
        {
            if (Type == StringType.Asci)
            {
                bw.Write(Value.ToCharArray());
                bw.Write(new byte[32 - Value.Length]);
            }
            else
                bw.WriteUnicodeString(Value, 32);
        }

        public string GetName()
        {
            return Name;
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            ShortString s = new ShortString(this.Name, this.Type);
            s.Value = Value;
            return s;
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "ShortString: " + ", Type: " + Type.ToString() + ", String: " + Value;
        }

        #endregion
    }
}
