﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Popups;

namespace MC.NestUtility.Core.Managers
{
#pragma warning disable CS4014
    public class MessageManager
    {
        readonly NestManager _nestMan;

        public MessageManager(NestManager nestMan)
        {
            _nestMan = nestMan;
        }

        public void ShowMessageSimple(string title, string content)
        {
            ShowMessaage(content, title);
        }

        public void ShowNestDownMessage()
        {
            ShowMessaage("Nest is Down", "It looks like Nest is down right now. Go outside for a while and try again in a few minutes.");
        }

        public async void ShowSigninMessage(string toDoWhat)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {
                // Add the buttons
                var response = await ShowYesNoMessage("Login Required", $"You must be logged into to a Nest account to {toDoWhat}. Do you want to login or create a new account now?");

                if(response.HasValue && response.Value)
                {
                    _nestMan.NavigateToLogin();
                }
            });
        }


        public void DebugDia(string str, Exception ex = null)
        {
            if (_nestMan.UiSettingsMan.DeveloperDebug)
            {
                Debug.WriteLine("Error, " + str + " Message: " + (ex == null ? "" : ex.Message));
                ShowMessaage("DebugDia: str " + str + " \n\nMessage: " + (ex == null ? "" : ex.Message) + "\n\nCall Stack:\n"+(ex== null? "" : ex.StackTrace), "DebugDia");
            }
        }

        private async void ShowMessaage(string content, string title)
        {
            // Don't show messages if we are in the background.
            if(_nestMan.IsBackgroundTask)
            {
                return;
            }

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
            {
                try
                {
                    var message = new MessageDialog(content, title);
                    await message.ShowAsync();
                }
                catch(Exception e)
                {
                    TelemetryManager.ReportUnExpectedEvent(this, "FailedToShowMessage",e);
                }
            });
        }

        /// <summary>
        /// Shows a yes no dialog with a message.
        /// MUST BE CALLED FROM THE UI THREAD!
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<bool?> ShowYesNoMessage(string title, string content, string postiveButton = "Yes", string negativeButton = "No")
        {
            // Don't show messages if we are in the background.
            if (_nestMan.IsBackgroundTask)
            {
                return null;
            }

            bool? response = null;

            // Add the buttons
            var message = new MessageDialog(content, title);
            message.Commands.Add(new UICommand(
                postiveButton,
                (IUICommand command)=> {
                    response = true; }));
            message.Commands.Add(new UICommand(
                negativeButton,
                (IUICommand command) => { response = false; }));
            message.DefaultCommandIndex = 0;
            message.CancelCommandIndex = 1;

            // Show the dialog
            await message.ShowAsync();

            return response;
        }
    }
}
