using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AgencySystem
{
    public partial class DestinationsAddPage : DataMethods
    {
        private static DestinationsAddPage _instance;
        public static DestinationsAddPage Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DestinationsAddPage();
                return _instance;
            }
        }

        public DestinationsAddPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(DestinationsAddHeading);
            DefaultScreen.initializePanel(DestinationsAddPagePanel);
            DestinationsAddHeading.Text = Properties.Resources.DestinationsAddHeading;
            CountryOfDestinationLabel.Text = Properties.Resources.CountryOfDestinationTitle;
            DestinationsAddNewDestinationDescription.Text = Properties.Resources.DestinationsNewDestinationDescription;
            DestinationsAddNewDestinationName.Text = Properties.Resources.DestinationsNewDestinationName;

            List<string> dataToCombobox = GenerateCountriesList();
            CountryOfDestinationComboBox.DataSource = dataToCombobox;
        }

        private void ClearData()
        {
            newDestinationNameTextbox.Text = "";
            newDestinationDescriptionRichTextBox.Text = "";
            newDestinationRecommendedCheckbox.Checked = false;
            ClearGlobalData();
        }
        
        private void AddDestinationBackButton_Click(object sender, EventArgs e)
        {
            DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
            DefaultScreenForm.ChangePage.Invoke("BackToShowDestinations");
        }

        private void AddDestinationSaveButton_Click(object sender, EventArgs e)
        {
            bool state = DestinationsValidatedFields(CountryOfDestinationComboBox, newDestinationNameTextbox);
            if (state)
            {
                string selectedCombobox = CountryOfDestinationComboBox.Text;
                string[] ComboBoxSplit = selectedCombobox.Split('-');

                int status = DestinationsLayout.AddDestination(newDestinationNameTextbox.Text, newDestinationDescriptionRichTextBox.Text, newDestinationRecommendedCheckbox.Checked, Convert.ToInt16(ComboBoxSplit[0]));
                    if (status == 1)
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationAddedSuccessfully;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.DestinationAddedSuccessfully, Properties.Resources.DestinationAddedSuccessfullyDesc);
                        DestinationsShowPage.Instance.GetDestinations();
                        ClearData();
                    }
                    else
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationNotAdded;
                        DefaultScreen.ShowStatusPage(0, Properties.Resources.DestinationNotAdded, Properties.Resources.DestinationNotAddedDesc);
                    }
            }
        }

        private void DestinationsAddPage_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                AddDestinationSaveButton.PerformClick();
            }
        }

        private void CountryOfDestinationComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            PerformEnterHit(sender, e);
        }

        private void newDestinationNameTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            PerformEnterHit(sender, e);
        }

        private void PerformEnterHit(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                AddDestinationSaveButton.PerformClick();
            }
        }
    }
}
