using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using Global;
using System.Diagnostics;

namespace HaloControls
{
    public class GrimTabPage : XtraTabPage
    {
        /// <summary>
        /// Gets the Default Tag Class of the Tag associated with this Tab
        /// </summary>
        public string TagClass { get { return plugin.Layout.DefaultExtension; } }

        private bool saved = true;
        /// <summary>
        /// Gets the Saved Status of the Tab
        /// </summary>
        public bool Saved { get { return saved; } }

        private Plugin plugin;
        /// <summary>
        /// Gets the Plugin the Tab is using
        /// </summary>
        public Plugin Plugin { get { return plugin; } }

        private string path = "";
        /// <summary>
        /// Gets the Location of the Tag
        /// </summary>
        public string Path { get { return path; } }

        private XtraTabControl tabs;

        public GrimTabPage(string Class, bool IsNew, XtraTabControl Tabs)
        {
            //Suspend Layout
            this.SuspendLayout();

            // Set Fields
            tabs = Tabs;
            if (IsNew) saved = false;

            // Load Plugin
            plugin = new Plugin(Class, tabs);

            // Create Panel
            Panel panel = new Panel();
            panel.SuspendLayout();
            panel.Dock = DockStyle.Fill;
            panel.AutoScroll = true;
            panel.BackColor = Color.FromKnownColor(KnownColor.Control);

            // Load the Controls
            plugin.Load(ref panel, null);

            // Update Tab
            if (saved)
                this.Text = "New_Tag." + TagClass;
            else
                this.Text = "New_Tag." + TagClass + "*";
            panel.ResumeLayout();
            this.Controls.Add(panel);

            //Resume
            this.ResumeLayout();
        }

        public GrimTabPage(string Location, XtraTabControl Tabs)
        {
            //Suspend Layout
            this.SuspendLayout();

            // Set Fields
            tabs = Tabs;

            // Load Plugin
            try { plugin = new Plugin(Location.Substring(Location.LastIndexOf(".") + 1), tabs); }
            catch
            {
                MessageBox.Show("Invalid Tag!");
                return;
            }

            // Double Check
            if (plugin == null)
            {
                MessageBox.Show("Invalid Tag!");
                return;
            }

            // Create Panel
            Panel panel = new Panel();
            panel.SuspendLayout();
            panel.Dock = DockStyle.Fill;
            panel.AutoScroll = true;
            panel.BackColor = Color.FromKnownColor(KnownColor.Control);

            // Load the Controls
            BinaryReader br = new BinaryReader(new FileStream(Location, FileMode.Open, FileAccess.Read, FileShare.Read));
            plugin.Load(ref panel, br);
            br.Close();

            // Update Tab
            this.Text = Location.Substring(Location.LastIndexOf("\\") + 1);
            panel.ResumeLayout();
            this.Controls.Add(panel);

            //Resume
            this.ResumeLayout();
        }

        protected override void Dispose(bool disposing)
        {
            // Check to see if it's saved
            if (!saved)
            {
                if (MessageBox.Show("Would you like to save changes to: " + this.Text + "?", "Mutation", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    Save();
                }
            }

            base.Dispose(disposing);
        }

        public void Save()
        {
            // Double Check Path
            if (!File.Exists(path))
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter = "Halo 2 Tags (*." + TagClass + "|*." + TagClass;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    path = sfd.FileName;
                    Globals.Tags.Add(path);
                }
            }

            // Write
            Panel p = (Panel)this.Controls[0];
            plugin.Write(path, ref p);
            MessageBox.Show("Done!");
        }
    }
}
