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

namespace hukpiWinApp
{
    public partial class EditListViewContainers : hukpiWinApp.BaseEditListViewForm
    {
        public EditListViewContainers(ListView parentListView) : base(parentListView)
        {
            InitializeComponent();

            fillComboBoxContainers();
        }

        private void fillComboBoxContainers()
        {
            comboBoxNewContainer.DataSource = Utils.getContainersForCombo();
            comboBoxNewContainer.DisplayMember = "Description";
            comboBoxNewContainer.ValueMember = "Id";
        }

        private void clearInputsBoxes()
        {
            textBoxContainerNumber.Text = "";
            comboBoxNewContainer.SelectedValue = 0;
        }

        private void buttonAddNewContainer_Click(object sender, EventArgs e)
        {
            if (!validateForm())
            {
                MessageBox.Show(ErrorMessages.Default.invalidFormInputData);
            }
            else
            {
                ListViewItem newItem = new ListViewItem(comboBoxNewContainer.Text.Split('-')[0]);
                ListViewItem.ListViewSubItem subItem = new ListViewItem.ListViewSubItem(newItem, textBoxContainerNumber.Text);
                subItem.Tag = comboBoxNewContainer.SelectedValue;
                newItem.SubItems.Add(subItem);
                this.listViewData.Items.Add(newItem);

                clearInputsBoxes();
            }
        }

        private void comboBoxNewContainer_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderForEditListView, comboBoxNewContainer);
        }

        private void textBoxContainerNumber_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustNonEmpty(errorProviderForEditListView, textBoxContainerNumber);
        }

        private bool validateForm()
        {
            bool isValidComboNewContainer = Utils.validateMustNonEmpty(errorProviderForEditListView, comboBoxNewContainer);
            bool isValidTextBoxNewContainerNumber = Utils.validateMustHaveDigitsOnly(errorProviderForEditListView, textBoxContainerNumber, true);

            return isValidComboNewContainer && isValidTextBoxNewContainerNumber;
        }
    }
}
