﻿using System;
using System.Threading.Tasks;
using System.Windows;
using InLine.Services;
using MahApps.Metro.Controls;
using QueueLib;
using QueueLib.Messaging;

namespace InLine
{
    public partial class MainWindow : MetroWindow
    {
        private MetroDialogService dialogService;
        private MessagingService messagingService;

        public MainWindow()
        {
            InitializeComponent();

            dialogService = new MetroDialogService(this);
            messagingService = new MessagingService();

            var model = new QueueModel(dialogService, messagingService);

            LayoutRoot.DataContext = model;
            Flyouts.DataContext = messagingService;
            MessagesButton.DataContext = messagingService;

            try
            {
                Task.Run(async () =>
                {
                    await model.AddMachine(Environment.MachineName);
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SendOutgoingMessage()
        {
            var q = new System.Messaging.MessageQueue(@"FORMATNAME:DIRECT=OS:MyServer\private$\MyQueue");
            var msg = new System.Messaging.Message("Test message");
            q.Send(msg, System.Messaging.MessageQueueTransactionType.Single);
        }

        private void Flyouts_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            messagingService.InterruptAutoClose();
        }

        private void Flyouts_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            messagingService.Hide();
        }
    }
}