using System;
using System.Windows.Forms;

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

        private CountriesShowPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(countriesShowHeading);
            DefaultScreen.initializePanel(CountriesShowPagePanel);
            DefaultScreen.initializeAddButton(CountryShowPageAddCountry);
            countriesShowHeading.Text = Properties.Resources.CountriesHeading;

            GetCountriesRefresh();
        }

        public void GetCountriesRefresh()
        {
            CountriesShowPagePanel.Controls.Clear();
            CountriesShowPagePanel.Controls.Add(dataGridView1);

            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = 2;
            dataGridView1.Columns[0].Name = "Country ID";
            dataGridView1.Columns[1].Name = "Country Name";
            string[] row = new string[2];

            CountriesLayout.Countries[] allCountries = null;
            allCountries = CountriesLayout.GetCountriesFromDB();

            if (allCountries != null)
            {
                int count = 1;
                foreach (CountriesLayout.Countries country in allCountries)
                {
                    row = new string[] { country.Id.ToString(), country.Name };
                    dataGridView1.Rows.Add(row);
                    count++;
                }
            }

            DataGridViewButtonColumn editButton = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(editButton);
            editButton.HeaderText = "";
            editButton.Text = "Edit";
            editButton.Name = "Edit";
            editButton.UseColumnTextForButtonValue = true;

            DataGridViewButtonColumn deleteButton = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(deleteButton);
            deleteButton.HeaderText = "";
            deleteButton.Text = "Delete";
            deleteButton.Name = "Delete";
            deleteButton.UseColumnTextForButtonValue = true;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex>=0)
            {
                int countryId = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                string countryName = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();

                //EDIT
                if (e.ColumnIndex == 2)
                {
                    EditButtonClicked(countryId, countryName);
                }

                //DELETE
                if (e.ColumnIndex == 3)
                {
                    DeleteButtonClicked(countryId);
                }
            }
        }
        
        private void EditButtonClicked(int id, string name)
        {
            globalCountryId = id;
            globalCountryName = name;
            globalCountryDescription = CountriesLayout.GetCountryDescription(id);

            DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
            DefaultScreenForm.ChangePage.Invoke("EditCountry");
        } 

        private void DeleteButtonClicked(int id)
        {
            int status = DestinationsLayout.GetDestinationsByCountryID(id);
            if (status != 0)
            {
                MessageBox.Show(Properties.Resources.CountryCanNotBeDeletedWithNumber+": "+status);
            }
            else
            {
                DialogResult confirmationOfDeletion = MessageBox.Show(Properties.Resources.ConfirmCountryDeletionDescription, Properties.Resources.ConfirmCountryDeletionTitle, MessageBoxButtons.YesNo);
                if (confirmationOfDeletion == DialogResult.Yes)
                {
                    int deleted = CountriesLayout.DeleteCountry(id);
                    if (deleted == 1)
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryDeletedSuccessfully;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.CountryDeletedSuccessfully, Properties.Resources.CountryDeletedSuccessfullyDesc);
                        CountriesShowPage.Instance.GetCountriesRefresh();
                    }
                    else
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.CountryNotDeleted;
                        DefaultScreen.ShowStatusPage(0, Properties.Resources.CountryNotDeleted, Properties.Resources.CountryNotDeletedDesc);
                    }
                }
            }
        }

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