﻿using GalaSoft.MvvmLight.Messaging;
using Quest.MobileServices.Messaging;
using Quest.MobileServices.ViewModels;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace Quest.Mobile.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPageMaster : ContentPage
    {
        private static bool AreMessagesRegistered = false;

        public ListView ListView { get { return MenuItemsListView; } }
        private readonly IMasterPageViewModel _masterPageViewModel;

        public MainPageMaster()
        {
            InitializeComponent();

            RegisterMessages();

            grdMainMenu.BackgroundColor = Color.FromHex("1D9942");

            _masterPageViewModel = DependencyService.Get<IMasterPageViewModel>();

            BindingContext = _masterPageViewModel;
        }


        private void RegisterMessages()
        {
            if (!AreMessagesRegistered)
            {
                Messenger.Default.Register<ChangeNavigationBarColourMessage>(this, (message) =>
                {
                    Xamarin.Forms.Device.BeginInvokeOnMainThread(() =>
                    {
                        //We handle this here as opposed to using the view model and binding the background colour because the VM doesn't get registered in the IOC in time
                        //Forcing the IOC to register the VM right away causes issues with DI and the app will crash.
                        //This works fine.
                        grdMainMenu.BackgroundColor = message.BackgroundColour;
                    });

                });

                AreMessagesRegistered = true;
            }
        }
    }
}