using System;
using System.Windows.Forms;

namespace AgencySystem
{
    public partial class DestinationsShowPage : DataMethods
    {
        private static DestinationsShowPage _instance;
        public static DestinationsShowPage Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DestinationsShowPage();
                return _instance;
            }
        }
        public DestinationsShowPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(destinationsShowHeading);
            DefaultScreen.initializePanel(DestinationsShowPagePanel);
            DefaultScreen.initializeAddButton(DestiantionShowPageAddDestination);
            destinationsShowHeading.Text = Properties.Resources.DestinationsHeading;

            GetDestinations();
        }

        public void GetDestinations()
        {
            DestinationsShowPagePanel.Controls.Clear();
            DestinationsShowPagePanel.Controls.Add(dataGridView1);

            DestinationsLayout.Destinations[] allDestinations = null;
            allDestinations = DestinationsLayout.GetDestinationsFromDB();

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


            if (allDestinations != null)
            {
                int count = 1;
                foreach (DestinationsLayout.Destinations destination in allDestinations)
                {
                    row = new string[] { destination.Id.ToString(), destination.CountryName, destination.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 destinationId = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                string countryName = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                string destinationName = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

                //EDIT
                if (e.ColumnIndex == 3)
                {
                    EditButtonClicked(destinationId, destinationName, countryName);
                }

                //DELETE
                if (e.ColumnIndex == 4)
                {
                    DeleteButtonClicked(destinationId);
                }
            }
        }

        private void EditButtonClicked(int id, string name, string countryName)
        {
            globalDestinationId = id;
            globalDestinationName = name;
            globalCountryId = DestinationsLayout.GetCountryIdByDestinationId(id);
            globalCountryName = countryName;

            DestinationsLayout.Destinations[] destinationData = null;
            destinationData = DestinationsLayout.GetDestinationDataFromDbById(id);

            if (destinationData != null)
            {
                foreach (DestinationsLayout.Destinations destination in destinationData)
                {
                    globalDestinationDescription = destination.Description;
                    globalDestinationRecommended = destination.Recommended;
                }
            }

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

        private void DeleteButtonClicked(int id)
        {
            int status = DestinationsLayout.GetAllByDestinationID(id);
            if (status != 0)
            {
                MessageBox.Show(Properties.Resources.DestinationCanNotBeDeletedWithNumber + ": " + status);
            }
            else
            {
                DialogResult confirmationOfDeletion = MessageBox.Show(Properties.Resources.ConfirmDestinationDeletionDescription, Properties.Resources.ConfirmDestinationDeletionTitle, MessageBoxButtons.YesNo);
                if (confirmationOfDeletion == DialogResult.Yes)
                {
                    int deleted = DestinationsLayout.DeleteDestination(id);
                    if (deleted == 1)
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationDeletedSuccessfully;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.DestinationDeletedSuccessfully, Properties.Resources.DestinationDeletedSuccessfullyDesc);
                        DestinationsShowPage.Instance.GetDestinations();
                    }
                    else
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationNotDeleted;
                        DefaultScreen.ShowStatusPage(0, Properties.Resources.DestinationNotDeleted, Properties.Resources.DestinationNotDeletedDesc);
                    }
                }
            }
        }

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