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

namespace AgencySystem
{
    public class DataMethods : UserControl
    {
        protected static int globalCountryId;
        protected static string globalCountryName;
        protected static string globalCountryDescription;
        protected static int globalDestinationId;
        protected static string globalDestinationName;
        protected static string globalDestinationDescription;
        protected static bool globalDestinationRecommended;
        protected static int globalRestaurantId;
        protected static string globalRestaurantName;
        protected static int globalPOIId;
        protected static string globalPOIName;

        protected void ClearGlobalData()
        {
            globalCountryId = 0;
            globalCountryName = "";
            globalCountryDescription = "";
            globalDestinationId = 0;
            globalDestinationName = "";
            globalDestinationDescription = "";
            globalDestinationRecommended = false;
            globalRestaurantId = 0;
            globalRestaurantName = "";
            globalPOIId = 0;
            globalPOIName = "";
        }

        /// <summary>
        /// Method which generates countries to the list from database
        /// </summary>
        /// <returns></returns>
        protected List<string> GenerateCountriesList()
        {
            List<string> list = new List<string>();
            list.Add("-");

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

            if (allCountries != null)
            {
                foreach (CountriesLayout.Countries country in allCountries)
                {
                    string value = null;
                    value += country.Id + "-" + country.Name;
                    list.Add(value);
                }
            }

            return list;
            /*
             FOLLOWING CODE TO COPY LIST TO COMBOBOX
                 List<string> dataToCombobox = GenerateCountriesList();
                 <COMBOBOXNAME>.DataSource = dataToCombobox;
                 <COMBOBOXNAME>.SelectedIndex = <COMBOBOXNAME>.FindStringExact(globalCountryId + "-" + globalCountryName);
             */
        }

        /// <summary>
        /// Method which generates all destinations to the list from database
        /// </summary>
        /// <returns></returns>
        protected List<string> GenerateDestinationsList()
        {
            List<string> list = new List<string>();
            list.Add("-");
            DestinationsLayout.Destinations[] allDestinations = null;
            allDestinations = DestinationsLayout.GetDestinationsFromDB();

            if (allDestinations != null)
            {
                foreach (DestinationsLayout.Destinations destination in allDestinations)
                {
                    string value = null;
                    value += destination.Id + "-" + destination.Name;
                    list.Add(value);
                }
            }

            return list;

            /*
             FOLLOWING CODE TO COPY LIST TO COMBOBOX
                 List<string> dataToCombobox = GenerateDestinationsList();
                 <COMBOBOXNAME>.DataSource = dataToCombobox;
                 <COMBOBOXNAME>.SelectedIndex = <COMBOBOXNAME>.FindStringExact(globalCountryId + "-" + globalCountryName);
             */
        }

        protected List<string> GeneratePOIList()
        {
            List<string> list = new List<string>();
            list.Add("-");
            POILayout.POITypes[] allPOI = null;
            allPOI = POILayout.GetPOITypeFromDB();

            if (allPOI != null)
            {
                foreach (POILayout.POITypes POI in allPOI)
                {
                    string value = null;
                    value += POI.Id + "-" + POI.Name;
                    list.Add(value);
                }
            }

            return list;
        }

        /// <summary>
        /// This method validates destinations data before adding/saving/editing
        /// </summary>
        /// <param name="countryDestination"></param>
        /// <param name="destinationName"></param>
        /// <returns></returns>
        protected bool DestinationsValidatedFields(ComboBox countryDestination, TextBox destinationName)
        {
            countryDestination.BackColor = default(Color);
            destinationName.BackColor = default(Color);

            string selectedCombobox = countryDestination.Text;
            string[] ComboBoxSplit = selectedCombobox.Split('-');

            if (selectedCombobox == "-")
            {
                countryDestination.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationCountryNull;
                return false;
            }
            else if (destinationName.Text == "" || destinationName.Text == " ")
            {
                destinationName.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.DestinationNameNull;
                return false;
            }
            else
            {
                return true;
            }
        }
        /// <summary>
        /// Method which validates all fields during saving/editing/adding POI
        /// </summary>
        /// <param name="POIDestination"></param>
        /// <param name="POIName"></param>
        /// <param name="POIStreetName"></param>
        /// <param name="POIStreetNumber"></param>
        /// <param name="POIPostcode"></param>
        /// <param name="POILatitude"></param>
        /// <param name="POILongtitude"></param>
        /// <param name="POIDescription"></param>
        /// <returns></returns>
        protected bool POIValidatedFields(ComboBox POIDestination, ComboBox POIType, TextBox POIName, TextBox POIStreetName, TextBox POIStreetNumber, TextBox POIPostcode, TextBox POILatitude, TextBox POILongtitude, RichTextBox POIDescription)
        {
            POIDestination.BackColor = default(Color);
            POIType.BackColor = default(Color);
            POIName.BackColor = default(Color);
            POIStreetName.BackColor = default(Color);
            POIStreetNumber.BackColor = default(Color);
            POIPostcode.BackColor = default(Color);
            POILatitude.BackColor = default(Color);
            POILongtitude.BackColor = default(Color);
            POIDescription.BackColor = default(Color);

            string selectedCombobox = POIDestination.Text;
            string[] ComboBoxSplit = selectedCombobox.Split('-');

            string selectedPOITypeCombobox = POIType.Text;
            string[] POITypeComboboxSplit = selectedPOITypeCombobox.Split('-');

            int intTestVariable;

            if (selectedCombobox == "-")
            {
                POIDestination.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIDestinationNull;
                return false;
            }
            else if (selectedPOITypeCombobox == "-")
            {
                POIType.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POITypeNull;
                return false;
            }
            else if (POIName.Text == "" || POIName.Text == " ")
            {
                POIName.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POINameNull;
                return false;
            }
            else if (POIStreetName.Text == "" || POIStreetName.Text == " ")
            {
                POIStreetName.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIStreetNameNull;
                return false;
            }
            else if (POIStreetNumber.Text == "" || POIStreetNumber.Text == " ")
            {
                POIStreetNumber.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIStreetNumberNull;
                return false;
            }
            else if (POIPostcode.Text == "" || POIPostcode.Text == " ")
            {
                POIPostcode.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIPostcodeNull;
                return false;
            }
            else if (!int.TryParse(POIPostcode.Text, out intTestVariable))
            {
                POIPostcode.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIPostcodeFormat;
                return false;
            }
            else if (POILatitude.Text == "" || POILatitude.Text == " ")
            {
                POILatitude.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POILatitudeNull;
                return false;
            }
            else if (POILongtitude.Text == "" || POILongtitude.Text == " ")
            {
                POILongtitude.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POILongtitudeNull;
                return false;
            }
            else if (POIDescription.Text == "" || POIDescription.Text == " ")
            {
                POIDescription.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.POIDescriptionNull;
                return false;
            }
            else
            {
                POILatitude.Text = Regex.Replace(POILatitude.Text, "[^0-9,.]", "");
                POILongtitude.Text = Regex.Replace(POILongtitude.Text, "[^0-9,.]", "");

                double testVariableLa;
                bool isDoubleLa = Double.TryParse(POILatitude.Text, out testVariableLa);
                if (!isDoubleLa)
                {
                    string latitude = POILatitude.Text;
                    latitude = latitude.Replace(".", ",");
                    POILatitude.Text = latitude;
                }

                double testVariableLo;
                bool isDoubleLo = Double.TryParse(POILongtitude.Text, out testVariableLo);
                if (!isDoubleLo)
                {
                    string longtitude = POILongtitude.Text;
                    longtitude = longtitude.Replace(".", ",");
                    POILongtitude.Text = longtitude;
                }

                return true;
            }
        }
        /// <summary>
        /// Method which validates all fields during saving/editing/adding restaurant
        /// </summary>
        /// <param name="restaurantDestination"></param>
        /// <param name="restaurantName"></param>
        /// <param name="restaurantStreetName"></param>
        /// <param name="restaurantStreetNumber"></param>
        /// <param name="restaurantPostcode"></param>
        /// <param name="restaurantLatitude"></param>
        /// <param name="restaurantLongtitude"></param>
        /// <param name="restaurantDescription"></param>
        /// <returns></returns>
        protected bool RestaurantsValidatedFields(ComboBox restaurantDestination, TextBox restaurantName, TextBox restaurantStreetName, TextBox restaurantStreetNumber, TextBox restaurantPostcode, TextBox restaurantLatitude, TextBox restaurantLongtitude, RichTextBox restaurantDescription)
        {
            restaurantDestination.BackColor = default(Color);
            restaurantName.BackColor = default(Color);
            restaurantStreetName.BackColor = default(Color);
            restaurantStreetNumber.BackColor = default(Color);
            restaurantPostcode.BackColor = default(Color);
            restaurantLatitude.BackColor = default(Color);
            restaurantLongtitude.BackColor = default(Color);
            restaurantDescription.BackColor = default(Color);

            string selectedCombobox = restaurantDestination.Text;
            string[] ComboBoxSplit = selectedCombobox.Split('-');

            int intTestVariable;

            if (selectedCombobox == "-")
            {
                restaurantDestination.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantDestinationNull;
                return false;
            }
            else if (restaurantName.Text =="" || restaurantName.Text ==" ")
            {
                restaurantName.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantNameNull;
                return false;
            }
            else if(restaurantStreetName.Text == "" || restaurantStreetName.Text == " ")
            {
                restaurantStreetName.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantStreetNameNull;
                return false;
            }
            else if (restaurantStreetNumber.Text == "" || restaurantStreetNumber.Text == " ")
            {
                restaurantStreetNumber.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantStreetNumberNull;
                return false;
            }
            else if (restaurantPostcode.Text == "" || restaurantPostcode.Text == " ")
            {
                restaurantPostcode.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantPostcodeNull;
                return false;
            }
            else if (!int.TryParse(restaurantPostcode.Text, out intTestVariable))
            {
                restaurantPostcode.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantPostcodeFormat;
                return false;
            }
            else if (restaurantLatitude.Text == "" || restaurantLatitude.Text == " ")
            {
                    restaurantLatitude.BackColor = Color.FromArgb(255, 232, 232);
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantLatitudeNull;
                    return false;
            }
            else if (restaurantLongtitude.Text == "" || restaurantLongtitude.Text == " ")
            {
                restaurantLongtitude.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantLongtitudeNull;
                return false;
            }
            else if (restaurantDescription.Text == "" || restaurantDescription.Text == " ")
            {
                restaurantDescription.BackColor = Color.FromArgb(255, 232, 232);
                DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantDescriptionNull;
                return false;
            }
            else
            {
                restaurantLatitude.Text = Regex.Replace(restaurantLatitude.Text, "[^0-9,.]", "");
                restaurantLongtitude.Text = Regex.Replace(restaurantLongtitude.Text, "[^0-9,.]", "");

                double testVariableLa;
                bool isDoubleLa = Double.TryParse(restaurantLatitude.Text, out testVariableLa);
                if (!isDoubleLa)
                {
                    string latitude = restaurantLatitude.Text;
                    latitude = latitude.Replace(".", ",");
                    restaurantLatitude.Text = latitude;
                }

                double testVariableLo;
                bool isDoubleLo = Double.TryParse(restaurantLongtitude.Text, out testVariableLo);
                if (!isDoubleLo)
                {
                    string longtitude = restaurantLongtitude.Text;
                    longtitude = longtitude.Replace(".", ",");
                    restaurantLongtitude.Text = longtitude;
                }

                return true;
            }
        }
    }
}
