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 EditListViewDistances : hukpiWinApp.BaseEditListViewForm
    {
        public EditListViewDistances(ListView parentListView) : base(parentListView)
        {
            
            InitializeComponent();
            
            fillComboBoxCompanyDepots();
        }

        private void fillComboBoxCompanyDepots()
        {
            comboBoxNewCompanyDepot.DataSource = Utils.getCompanyDepotsForCombo();
            comboBoxNewCompanyDepot.DisplayMember = "Description";
            comboBoxNewCompanyDepot.ValueMember = "Id";

        }

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

                clearInputsBoxes();
            }
        }

        private void clearInputsBoxes()
        {
            textBoxDistance.Text = "";
            comboBoxNewCompanyDepot.SelectedValue = 0;
        }

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

        private void textBoxDistance_Validating(object sender, CancelEventArgs e)
        {
            Utils.validateMustHaveDigitsOnly(errorProviderForEditListView, textBoxDistance, true);
        }

        private bool validateForm()
        {
            bool isValidComboNewCompanyDepot = Utils.validateMustNonEmpty(errorProviderForEditListView, comboBoxNewCompanyDepot);
            bool isValidTextBoxNewDistance = Utils.validateMustHaveDigitsOnly(errorProviderForEditListView, textBoxDistance, true);

            return isValidComboNewCompanyDepot && isValidTextBoxNewDistance;
        }
    }
}
