using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Halo3Map.Map;
using Halo3Map.Data;

namespace MetaEditor.Controls
{
    /// <summary>
    /// Used to display TagReference value types
    /// </summary>
    public partial class Ident : UserControl
    {
        /// <summary>
        /// Lets us access various information about the map file we are working with
        /// </summary>
        public Map map;
        /// <summary>
        /// Creates a new Instance of Ident
        /// </summary>
        /// <param name="value">Ident of the Tag Reference to be displayed</param>
        /// <param name="name">Name of the Tag Reference</param>
        /// <param name="Map">Lets us access various information about the map file we are working with</param>
        public Ident(int value, string name, Map Map)
        {
            //Set map
            map = Map;

            InitializeComponent();
            //Set control name
            label1.Text = name;

            //Add Null options to comboBox's
            comboBox1.Items.Add("null");
            comboBox2.Items.Add("null");

            //If Ident isnt NULL
            if (value != -1)
            {
                comboBox1.Text = map.Index.Metas[map.FindIndexNumberByID(value)].TagType;
                comboBox2.Text = map.Index.Metas[map.FindIndexNumberByID(value)].FilePath;
                Load_Tag_List(comboBox1.Text);
            }
            else
            {
                //If Ident is NULL set control values to NULL
                comboBox1.Text = "null";
                comboBox2.Text = "null";
            }            
            //Load list of tag classes
            Load_Tag_Class_List();            
        }

        public void Reload(int value)
        {
            if (value != -1)
            {
                comboBox1.Text = map.Index.Metas[map.FindIndexNumberByID(value)].TagType;
                comboBox2.Text = map.Index.Metas[map.FindIndexNumberByID(value)].FilePath;
            }
            else
            {
                comboBox1.Text = "null";
                comboBox2.Text = "null";
            } 
        }

        /// <summary>
        /// Load tag list a tag names for selected tag type
        /// </summary>
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_Tag_List(comboBox1.Items[comboBox1.SelectedIndex].ToString());
        }
        /// <summary>
        /// Returns the name of the controls value
        /// </summary>
        /// <returns></returns>
        public string ReturnName()
        {
            return label1.Text.Replace(' ', '_');
        }
        /// <summary>
        /// Returns a value of the controls Tag References
        /// </summary>
        /// <returns></returns>
        public TagReference ReturnValue()
        {
            TagReference t = new TagReference();
            t.TagClass = comboBox1.Text;
            t.Unknown = new byte[8];
            t.Ident = map.Index.Metas[map.FindIndexNumberByFilename(comboBox2.Text)].ident;
            t.Index = (short)map.FindIndexNumberByFilename(comboBox2.Text);
            t.Map = map;
            return t;
        }
        /// <summary>
        /// Loads a list of Tag classes into the control
        /// </summary>
        public void Load_Tag_Class_List()
        {
            comboBox1.Items.Clear();
            for (int i = 0; i < map.Index.MetaClasses.Count; i++)
            {
                comboBox1.Items.Add(map.Index.MetaClasses[i].type);
            }
        }
        /// <summary>
        /// Loads a list of Tag names into the control
        /// </summary>
        /// <param name="type"></param>
        public void Load_Tag_List(string type)
        {
            comboBox2.Items.Clear();
            for (int i = 0; i < map.Index.meta_count; i++)
            {
                if (map.Index.Metas[i].FilePath != null && comboBox1.Text == map.Index.Metas[i].TagType)
                {
                    comboBox2.Items.Add(map.Index.Metas[i].FilePath);
                }
            }
        }
    }
}
