//
// $HeadURL: https://danbala.squirrel.local/repository/SystemProperties/trunk/SystemProperties/EnvironmentVariables/VariableDetail.cs $
//
// $Author: joerg $
// $Date: 2006-02-19 22:18:49 +0100 (So, 19 Feb 2006) $
// $Revision: 6 $
//
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// http://www.gnu.org/copyleft/gpl.html
//

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

namespace Finalize.SystemProperties.GUI.EnvironmentVariables
{
    public partial class VariableDetail : Form
    {
        private static readonly string TITLE = "Variable: ";
        private string[] _lbEntries;
        private TextBox _listEditBox;
        private int _selectedListItem;

        public VariableDetail()
        {
            InitializeComponent();

            _selectedListItem = -1;
            _listEditBox = new TextBox();
            _listEditBox.Location = new Point(0, 0);
            _listEditBox.Size = new Size(0, 0);
            _listEditBox.Hide();
            _lbVariableValue.Controls.AddRange(new Control[] { _listEditBox });
            _listEditBox.Text = "";
            _listEditBox.LostFocus += new EventHandler(FocusOver);
            _listEditBox.BackColor = Color.White;
            _listEditBox.Font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular, GraphicsUnit.Pixel);
            _listEditBox.ForeColor = Color.Black;
            _listEditBox.BorderStyle = BorderStyle.FixedSingle;

            _lbVariableValue.KeyDown += new KeyEventHandler(ListEditBox_KeyDown);
            _lbVariableValue.DoubleClick += new EventHandler(ListEditBox_DoubleClick);

        }

        public VariableDetail(bool newVariable)
            : this()
        {
            if (newVariable)
            {
                _textVariableName.ReadOnly = false;
            }
            else
            {
                _textVariableName.ReadOnly = true;
            }
        }

        public string Variable
        {
            get
            {
                return _textVariableName.Text.Trim();
            }

            set
            {
                _textVariableName.Text = value;
            }
        }

        public string Value
        {
            get
            {
                return _textVariableValue.Text.Trim();
            }

            set
            {
                _textVariableValue.Text = value;
                UpdateListView(this, null);
            }
        }

        private void UpdateValue()
        {
            StringBuilder variable = new StringBuilder();
            for (int i = 0; i < _lbEntries.Length; i++)
            {
                variable.Append(_lbEntries[i]);
                if (i < _lbEntries.Length - 1)
                {
                    variable.Append(_textSeparator.Text);
                }
            }

            Value = variable.ToString();
        }

        private void UpdateListView(object sender, EventArgs e)
        {
            char[] separatorChar = _textSeparator.Text.ToCharArray();
            if (separatorChar.Length == 0)
            {
                separatorChar = new char[] { ';' };
            }
            _lbEntries = Value.Split(separatorChar, StringSplitOptions.None);
            _lbVariableValue.DataSource = _lbEntries;
        }

        private void MoveSelectedUp(object sender, EventArgs e)
        {
            int index = _lbVariableValue.SelectedIndex;
            if (index > 0)
            {
                int newIndex = index - 1;
                string value = _lbEntries[index];
                string valuePrevious = _lbEntries[newIndex];

                _lbEntries[newIndex] = value;
                _lbEntries[index] = valuePrevious;

                UpdateValue();
                _lbVariableValue.DataSource = _lbEntries;
                _lbVariableValue.SelectedIndex = newIndex;
            }
        }

        private void MoveSelectedDown(object sender, EventArgs e)
        {
            int index = _lbVariableValue.SelectedIndex;
            if (index < _lbEntries.Length - 1)
            {
                int newIndex = index + 1;
                string value = _lbEntries[index];
                string valuePrevious = _lbEntries[newIndex];

                _lbEntries[newIndex] = value;
                _lbEntries[index] = valuePrevious;

                UpdateValue();
                _lbVariableValue.DataSource = _lbEntries;
                _lbVariableValue.SelectedIndex = newIndex;
            }
        }

        private void NewEntry(object sender, EventArgs e)
        {
            string[] entries = new string[_lbEntries.Length + 1];
            for (int i = 0; i < _lbEntries.Length; i++)
            {
                entries[i] = _lbEntries[i];
            }

            _lbEntries = entries;
            UpdateValue();
            _lbVariableValue.SelectedIndex = _lbEntries.Length - 1;
        }

        private void DeleteEntry(object sender, EventArgs e)
        {
            int index = _lbVariableValue.SelectedIndex;
            string[] entries = new string[_lbEntries.Length - 1];
            for (int i = 0; i < _lbEntries.Length; i++)
            {
                if (i < index)
                {
                    entries[i] = _lbEntries[i];
                }
                else if (i > index)
                {
                    entries[i - 1] = _lbEntries[i];
                }
            }

            _lbEntries = entries;
            UpdateValue();
            _lbVariableValue.DataSource = _lbEntries;

            if (index > 0)
            {
                _lbVariableValue.SelectedIndex = index - 1;
            }
            else if (index == 0)
            {
                _lbVariableValue.SelectedIndex = 0;
            }
        }

        #region Editable ListBox Item
        private void ListEditBox_DoubleClick(object sender, EventArgs e)
        {
            CreateEditBox();
        }

        private void ListEditBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F2)
            {
                CreateEditBox();
            }
        }


        private void CreateEditBox()
        {
            _selectedListItem = _lbVariableValue.SelectedIndex;
            Rectangle r = _lbVariableValue.GetItemRectangle(_selectedListItem);
            string itemText = (string) _lbVariableValue.Items[_selectedListItem];

            if (_selectedListItem > 0 && _selectedListItem == _lbEntries.Length - 1)
            {
                _listEditBox.Location = new System.Drawing.Point(r.X + 5, r.Y - 15);
            }
            else
            {
                _listEditBox.Location = new System.Drawing.Point(r.X + 5, r.Y + 5);
            }
            _listEditBox.Size = new System.Drawing.Size(r.Width - 10, r.Height - 5);
            _listEditBox.Show();
            _lbVariableValue.Controls.AddRange(new System.Windows.Forms.Control[] { _listEditBox });
            _listEditBox.Text = itemText;
            _listEditBox.Focus();
            _listEditBox.SelectAll();
        }


        private void FocusOver(object sender, System.EventArgs e)
        {
            _lbEntries[_selectedListItem] = _listEditBox.Text;
            _lbVariableValue.DataSource = _lbEntries;
            UpdateValue();
            _listEditBox.Hide();
        }
        #endregion

        private void TabChanged(object sender, EventArgs e)
        {
            if (_tabs.SelectedTab == _tabNormal)
            {
                UpdateValue();
            }
            else if (_tabs.SelectedTab == _tabSeparated)
            {
                UpdateListView(this, null);
            }
        }

        private void SeparatorLostFocus(object sender, EventArgs e)
        {
            if (_textSeparator.Text == null || _textSeparator.Text.Length == 0)
            {
                _textSeparator.Text = ";";
            }
        }

        private void UpdateButtonState(object sender, EventArgs e)
        {
            if (_lbVariableValue.SelectedIndex == 0 && _lbEntries.Length > 1)
            {
                _btnUp.Enabled = false;
                _btnDown.Enabled = true;
            }
            else if (_lbVariableValue.SelectedIndex == 0 && _lbEntries.Length == 1)
            {
                _btnUp.Enabled = false;
                _btnDown.Enabled = false;
            }
            else if (_lbVariableValue.SelectedIndex == _lbEntries.Length - 1)
            {
                _btnUp.Enabled = true;
                _btnDown.Enabled = false;
            }
            else
            {
                _btnUp.Enabled = true;
                _btnDown.Enabled = true;
            }
        }

        private void Accept(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Hide();
        }

        private void Cancel(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Hide();
        }

        private void VariableNameChanged(object sender, EventArgs e)
        {
            Text = TITLE + _textVariableName.Text;
        }
    }
}