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

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

        public RestaurantsAddPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(RestaurantAddHeading);
            DefaultScreen.initializePanel(RestaurantsAddPagePanel);
            RestaurantAddHeading.Text = Properties.Resources.RestaurantsAddHeading;
            RestaurantAddNameLabel.Text = Properties.Resources.RestaurantAddName;
            RestaurantAddDestinationLabel.Text = Properties.Resources.RestaurantAddDestination;
            RestaurantAddStreetNameLabel.Text = Properties.Resources.RestaurantAddStreetName;
            RestaurantAddStreetNumberLabel.Text = Properties.Resources.RestaurantAddStreetNumber;
            RestaurantAddPostcodeLabel.Text = Properties.Resources.RestaurantAddPostcode;
            RestaurantAddMapLatitudeLabel.Text = Properties.Resources.RestaurantAddMapLatitude;
            RestaurantAddMapLongtitudeLabel.Text = Properties.Resources.RestaurantAddMapLongtitude;
            RestaurantAddDescriptionLabel.Text = Properties.Resources.RestaurantAddDescription;
            RestaurantAddImageLabel.Text = Properties.Resources.RestaurantAddImageLabel;
            RestaurantAddRecommendedCheckBox.Text = Properties.Resources.RestaurantRecommendedCheckBox;

            List<string> dataToCombobox = GenerateDestinationsList();
            DestinationOfRestaurantComboBox.DataSource = dataToCombobox;
        }

        private void ClearData()
        {
            RestaurantAddNameTextbox.Text = "";
            RestaurantAddStreenNameTextbox.Text = "";
            RestaurantAddStreetNumberTextbox.Text = "";
            RestaurantAddPostcodeTextbox.Text = "";
            RestaurantAddMapLatitudeTextbox.Text = "";
            RestaurantAddMapLongtitudeTextbox.Text = "";
            RestaurantAddDescriptionRichTextBox.Text = "";
            coverImageURL = "";
            AddRestaurantShowImagePictureBox.Image = null;
            RestaurantAddRecommendedCheckBox.Checked = false;

            ClearGlobalData();
        }

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

        private void AddRestaurantSaveButton_Click(object sender, EventArgs e)
        {
            bool state = RestaurantsValidatedFields(DestinationOfRestaurantComboBox, RestaurantAddNameTextbox, RestaurantAddStreenNameTextbox, RestaurantAddStreetNumberTextbox, RestaurantAddPostcodeTextbox, RestaurantAddMapLatitudeTextbox, RestaurantAddMapLongtitudeTextbox, RestaurantAddDescriptionRichTextBox);
            
            if (state)
            {
                string selectedCombobox = DestinationOfRestaurantComboBox.Text;
                string[] ComboBoxSplit = selectedCombobox.Split('-');

                int status = RestaurantsLayout.AddRestaurant(Convert.ToInt16(ComboBoxSplit[0]),RestaurantAddNameTextbox.Text, RestaurantAddStreenNameTextbox.Text, RestaurantAddStreetNumberTextbox.Text, RestaurantAddPostcodeTextbox.Text, RestaurantAddMapLatitudeTextbox.Text, RestaurantAddMapLongtitudeTextbox.Text, RestaurantAddDescriptionRichTextBox.Text, coverImageURL, RestaurantAddRecommendedCheckBox.Checked);
                    if (status == 1)
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantAddedSuccessfully;
                        DefaultScreen.ShowStatusPage(1, Properties.Resources.RestaurantAddedSuccessfully, Properties.Resources.RestaurantAddedSuccessfullyDesc);
                        RestaurantsShowPage.Instance.GetRestaurants();
                        ClearData();
                    }
                    else
                    {
                        DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantNotAdded;
                        DefaultScreen.ShowStatusPage(0, Properties.Resources.RestaurantNotAdded, Properties.Resources.RestaurantNotAddedDesc);
                    }
            }
        }

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

        private void AddRestaurantImageUploadButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                string fullPath = open.FileName;
                string fileName = open.SafeFileName;
                string path = fullPath.Replace(fileName, "");
                string folderName = "restaurants";
                string uploadedFileName = FTPLayout.FTPUpload(fullPath, folderName);
                if (uploadedFileName != "")
                {
                    coverImageURL = uploadedFileName;
                    AddRestaurantShowImagePictureBox.Image = FTPLayout.FTPRead(coverImageURL);
                }
            }
        }
    }
}
