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

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

        public CountriesAddPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(countriesAddHeading);
            DefaultScreen.initializePanel(CountriesAddPagePanel);
            countriesAddHeading.Text = Properties.Resources.CountriesAddHeading;
        }

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

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

        private void AddCountrySaveButton_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.AddCountry(newCountryNameTextbox.Text, newCountryDescriptionRichTextBox.Text);
                if (status == 1)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryAddedSuccessfully;
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.CountryAddedSuccessfully, Properties.Resources.CountryAddedSuccessfullyDesc);
                    CountriesShowPage.Instance.GetCountriesRefresh();
                    ClearData();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryNotAdded;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.CountryNotAdded, Properties.Resources.CountryNotAddedDesc);
                }
            }
        }

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

    }
}
