using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using GalaSoft.MvvmLight.Messaging;
using OzCommon.View;
using OzCommon.ViewModel;
using LoginViewModel = OPSCallCenterCRM.ViewModel.LoginViewModel;

namespace OPSCallCenterCRM.View
{
    public partial class LoginWindow : Window
    {
        private WaitWindow _waitWindow;
        private bool _connected;
        private LoginViewModel _viewModel;

        public LoginWindow()
        {
            InitializeComponent();
            _viewModel = (LoginViewModel)DataContext;

            Messenger.Default.Register<NotificationMessage>(this, MessageReceived);

            ServerAddress.Focus();
            Closed += LoginWindow_Closed;
        }

        void LoginWindow_Closed(object sender, EventArgs e)
        {
            Messenger.Default.Unregister<NotificationMessage>(this, MessageReceived);
            Closed -= LoginWindow_Closed;
        }

        private void MessageReceived(NotificationMessage notificationMessage)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                if (notificationMessage.Notification == Messages.ShowWaitWindow)
                {
                    if (_waitWindow != null)
                        _waitWindow.Close();

                    _connected = false;

                    _waitWindow = new WaitWindow("Logging in, please wait...", true);

                    _waitWindow.Owner = this;
                    _waitWindow.ShowDialog();
                }
                else if (notificationMessage.Notification == Messages.NavigateToMainWindow)
                {
                    _connected = true;
                    if (_waitWindow != null)
                        _waitWindow.Close();

                    Close();
                }
                else if (notificationMessage.Notification == Messages.DismissWaitWindow)
                {
                    if (_waitWindow != null)
                    {
                        _waitWindow.Close();
                        _waitWindow = null;
                    }
                }
            }));
        }

        public void ShowWindow()
        {
            Activate();
            ShowInTaskbar = true;
            Show();
            WindowState = WindowState.Normal;
            Focus();
        }

        private void LoginCloseButtonMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Close();
        }

        private void LoginCloseButton_OnMouseEnter(object sender, MouseEventArgs e)
        {
            LoginCloseButton.Foreground = new SolidColorBrush(Color.FromRgb(188, 216, 188));
        }

        private void LoginCloseButton_MouseLeave(object sender, MouseEventArgs e)
        {
            LoginCloseButton.Foreground = new SolidColorBrush(Colors.Gray);
        }

        private void LoginHeader_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
    }
}
