using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress;
using DevExpress.XtraTab;
using System.Reflection;
using System.IO;
using Global;
using HaloPlugins;
using IO;

namespace HaloControls
{
    public partial class TagReference : UserControl
    {
        string Reference;
        public int Index;

        public TagReference(string Description, string Reference, int Index)
        {
            InitializeComponent();
            label1.Text = Description;
            this.Index = Index;
            this.Reference = Reference;

            // Set textbox values
            textEdit1.Text = Reference;
            textEdit1.ForeColor = Reference != "Null Reference" ? Color.RoyalBlue : Color.Red;
            textEdit1.Location = new Point((label1.Location.X + label1.Size.Width) + 5, 5);

            // Set button values
            button1.Location = new Point((textEdit1.Location.X + textEdit1.Size.Width) + 5, 3);
            button2.Location = new Point((button1.Location.X + button1.Size.Width) + 5, 3);
        }

        public void Reload(object Value)
        {
            Reference = (string)Value;
            textEdit1.Text = Reference;
            textEdit1.ForeColor = Reference != "Null Reference" ? Color.RoyalBlue : Color.Red;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Halo 2 Tags (*.*)|*.*";
            if (Global.ProjectManager.GetOpenedProject() != null)
                ofd.InitialDirectory = Global.ProjectManager.GetOpenedProject().TagFolder;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // Open tag
                EndianReader br = new EndianReader(IO.Endianness.Little, new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read));

                // Get Ext
                string ext = ofd.FileName.Substring(ofd.FileName.LastIndexOf(".") + 1);
                TagDefinition tagdef = EngineManager.GetEngine()[Global.ProjectManager.GetOpenedProject().Engine].CreateInstance(ext);

                // Read
                tagdef.Read(br);

                // Close
                br.Close();

                // See if tag belongs to this project
                if (!Global.ProjectManager.GetOpenedProject().Tags.Contains(tagdef.AbsolutePath))
                {
                    // Ask to add
                    if (MessageBox.Show("Mutation has detected this tag does not belong to the opened project.\r\nWould you like to add it to the project?", "Add tag to project", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        // Add
                        MessageBox.Show("This feature has not yet been enabled in Mutation.\r\nPlease wait for a new build.", "Future feature");
                        return;
                    }
                }
                else
                {
                    Reference = tagdef.AbsolutePath;
                    this.textEdit1.Text = Reference;
                    textEdit1.ForeColor = Color.RoyalBlue;
                }
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            // Check if it Exists
            if (Global.ProjectManager.GetOpenedProject() != null)
            {
                string Path = Global.ProjectManager.GetOpenedProject().TagFolder + textEdit1.Text;
                if (File.Exists(Path))
                    Global.EventHandler.OpenTag(Path);
            }
        }

        public object Save()
        {
            return textEdit1.Text;
        }
    }
}
