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

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

        public POIAddPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(POIAddHeading);
            DefaultScreen.initializePanel(POIAddPagePanel);
            POIAddHeading.Text = Properties.Resources.POIAddHeading;
            POIAddNameLabel.Text = Properties.Resources.POIAddName;
            POIAddDestinationLabel.Text = Properties.Resources.POIAddDestination;
            POIAddPOITypeLabel.Text = Properties.Resources.POIAddPOIType;
            POIAddStreetNameLabel.Text = Properties.Resources.POIAddStreetName;
            POIAddStreetNumberLabel.Text = Properties.Resources.POIAddStreetNumber;
            POIAddPostcodeLabel.Text = Properties.Resources.POIAddPostcode;
            POIAddMapLatitudeLabel.Text = Properties.Resources.POIAddMapLatitude;
            POIAddMapLongtitudeLabel.Text = Properties.Resources.POIAddMapLongtitude;
            POIAddDescriptionLabel.Text = Properties.Resources.POIAddDescription;

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

            List<string> dataToPOITypeCombobox = GeneratePOIList();
            POITypeCombobox.DataSource = dataToPOITypeCombobox;
        }

        private void ClearData()
        {
            POIAddNameTextbox.Text = "";
            POIAddStreenNameTextbox.Text = "";
            POIAddStreetNumberTextbox.Text = "";
            POIAddPostcodeTextbox.Text = "";
            POIAddMapLatitudeTextbox.Text = "";
            POIAddMapLongtitudeTextbox.Text = "";
            POIAddDescriptionRichTextBox.Text = "";

            ClearGlobalData();
        }

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

        private void AddPOISaveButton_Click(object sender, EventArgs e)
        {
            bool state = POIValidatedFields(DestinationOfPOIComboBox, POITypeCombobox, POIAddNameTextbox, POIAddStreenNameTextbox, POIAddStreetNumberTextbox, POIAddPostcodeTextbox, POIAddMapLatitudeTextbox, POIAddMapLongtitudeTextbox, POIAddDescriptionRichTextBox);

            if (state)
            {
                string selectedCombobox = DestinationOfPOIComboBox.Text;
                string[] ComboBoxSplit = selectedCombobox.Split('-');
                string selectedPOITypeCombobox = POITypeCombobox.Text;
                string[] POITypeComboboxSplit = selectedPOITypeCombobox.Split('-');

                int status = POILayout.AddPOI(Convert.ToInt16(ComboBoxSplit[0]), Convert.ToInt16(POITypeComboboxSplit[0]), POIAddNameTextbox.Text, POIAddStreenNameTextbox.Text, POIAddStreetNumberTextbox.Text, POIAddPostcodeTextbox.Text, POIAddMapLatitudeTextbox.Text, POIAddMapLongtitudeTextbox.Text, POIAddDescriptionRichTextBox.Text);
                if (status == 1)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIAddedSuccessfully;
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.POIAddedSuccessfully, Properties.Resources.POIAddedSuccessfullyDesc);
                    POIShowPage.Instance.GetPOI();
                    ClearData();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POINotAdded;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.POINotAdded, Properties.Resources.POINotAddedDesc);
                }
            }
        }

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