using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Halo3Map.Data
{
    /// <summary>
    /// This is the based class used for all .cs plugins
    /// </summary>
    public class H3Tag : Chunk
    {
        /// <summary>
        /// Holds the name of the plugins Author for this tag
        /// </summary>
        private string author;
        /// <summary>
        /// Retrieves the name of the Authour of this tags plugin
        /// </summary>
        public string Author { get { return author; } }
        /// <summary>
        /// Holds the Tag Type of this tag
        /// </summary>
        private string tagType;
        /// <summary>
        /// Retrieves the Tag Type of this tag
        /// </summary>
        public string TagType { get { return tagType; } }
        /// <summary>
        /// Holds the version of the plugin used to load this tag
        /// </summary>
        private double version;
        /// <summary>
        /// Retrieves the version of the plguin used to load this tag
        /// </summary>
        public double Version { get { return version; } }
        /// <summary>
        /// Stores the offset this tag is stored at in the map
        /// </summary>
        private int mapOffset;
        /// <summary>
        /// Gets or Sets the offset this tag is located in the map
        /// </summary>
        public int MapOffset { get { return mapOffset; } set { mapOffset = value; } }

        public H3Tag()
        {
            object[] attributes = this.GetType().GetCustomAttributes(typeof(TagAttribute), false);
            if (attributes.Length > 0)
            {
                TagAttribute tagA = (TagAttribute)attributes[0];
                author = tagA.Author;
                tagType = tagA.TagType;
                version = tagA.Version;
            }
            else
                MessageBox.Show("There is no information for this tag! Tag: " + this.GetType());
        }

        public override void Read(Halo3Map.IO.EndianReader br)
        {
            base.Read(br);
        }

        public void Read(Halo3Map.IO.EndianReader br, uint TagOffset)
        {
            Read(br, (int)TagOffset);
        }

        public void Read(Halo3Map.IO.EndianReader br, int TagOffset)
        {
            mapOffset = TagOffset;
            br.SeekTo(mapOffset);
            Read(br);
        }

        public override void Write(Halo3Map.IO.EndianWriter bw)
        {
            bw.SeekTo(mapOffset);
            base.Write(bw);
        }

        public override void Extract(Halo3Map.IO.EndianWriter bw, System.Xml.XmlTextWriter xtw)
        {
            xtw.Formatting = Formatting.Indented;

            xtw.WriteStartElement("Tag");
            xtw.WriteAttributeString("Type", this.tagType);
            xtw.WriteAttributeString("Offset", this.mapOffset.ToString());
            xtw.WriteAttributeString("EngineerVersion", "1.1");

            base.Extract(bw, xtw);

            xtw.WriteEndElement();
            xtw.Close();
        }

        public override void Inject(Halo3Map.IO.EndianReader br)
        {
            base.Inject(br);
            base.Map.IO.Out.SeekTo(mapOffset);
            base.Write(base.Map.IO.Out);
        }
    }
}