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

using Finalize.SystemProperties.Data;

namespace Finalize.SystemProperties.GUI.EnvironmentVariables
{
    public partial class TabEnvironmentVariables : UserControl
    {
        public TabEnvironmentVariables()
        {
            InitializeComponent();

            UpdateUserVariables();
            UpdateSystemVariables();
            UpdateSaveButtonState();
        }

        private void UpdateSystemVariables()
        {
            DataView systemVariables = new DataView(EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables);
            systemVariables.Sort = EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables.KeyColumn.ColumnName;
            _systemVariablesDataSource.DataSource = systemVariables;
        }

        private void UpdateUserVariables()
        {
            DataView userVariables = new DataView(EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables);
            userVariables.Sort = EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables.KeyColumn.ColumnName;
            _userVariablesDataSource.DataSource = userVariables;
        }

        private void UpdateSaveButtonState()
        {
            if (EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables.GetChanges() != null ||
                EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables.GetChanges() != null)
            {
                _btnSave.Enabled = true;
                _btnRevert.Enabled = true;
            }
            else
            {
                _btnSave.Enabled = false;
                _btnRevert.Enabled = false;
            }
        }

        #region User Variable Events
        private void NewUserVariable(object sender, EventArgs e)
        {
            VariableDetail details = new VariableDetail(true);
            if (ShowAddNewVariable(details))
            {
                EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables.AddEnvorinmentVariablesRow(details.Variable, details.Value);
                UpdateUserVariables();
                UpdateSaveButtonState();
            }
        }

        private void EditUserVariable(object sender, EventArgs e)
        {
            EditVariableDetails((DataRowView) _dgUserVariables.SelectedRows[0].DataBoundItem,
                                EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables);
            UpdateUserVariables();
            UpdateSaveButtonState();
        }

        private void DeleteUserVariable(object sender, EventArgs e)
        {
            DeleteVariable((DataRowView) _dgUserVariables.SelectedRows[0].DataBoundItem,
                           EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables);
            UpdateUserVariables();
            UpdateSaveButtonState();
        }
        #endregion

        #region System Variable Events
        private void NewSystemVariable(object sender, EventArgs e)
        {
            VariableDetail details = new VariableDetail(true);
            if (ShowAddNewVariable(details))
            {
                EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables.AddEnvorinmentVariablesRow(details.Variable, details.Value);
                UpdateSystemVariables();
                UpdateSaveButtonState();
            }
        }

        private void EditSystemVariable(object sender, EventArgs e)
        {
            EditVariableDetails((DataRowView) _dgSystemVariables.SelectedRows[0].DataBoundItem,
                                EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables);
            UpdateSystemVariables();
            UpdateSaveButtonState();
        }

        private void DeleteSystemVariable(object sender, EventArgs e)
        {
            DeleteVariable((DataRowView) _dgSystemVariables.SelectedRows[0].DataBoundItem,
                           EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables);
            UpdateSystemVariables();
            UpdateSaveButtonState();
        }
        #endregion

        private void Save(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            EnvironmentVariableManager.Instance.SaveChanges();
            UpdateSaveButtonState();
            Cursor = Cursors.Default;
        }

        private void RevertChanges(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show(this, "Do you really want to revert the changes?",
                                                   "Revert changes?", MessageBoxButtons.OKCancel,
                                                   MessageBoxIcon.Question))
            {
                if (EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables.GetChanges() != null)
                {
                    EnvironmentVariableManager.Instance.UserVariables.EnvorinmentVariables.RejectChanges();
                    UpdateUserVariables();
                }
                else if (EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables.GetChanges() != null)
                {
                    EnvironmentVariableManager.Instance.SystemVariables.EnvorinmentVariables.RejectChanges();
                    UpdateSystemVariables();
                }
                UpdateSaveButtonState();
            }
        }

        private bool ShowAddNewVariable(VariableDetail details)
        {
            if (DialogResult.OK == details.ShowDialog(this))
            {
                if (details.Variable != null && details.Variable.Length > 0)
                {
                    return true;
                }
                else
                {
                    if (DialogResult.OK == MessageBox.Show(this, "The name of the new variable have to be entered.\n\n" +
                                                               "Do you want to cancel?", "Variablename is missing!", MessageBoxButtons.OKCancel,
                                                               MessageBoxIcon.Question))
                    {
                        return false;
                    }
                    else
                    {
                        return ShowAddNewVariable(details);
                    }
                }
            }
            else
            {
                return false;
            }
        }

        private void EditVariableDetails(DataRowView item, Data.EnvironmentVariables.EnvorinmentVariablesDataTable variables)
        {
            VariableDetail details = new VariableDetail(false);
            details.Variable = (string) item.Row.ItemArray[0];
            details.Value = (string) item.Row.ItemArray[1];

            if (DialogResult.OK == details.ShowDialog(this) &&
                !item.Row.ItemArray[1].Equals(details.Value))
            {
                Data.EnvironmentVariables.EnvorinmentVariablesRow[] rows =
                    (Data.EnvironmentVariables.EnvorinmentVariablesRow[]) variables.Select("Key = '" + item.Row.ItemArray[0] + "'");
                if (rows.Length > 0)
                {
                    Console.WriteLine(rows[0].ItemArray[1] + " ~ " + details.Value);
                    rows[0].BeginEdit();
                    rows[0]["Value"] = details.Value;
                    rows[0].EndEdit();
                }
            }
        }

        private void DeleteVariable(DataRowView item, Data.EnvironmentVariables.EnvorinmentVariablesDataTable variables)
        {
            if (DialogResult.OK == MessageBox.Show(this, "Do you really want to delete the variable '" + item.Row.ItemArray[0]
                                                    + "' ?", "Delete the variable '" + item.Row.ItemArray[0] + "'",
                                                    MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                Data.EnvironmentVariables.EnvorinmentVariablesRow[] rows =
                    (Data.EnvironmentVariables.EnvorinmentVariablesRow[]) variables.Select("Key = '" + item.Row.ItemArray[0] + "'");
                if (rows.Length > 0)
                {
                    rows[0].Delete();
                }
            }
        }
    }
}

