using System;
using System.Windows.Forms;

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

        public POIShowPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(POIShowHead);
            DefaultScreen.initializePanel(POIShowPagePanel);
            DefaultScreen.initializeAddButton(POIShowPageAddPOI);
            POIShowHead.Text = Properties.Resources.POIHeading;
            dataGridView1.Dock = DockStyle.Fill;

            GetPOI();
        }

        public void GetPOI()
        {
            POIShowPagePanel.Controls.Clear();
            POIShowPagePanel.Controls.Add(dataGridView1);

            POILayout.POI[] allPOI = null;
            allPOI = POILayout.GetPOIFromDB();

            dataGridView1.Rows.Clear();
            dataGridView1.ColumnCount = 5;
            dataGridView1.Columns[0].Name = "POI ID";
            dataGridView1.Columns[0].Width = 40;
            dataGridView1.Columns[1].Name = "Country Name";
            dataGridView1.Columns[1].Width = 90;
            dataGridView1.Columns[2].Name = "Destination Name";
            dataGridView1.Columns[2].Width = 90;
            dataGridView1.Columns[3].Name = "POI Type";
            dataGridView1.Columns[3].Width = 90;
            dataGridView1.Columns[4].Name = "POI Name";
            dataGridView1.Columns[4].Width = 130; 
            string[] row = new string[5];


            if (allPOI != null)
            {
                int count = 1;
                foreach (POILayout.POI POI in allPOI)
                {
                    row = new string[] { POI.Id.ToString(), POI.CountryName, POI.DestinationName, POI.POITypeName, POI.Name };
                    dataGridView1.Rows.Add(row);
                    count++;
                }
            }

            DataGridViewButtonColumn showButton = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(showButton);
            showButton.Width = 60;
            showButton.HeaderText = "";
            showButton.Text = "Show";
            showButton.Name = "Show";
            showButton.UseColumnTextForButtonValue = true;

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

            DataGridViewButtonColumn deleteButton = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(deleteButton);
            deleteButton.Width = 60;
            deleteButton.HeaderText = "";
            deleteButton.Text = "Delete";
            deleteButton.Name = "Delete";
            deleteButton.UseColumnTextForButtonValue = true;
        }
       
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                int POIId = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
                string destinationName = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
                string POIName = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();

                //SHOW
                if (e.ColumnIndex == 5)
                {
                    ShowButtonClicked(POIId);
                }

                //EDIT
                if (e.ColumnIndex == 6)
                {
                    EditButtonClicked(POIId);
                }

                //DELETE
                if (e.ColumnIndex == 7)
                {
                    DeleteButtonClicked(POIId);
                }
            }
        }
        private void ShowButtonClicked(int id)
        {
            globalPOIId = id;
            DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
            DefaultScreenForm.ChangePage.Invoke("ShowPOI");
        }

        private void EditButtonClicked(int id)
        {
            globalPOIId = id;
            DefaultScreen DefaultScreenForm = (DefaultScreen)Application.OpenForms["DefaultScreen"];
            DefaultScreenForm.ChangePage.Invoke("EditPOI");
        }

        private void DeleteButtonClicked(int id)
        {
                DialogResult confirmationOfDeletion = MessageBox.Show(Properties.Resources.ConfirmPOIDeletionDescription, Properties.Resources.ConfirmPOIDeletionTitle, MessageBoxButtons.YesNo);
                if (confirmationOfDeletion == DialogResult.Yes)
                {
                    int deleted = POILayout.DeletePOI(id);
                    if (deleted == 1)
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIDeletedSuccessfully;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.POIDeletedSuccessfully, Properties.Resources.POIDeletedSuccessfullyDesc);
                        POIShowPage.Instance.GetPOI();
                    }
                    else
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POINotDeleted;
                        DefaultScreen.ShowStatusPage(0, Properties.Resources.POINotDeleted, Properties.Resources.POINotDeletedDesc);
                    }
                }
        }

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