﻿using System;
using System.Windows;
using System.Windows.Controls;
using EspaceClient.BackOffice.Silverlight.ViewModels.Messages;
using nRoute.Components.Messaging;
using nRoute.Services;
using OGDC.Silverlight.Toolkit.Services.Services;

namespace EspaceClient.BackOffice.Silverlight.ViewServices
{
    public partial class MessageLogger : UserControl, IDisposable
    {
        public MessageLogger()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            IMessenging messenging = ServiceLocator.GetService<IMessenging>();

            _CloseCurrentTabMessage = messenging.CreateObserver<CloseCurrentTabMessage>(msg => OnMessage(msg));
            _CreateTabMessage = messenging.CreateObserver<CreateTabMessage>(msg => OnMessage(msg));
            _DeletePostItMessage = messenging.CreateObserver<DeletePostItMessage>(msg => OnMessage(msg));
            _DisableEditionMessage = messenging.CreateObserver<DisableEditionMessage>(msg => OnMessage(msg));
            _EnableEditionMessage = messenging.CreateObserver<EnableEditionMessage>(msg => OnMessage(msg));
            _ErrorMessage = messenging.CreateObserver<ErrorMessage>(msg => OnMessage(msg));
            _FullscreenMessage = messenging.CreateObserver<FullscreenMessage>(msg => OnMessage(msg));
            _NavigationMessage = messenging.CreateObserver<NavigationMessage>(msg => OnMessage(msg));
            _TypeConteneur = messenging.CreateObserver<TypeConteneur>(msg => OnMessage(msg));
            _UpdateMenuMessage = messenging.CreateObserver<UpdateMenuMessage>(msg => OnMessage(msg));
            _UpdateListMessage = messenging.CreateObserver<UpdateListMessage>(msg => OnMessage(msg));
            _UpdateSelectedTabContextMessage = messenging.CreateObserver<UpdateSelectedTabContextMessage>(msg => OnMessage(msg));
            _UpdateSelectedTabContextMessage.Subscribe(ThreadOption.UIThread);
            _UpdateTabMessage = messenging.CreateObserver<UpdateTabMessage>(msg => OnMessage(msg));

            _CloseCurrentTabMessage.Subscribe(ThreadOption.UIThread);
            _CreateTabMessage.Subscribe(ThreadOption.UIThread);
            _ErrorMessage.Subscribe(ThreadOption.UIThread);
            _UpdateTabMessage.Subscribe(ThreadOption.UIThread);
            _UpdateListMessage.Subscribe(ThreadOption.UIThread);
            _UpdateMenuMessage.Subscribe(ThreadOption.UIThread);
            _TypeConteneur.Subscribe(ThreadOption.UIThread);
            _NavigationMessage.Subscribe(ThreadOption.UIThread);
            _FullscreenMessage.Subscribe(ThreadOption.UIThread);
            _EnableEditionMessage.Subscribe(ThreadOption.UIThread);
            _DisableEditionMessage.Subscribe(ThreadOption.UIThread);
            _DeletePostItMessage.Subscribe(ThreadOption.UIThread);
        }

        void OnMessage(object message)
        {
            logger.Text = String.Format("{0}{1}{2}", logger.Text, Environment.NewLine, message.GetType().Name);

            myScrollViewer.ScrollToVerticalOffset(logger.ActualHeight);
        }

        public void Dispose()
        {
            _CloseCurrentTabMessage.Unsubscribe();
            _CreateTabMessage.Unsubscribe();
            _ErrorMessage.Unsubscribe();
            _UpdateTabMessage.Unsubscribe();
            _UpdateListMessage.Unsubscribe();
            _UpdateMenuMessage.Unsubscribe();
            _TypeConteneur.Unsubscribe();
            _NavigationMessage.Unsubscribe();
            _FullscreenMessage.Unsubscribe();
            _EnableEditionMessage.Unsubscribe();
            _DisableEditionMessage.Unsubscribe();
            _DeletePostItMessage.Unsubscribe();

            Loaded -= OnLoaded;
        }

        public ChannelObserver<CloseCurrentTabMessage> _CloseCurrentTabMessage { get; set; }
        public ChannelObserver<CreateTabMessage> _CreateTabMessage { get; set; }
        public ChannelObserver<DeletePostItMessage> _DeletePostItMessage { get; set; }
        public ChannelObserver<DisableEditionMessage> _DisableEditionMessage { get; set; }
        public ChannelObserver<FullscreenMessage> _FullscreenMessage { get; set; }
        public ChannelObserver<EnableEditionMessage> _EnableEditionMessage { get; set; }
        public ChannelObserver<ErrorMessage> _ErrorMessage { get; set; }
        public ChannelObserver<NavigationMessage> _NavigationMessage { get; set; }
        public ChannelObserver<TypeConteneur> _TypeConteneur { get; set; }
        public ChannelObserver<UpdateMenuMessage> _UpdateMenuMessage { get; set; }
        public ChannelObserver<UpdateListMessage> _UpdateListMessage { get; set; }
        public ChannelObserver<UpdateSelectedTabContextMessage> _UpdateSelectedTabContextMessage { get; set; }
        public ChannelObserver<UpdateTabMessage> _UpdateTabMessage { get; set; }
    }
}
