using System;
using System.Drawing;
using System.Windows.Forms;

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

        public CountriesEditPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(countriesEditHeading);
            DefaultScreen.initializePanel(CountriesEditPagePanel);
            countriesEditHeading.Text = Properties.Resources.CountriesEditHeading;
            CountryEditCurrentCountryLabel.Text = Properties.Resources.CountryEditCurrentCountryLabel;
            CountryEditNewCountryName.Text = Properties.Resources.CountryEditNewCountryName;
            CountryEditNewCountryDescription.Text = Properties.Resources.CountryEditNewCountryDestination;
        }

        private void ClearData()
        {
            ClearGlobalData();
            CountryEditCurrentCountryNameFill.Text = "";
            newCountryNameTextbox.Text = "";
            newCountryDescriptionRichTextBox.Text = "";
        }

        public void FillData()
        {
            CountryEditCurrentCountryNameFill.Text = globalCountryName;
            newCountryNameTextbox.Text = globalCountryName;
            newCountryDescriptionRichTextBox.Text = globalCountryDescription;
        }

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

        private void EditCountrySaveButton_Click(object sender, EventArgs e)
        {
            newCountryNameTextbox.BackColor = default(Color);

            if (newCountryNameTextbox.Text == null || newCountryNameTextbox.Text == "" || newCountryNameTextbox.Text == " ")
            {
                newCountryNameTextbox.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryNameNull;
            }
            else
            {
                int status = CountriesLayout.EditCountry(globalCountryId, newCountryNameTextbox.Text, newCountryDescriptionRichTextBox.Text);
                if (status==1)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryEditedSuccessfully;
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.CountryEditedSuccessfully, Properties.Resources.CountryEditedSuccessfullyDesc);
                    CountriesShowPage.Instance.GetCountriesRefresh();
                    DestinationsShowPage.Instance.GetDestinations();
                    ClearData();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryNotEdited;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.CountryNotEdited, Properties.Resources.CountryNotEditedDesc);
                }
            }
        }

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