using System;
using System.Windows.Forms;
using System.Net;

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

        public RestaurantsShowRestaurantPage()
        {
            InitializeComponent();

            DefaultScreen.initializeHeading(RestaurantShowHeading);
            DefaultScreen.initializePanel(RestaurantsShowRestaurantPagePanel);

            RestaurantShowRestaurantAddressLabel.Text = Properties.Resources.RestaurantShowRestaurantAddressLabel;
            RestaurantsShowRestaurantDescriptionLabel.Text = Properties.Resources.RestaurantShowRestaurantDescriptionLabel;
            RestaurantShowImageLabel.Text = Properties.Resources.RestaurantShowRestaurantImageCoverLabel;
            RestaurantShowRecommendedCheckBox.Text = Properties.Resources.RestaurantRecommendedCheckBox;
            RestaurantsShowRestaurantShowMapCheckBox.Checked = false;
            mapBrowser.Visible = false;
            FillData();
        }

        public void FillData()
        {
            RestaurantsShowRestaurantPageStreetLabel.Text = "";
            RestaurantsShowRestaurantPagePostcodeLabel.Text = "";
            RestaurantsShowRestaurantPageLatitudeLabel.Text = "";
            RestaurantsShowRestaurantPageLongtitudeLabel.Text = "";
            RestaurantShowRestaurantDescriptionRichTextBox.Text = "";
            RestaurantsShowRestaurantShowMapCheckBox.Checked = false;
            RestaurantShowRecommendedCheckBox.Checked = false;

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

            if (restaurantData != null)
            {
                foreach (RestaurantsLayout.Restaurants restaurant in restaurantData)
                {
                    globalRestaurantId = restaurant.Id;
                    RestaurantShowHeading.Text = Properties.Resources.RestaurantShowHeadingName + " " + restaurant.Name + " " + Properties.Resources.RestaurantShowHeadingNameIn + " " + restaurant.DestinationName + " " + Properties.Resources.RestaurantShowHeadingNameDestination;
                    RestaurantsShowRestaurantPageStreetLabel.Text = restaurant.Street + " " + restaurant.StreetNumber;
                    RestaurantsShowRestaurantPagePostcodeLabel.Text = restaurant.Postcode.ToString();
                    RestaurantsShowRestaurantPageLatitudeLabel.Text = "Latitude - " + restaurant.Latitude;
                    RestaurantsShowRestaurantPageLongtitudeLabel.Text = "Longtitude - " + restaurant.Longtitude;
                    RestaurantShowRestaurantDescriptionRichTextBox.Text = restaurant.Description;
                    coverImageURL = restaurant.coverImageURL;
                    RestaurantShowRecommendedCheckBox.Checked = restaurant.Recommended;

                    string address = restaurant.DestinationName + "," + restaurant.Street + " " + restaurant.StreetNumber;
                    string embeddedpage = @"
                        <html>
                            <body>
                                <iframe type='text/html' width='280' height='189' 
                                    src='https://www.google.com/maps/embed/v1/place?key=AIzaSyDMxQ0lwTKcxqsJtkUbhRtFtMGLDjEZoYE&q=" + address + @"' frameborder='0'>
                                </iframe>
                            </body>
                        </html>";

                    mapBrowser.DocumentText = embeddedpage;
                }
                if (coverImageURL != "")
                {
                    ShowRestaurantShowImagePictureBox.Image = FTPLayout.FTPRead(coverImageURL);
                }
                else
                {
                    ShowRestaurantShowImagePictureBox.Image = null;
                }
            }
        }

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

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

        private void RestaurantsShowRestaurantShowMapCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.RestaurantsShowRestaurantShowMapCheckBox.Checked == true)
            {
                mapBrowser.Visible = true;
            }
            else
            {
                mapBrowser.Visible = false;
            }
        }

    }
}
