﻿using System;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Xaml;

namespace pdt.Views.OperationalMap
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
	public partial class MapView : ContentPage
	{
        //private static bool AreMessagesWired = false;

        public MapView ()
		{
            try
            {
                InitializeComponent();
                this.BindingContext = App.Locator.OperationalMapViewModel;
                OperationalMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(51.5, 0), Distance.FromMiles(5)));

                //WireMessages();

            }
            catch (Exception e)
            {
                throw e;
            }
        }

        //private void WireMessages()
        //{
        //    if (AreMessagesWired)
        //    {
        //        Messenger.Default.Register<OperationMapArtefactsChangedMessage>(this, (message) =>
        //        {
        //            Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
        //            {
        //                RenderDestinations(message.Artefacts);
        //            });

        //        });


        //        AreMessagesWired = true;
        //    }
        //}

        //private void RenderDestinations(OperationalMapArtefacts operationalMapArtefacts)
        //{
        //    if (operationalMapArtefacts != null && operationalMapArtefacts.Destinations != null)
        //    {
        //        foreach (var dest in operationalMapArtefacts.Destinations)
        //        {
        //            var p = new Pin();
        //            p.Type = PinType.Place;
        //            p.Position = new Position(dest.Position.Latitude, p.Position.Longitude);
        //            p.Label = dest.Name;
        //            p.Address = MakeDestinationAddress(dest);


        //            OperationalMap.Pins.Add(p);
        //        }

                
        //    }
        //}

        //private string MakeDestinationAddress(Destination destination)
        //{
        //    var sb = new System.Text.StringBuilder();
        //    if (destination != null)
        //    {
        //        sb.AppendLine($"Code: {destination.Code}");
        //        sb.AppendLine();
        //        sb.AppendLine($"Station?: {destination.IsStation.ToString()}");
        //        sb.AppendLine($"Standby?: {destination.IsStandby.ToString()}");
        //        sb.AppendLine($"Road?: {destination.IsRoad.ToString()}");
        //        sb.AppendLine($"Hospital?: {destination.IsHospital.ToString()}");
        //        sb.AppendLine($"A&E?: {destination.IsAandE.ToString()}");
        //    }

        //    return sb.ToString();
        //}
    }
}