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 DevComponents.DotNetBar;
using DevExpress;
using DevExpress.XtraTab;
using Global;
using HaloObjects;
using System.Reflection;
using System.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;
            if (Reference != "Null Reference")
            {
                textEdit1.Text = Reference;
                textEdit1.ForeColor = Color.RoyalBlue;
            }
            textEdit1.Location = new Point((label1.Location.X + label1.Size.Width) + 5, 5);
            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;
            if (Reference != "Null Reference")
            {
                textEdit1.Text = Reference;
                textEdit1.ForeColor = Color.RoyalBlue;
            }
            else
            {
                textEdit1.Text = "Null Reference";
                textEdit1.ForeColor = Color.Red;
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Halo 2 Tags (*.*)|*.*";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                throw new Exception("Not Fixed!");
                textEdit1.ForeColor = Color.RoyalBlue;
                textEdit1.Text = Globals.Tags[Globals.GetTagIndex(ofd.FileName)];
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            // Find the Tabs
            XtraTabControl tabs = null;
            object parent = this.Parent;
            while (tabs == null)
            {
                if (parent.GetType() == typeof(XtraTabControl))
                    tabs = (XtraTabControl)parent;
                else
                    parent = ((Control)parent).Parent;
            }
            
            // Check if it Exists
            if (File.Exists(Global.Globals.p.Directory + textEdit1.Text))
            {
                GrimTabPage tab = new GrimTabPage(Global.Globals.p.Directory + textEdit1.Text, tabs);
                tabs.TabPages.Add(tab);
                tabs.SelectedTabPageIndex = tabs.TabPages.Count - 1;
            }
        }

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