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

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

        public RestaurantsEditPage()
        {
            InitializeComponent();
            DefaultScreen.initializeHeading(RestaurantEditHeading);
            DefaultScreen.initializePanel(RestaurantsEditPagePanel);
            RestaurantEditHeading.Text = Properties.Resources.RestaurantEditHeading;
            RestaurantEditNameLabel.Text = Properties.Resources.RestaurantEditName;
            RestaurantEditDestinationLabel.Text = Properties.Resources.RestaurantEditDestination;
            RestaurantEditStreetNameLabel.Text = Properties.Resources.RestaurantEditStreetName;
            RestaurantEditStreetNumberLabel.Text = Properties.Resources.RestaurantEditStreetNumber;
            RestaurantEditPostcodeLabel.Text = Properties.Resources.RestaurantEditPostcode;
            RestaurantEditMapLatitudeLabel.Text = Properties.Resources.RestaurantEditMapLatitude;
            RestaurantEditMapLongtitudeLabel.Text = Properties.Resources.RestaurantEditMapLongtitude;
            RestaurantEditDescriptionLabel.Text = Properties.Resources.RestaurantEditDescription;
            RestaurantEditImageLabel.Text = Properties.Resources.RestaurantEditImageLabel;
            RestaurantEditRecommendedCheckBox.Text = Properties.Resources.RestaurantRecommendedCheckBox;

            FillData();
        }

        public void FillData()
        {
            RestaurantEditNameTextbox.Text = "";
            RestaurantEditStreenNameTextbox.Text = "";
            RestaurantEditStreetNumberTextbox.Text = "";
            RestaurantEditPostcodeTextbox.Text = "";
            RestaurantEditMapLatitudeTextbox.Text = "";
            RestaurantEditMapLongtitudeTextbox.Text = "";
            RestaurantEditDescriptionRichTextBox.Text = "";
            coverImageURL = "";
            EditRestaurantShowImagePictureBox.Image = null;
            RestaurantEditRecommendedCheckBox.Checked = false;

            RestaurantsLayout.Restaurants[] restaurantData = null;
            restaurantData = RestaurantsLayout.GetRestaurantFromDbById(globalRestaurantId);

            int destinationId = 0;
            string destinationName = "";

            if (restaurantData != null)
            {
                foreach (RestaurantsLayout.Restaurants restaurant in restaurantData)
                {

                    RestaurantEditNameTextbox.Text = restaurant.Name;
                    RestaurantEditStreenNameTextbox.Text = restaurant.Street;
                    RestaurantEditStreetNumberTextbox.Text = restaurant.StreetNumber;
                    RestaurantEditPostcodeTextbox.Text = restaurant.Postcode.ToString();
                    RestaurantEditMapLatitudeTextbox.Text = restaurant.Latitude.ToString();
                    RestaurantEditMapLongtitudeTextbox.Text = restaurant.Longtitude.ToString();
                    RestaurantEditDescriptionRichTextBox.Text = restaurant.Description;
                    coverImageURL = restaurant.coverImageURL;
                    destinationId = restaurant.DestinationId;
                    destinationName = restaurant.DestinationName;
                    RestaurantEditRecommendedCheckBox.Checked = restaurant.Recommended;
                }
            }
            if (coverImageURL != "")
            {
                Bitmap returned = FTPLayout.FTPRead(coverImageURL);
                if (returned != null)
                {
                    EditRestaurantShowImagePictureBox.Image = returned;
                }
                else
                {
                    EditRestaurantShowImagePictureBox.Image = null;
                }
            }
            else
            {
                EditRestaurantShowImagePictureBox.Image = null;
            }
            List <string> dataToCombobox = GenerateDestinationsList();
            ChangeDestinationNameCombobox.DataSource = dataToCombobox;
            ChangeDestinationNameCombobox.SelectedIndex = ChangeDestinationNameCombobox.FindStringExact(destinationId + "-" + destinationName);
        }

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

        private void EditRestaurantSaveButton_Click(object sender, EventArgs e)
        {
            bool state = RestaurantsValidatedFields(ChangeDestinationNameCombobox, RestaurantEditNameTextbox, RestaurantEditStreenNameTextbox, RestaurantEditStreetNumberTextbox, RestaurantEditPostcodeTextbox, RestaurantEditMapLatitudeTextbox, RestaurantEditMapLongtitudeTextbox, RestaurantEditDescriptionRichTextBox);

            if (state)
            {
                string selectedCombobox = ChangeDestinationNameCombobox.Text;
                string[] ComboBoxSplit = selectedCombobox.Split('-');

                int status = RestaurantsLayout.EditRestaurant(globalRestaurantId, Convert.ToInt16(ComboBoxSplit[0]), RestaurantEditNameTextbox.Text, RestaurantEditStreenNameTextbox.Text, RestaurantEditStreetNumberTextbox.Text, RestaurantEditPostcodeTextbox.Text, RestaurantEditMapLatitudeTextbox.Text, RestaurantEditMapLongtitudeTextbox.Text, RestaurantEditDescriptionRichTextBox.Text, coverImageURL, RestaurantEditRecommendedCheckBox.Checked);

                if (status == 1)
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantEditedSuccessfully;
                    DefaultScreen.ShowStatusPage(1, Properties.Resources.RestaurantEditedSuccessfully, Properties.Resources.RestaurantEditedSuccessfullyDesc);
                    RestaurantsShowPage.Instance.GetRestaurants();
                }
                else
                {
                    DefaultScreen.StatusLabel.Text = DateTime.Now + " - " + Properties.Resources.RestaurantNotEdited;
                    DefaultScreen.ShowStatusPage(0, Properties.Resources.RestaurantNotEdited, Properties.Resources.RestaurantNotEditedDesc);
                }
            }
        }

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

        private void EditRestaurantImageUploadButton_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;
                    EditRestaurantShowImagePictureBox.Image = FTPLayout.FTPRead(coverImageURL);
                }
                coverImageURL = uploadedFileName;
            }
        }

        private void EditRestaurantImageRemoveButton_Click(object sender, EventArgs e)
        {
            coverImageURL = "";
            EditRestaurantShowImagePictureBox.Image = null;
        }
    }
}
