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

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

        public DestinationsEditPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(DestinationsEditHeading);
            DefaultScreen.initializePanel(DestinationsEditPagePanel);
            DestinationsEditHeading.Text = Properties.Resources.DestinationsEditHeading;
            DestinationEditCurrentDestinationLabel.Text = Properties.Resources.DestinationEditCurrentDestinationLabel;
            DestinationEditCurrentCountryLabel.Text = Properties.Resources.DestinationEditCurrentCountryLabel;
            DestinationEditNewDestinationName.Text = Properties.Resources.DestinationEditNewDestinationName;
            DestinationEditNewCountryName.Text = Properties.Resources.DestinationEditNewCountryName;
            DestinationEditDestinationDescription.Text = Properties.Resources.DestinationEditDescription;
        }

        public void FillData()
        {
            DestinationEditCurrentDestinationNameFill.Text = globalDestinationName;
            DestinationEditCurrentCountryNameFill.Text = globalCountryName;
            editDestinationNewNameTextbox.Text = globalDestinationName;
            editDestinationDescriptionTextBox.Text = globalDestinationDescription;
            editDestinationRecommendedCheckbox.Checked = globalDestinationRecommended;
   
            List<string> dataToCombobox = GenerateCountriesList();
            ChangeCountryNameCombobox.DataSource = dataToCombobox;
            ChangeCountryNameCombobox.SelectedIndex = ChangeCountryNameCombobox.FindStringExact(globalCountryId+"-"+globalCountryName);
        }

        private void EditDestinationBackButton_Click(object sender, EventArgs e)
        {
            DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
            DefaultScreenForm.ChangePage.Invoke("BackToShowDestinations");
        }

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

                int status = DestinationsLayout.EditDestination(globalDestinationId, editDestinationNewNameTextbox.Text, editDestinationDescriptionTextBox.Text, editDestinationRecommendedCheckbox.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();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationNotAdded;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.DestinationNotAdded, Properties.Resources.DestinationNotAddedDesc);
                }
            }
        }

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

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

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