using System;
using System.Windows.Forms;
using System.Net;
using Microsoft.Win32;

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

        public POIsShowPOIPage()
        {
            InitializeComponent();
            
            DefaultScreen.initializeHeading(POIShowHeading);
            DefaultScreen.initializePanel(POIShowPOIPagePanel);

            POIShowPOITypeNameLabel.Text = Properties.Resources.POIShowPOITypeLabel;
            POIShowPOIAddressLabel.Text = Properties.Resources.POIShowPOIAddressLabel;
            POIShowPOIDescriptionLabel.Text = Properties.Resources.POIShowPOIDescriptionLabel;
            
            POIShowPOIShowMapCheckBox.Checked = false;
            mapBrowser.Visible = false;
            FillData();
        }

        public void FillData()
        {
            POIShowPOIPageTypeLabel.Text = "";
            POIShowPOIPageStreetLabel.Text = "";
            POIShowPOIPagePostcodeLabel.Text = "";
            POIShowPOIPageLatitudeLabel.Text = "";
            POIShowPOIPageLongtitudeLabel.Text = "";
            POIShowPOIDescriptionRichTextBox.Text = "";
            POIShowPOIShowMapCheckBox.Checked = false;

            POILayout.POI[] poiData = null;
            poiData = POILayout.GetPOIFromDbById(globalPOIId);
           
            if (poiData != null)
            {
                foreach (POILayout.POI poi in poiData)
                {
                    globalPOIId = poi.Id;
                    POIShowPOIPageTypeLabel.Text = poi.POITypeName;
                    POIShowHeading.Text = Properties.Resources.POIShowHeadingName + " " + poi.Name + " " + Properties.Resources.POIShowHeadingNameIn + " " + poi.DestinationName + " " + Properties.Resources.POIShowHeadingNameDestination;
                    POIShowPOIPageStreetLabel.Text = poi.Street + " " + poi.StreetNumber;
                    POIShowPOIPagePostcodeLabel.Text = poi.Postcode.ToString();
                    POIShowPOIPageLatitudeLabel.Text = "Latitude - " + poi.Latitude;
                    POIShowPOIPageLongtitudeLabel.Text = "Longtitude - " + poi.Longtitude;
                    POIShowPOIDescriptionRichTextBox.Text = poi.Description;

                    string address = poi.DestinationName + "," + poi.Street + " " + poi.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;
                } 
            }
        }

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

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

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