using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

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

        public POIEditPage()
        {
            InitializeComponent();
            
            DefaultScreen.initializeHeading(POIEditHeading);
            DefaultScreen.initializePanel(POIEditPagePanel);
            POIEditHeading.Text = Properties.Resources.POIHeading;
            POIEditNameLabel.Text = Properties.Resources.POIEditName;
            POIEditDestinationLabel.Text = Properties.Resources.POIEditDestination;
            POIEditTypeLabel.Text = Properties.Resources.POIEditType;
            POIEditStreetNameLabel.Text = Properties.Resources.POIEditStreetName;
            POIEditStreetNumberLabel.Text = Properties.Resources.POIEditStreetNumber;
            POIEditPostcodeLabel.Text = Properties.Resources.POIEditPostcode;
            POIEditMapLatitudeLabel.Text = Properties.Resources.POIEditMapLatitude;
            POIEditMapLongtitudeLabel.Text = Properties.Resources.POIEditMapLongtitude;
            POIEditDescriptionLabel.Text = Properties.Resources.POIEditDescription;
            
            FillData();
        }

        public void FillData()
        {
            
            POIEditNameTextbox.Text = "";
            POIEditStreenNameTextbox.Text = "";
            POIEditStreetNumberTextbox.Text = "";
            POIEditPostcodeTextbox.Text = "";
            POIEditMapLatitudeTextbox.Text = "";
            POIEditMapLongtitudeTextbox.Text = "";
            POIEditDescriptionRichTextBox.Text = "";

            /*
             * List<string> dataToDestinationCombobox = GenerateDestinationsList();
            DestinationOfPOIComboBox.DataSource = dataToDestinationCombobox;

            List<string> dataToPOITypeCombobox = GeneratePOIList();
            POITypeCombobox.DataSource = dataToPOITypeCombobox;
            */
            POILayout.POI[] poiData = null;
            poiData = POILayout.GetPOIFromDbById(globalPOIId);

            int destinationId = 0;
            string poiDestinationName = "";
            int poiTypeId = 0;
            string poiTypeName = "";
           if (poiData != null)
           {
               foreach (POILayout.POI poi in poiData)
               {

                   POIEditNameTextbox.Text = poi.Name;
                   POIEditStreenNameTextbox.Text = poi.Street;
                   POIEditStreetNumberTextbox.Text = poi.StreetNumber;
                   POIEditPostcodeTextbox.Text = poi.Postcode.ToString();
                   POIEditMapLatitudeTextbox.Text = poi.Latitude.ToString();
                   POIEditMapLongtitudeTextbox.Text = poi.Longtitude.ToString();
                   POIEditDescriptionRichTextBox.Text = poi.Description;
                   destinationId = poi.DestinationId;
                   poiDestinationName = poi.DestinationName;
                   poiTypeId = poi.POITypeId;
                   poiTypeName = poi.POITypeName;
               }
           }

            List<string> dataToDestinationCombobox = GenerateDestinationsList();
            EditDestinationOfPOIComboBox.DataSource = dataToDestinationCombobox;
            EditDestinationOfPOIComboBox.SelectedIndex = EditDestinationOfPOIComboBox.FindStringExact(destinationId + "-" + poiDestinationName);

            List<string> dataToPOITypeCombobox = GeneratePOIList();
            POIEdiTtypeCombobox.DataSource = dataToPOITypeCombobox;
            POIEdiTtypeCombobox.SelectedIndex = POIEdiTtypeCombobox.FindStringExact(poiTypeId + "-" + poiTypeName);
        }

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

        private void EditPOISaveButton_Click(object sender, EventArgs e)
        {
            
            bool state = POIValidatedFields(EditDestinationOfPOIComboBox, POIEdiTtypeCombobox, POIEditNameTextbox, POIEditStreenNameTextbox, POIEditStreetNumberTextbox, POIEditPostcodeTextbox, POIEditMapLatitudeTextbox, POIEditMapLongtitudeTextbox, POIEditDescriptionRichTextBox);
            
            if (state)
            {
                string selectedDestinationCombobox = EditDestinationOfPOIComboBox.Text;
                string[] ComboBoxDestinationSplit = selectedDestinationCombobox.Split('-');

                string selectedPOITypeCombobox = POIEdiTtypeCombobox.Text;
                string[] ComboBoxPOITypeSplit = selectedPOITypeCombobox.Split('-');

                int status = POILayout.EditPOI(globalPOIId, Convert.ToInt16(ComboBoxDestinationSplit[0]), Convert.ToInt16(ComboBoxPOITypeSplit[0]), POIEditNameTextbox.Text, POIEditStreenNameTextbox.Text, POIEditStreetNumberTextbox.Text, POIEditPostcodeTextbox.Text, POIEditMapLatitudeTextbox.Text, POIEditMapLongtitudeTextbox.Text, POIEditDescriptionRichTextBox.Text);

                if (status == 1)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIEditedSuccessfully;
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.POIEditedSuccessfully, Properties.Resources.POIEditedSuccessfullyDesc);
                    RestaurantsShowPage.Instance.GetRestaurants();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POINotEdited;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.POINotEdited, Properties.Resources.POINotEditedDesc);
                }
            }
            
        }

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

    }
}
