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;

namespace Administration
{
    public partial class SpaceTabPage : UserControl
    {
        private List<PointRow> pointRowList_;

        public SpaceTabPage()
        {
            pointRowList_ = new List<PointRow>();

            InitializeComponent();

            RefreshButttons();
        }

        bool showAddButton_;

        private void OnRowRemove(PointRow removedRow)
        {
            pointsTableLayout.Controls.Remove(removedRow);
            pointRowList_.Remove(removedRow);
        }

        private void RefreshButttons()
        {
            if (showAddButton_)
            {
                addRowButton.Visible = true;
            }
            else
            {
                addRowButton.Visible = false;
            }
        }

        public void AddPointRow(bool isRemovable)
        {
            this.SuspendLayout();

            PointRow createdRow = new PointRow();

            if (isRemovable)
            {
                createdRow.ShowRemoveButton = isRemovable;
                createdRow.RemoveRowEvent += new PointRow.RemoveRowDelegate(OnRowRemove);
            }

            createdRow.AutoSize = true;
            createdRow.Name = "PointRow " + pointRowList_.Count;
            createdRow.Size = new System.Drawing.Size(257, 25);
            createdRow.TabIndex = pointRowList_.Count;

            pointsTableLayout.SuspendLayout();
            pointsTableLayout.Controls.Add(createdRow);
            pointsTableLayout.ResumeLayout();

            this.ResumeLayout();

            pointRowList_.Add(createdRow);
        }

        private void addRowButton_Click(object sender, EventArgs e)
        {
            AddPointRow(true);
        }

        private void confirmButton_Click(object sender, EventArgs e)
        {
            bool isValid = true;

            foreach (PointRow row in pointRowList_)
            {
                if (!row.IsValidated)
                {
                    row.validateAll();
                }

                if (!row.IsValid)
                {
                    isValid = false;
                }
            }

            if (isValid)
            {
                ExpandSpaceRequest request = new ExpandSpaceRequest();
                Logic.Communicator.SendMessage(request);
            }
        }

        public bool ShowAddButton
        {
            get
            {
                return showAddButton_;
            }

            set
            {
                showAddButton_ = value;
                RefreshButttons();
            }
        }
    }
}
