using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace VSview
{
    public class StatusViewModel : BaseViewModel
    {

        #region Public properties
        /// <summary>
        /// Reference to status window
        /// </summary>
        public StatusWindow StatusWin;

        /// <summary>
        /// Collection of detector state for filtering
        /// </summary>
        public List<Status> ActiveStatus { get; set; }

        /// <summary>
        /// Collection of active statuses
        /// </summary>
        public ListCollectionView StatusFilter { get; set; }

        private bool _errorsChecked;
        /// <summary>
        /// Property for showing only errors
        /// </summary>
        public bool ErrorsChecked
        {
            get { return _errorsChecked; }
            set { _errorsChecked = value; StatusFilter.Refresh(); }
        }

        #endregion

        #region Private methods
        /// <summary>
        /// Method for filtering active errors
        /// </summary>
        private void SetupFilter()
        {
            StatusFilter = CollectionViewSource.GetDefaultView(ActiveStatus) as ListCollectionView;
            StatusFilter.IsLiveFiltering = true;
            StatusFilter.LiveFilteringProperties.Add("IsActive");
            StatusFilter.Filter = Filter;
            StatusFilter.IsLiveSorting = true;
            StatusFilter.SortDescriptions.Add(new SortDescription("Time", ListSortDirection.Descending));
        }

        /// <summary>
        /// filter which filters only active errors - active errors are visible in <see cref="ActiveStatus"/>
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private bool Filter(object item)
        {
            Status err = item as Status;

            if (ErrorsChecked)
            {
                if (err.IsError)
                    return err.IsActive;
                else
                    return false;
            }
            else
                return err.IsActive;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default contructor
        /// </summary>
        public StatusViewModel()
        {
            ActiveStatus = new List<Status>();

            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboFault") as string, Application.Current.FindResource("StatusTurboFault") as string, true));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboSpeed") as string, Application.Current.FindResource("StatusTurboSpeed") as string, true));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboCrash") as string, Application.Current.FindResource("StatusTurboCrash") as string, true));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboTemperature") as string, Application.Current.FindResource("StatusTurboTemperature") as string, true));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboTepmWarning") as string, Application.Current.FindResource("StatusTurboTepmWarning") as string, false));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboBoard") as string, Application.Current.FindResource("StatusTurboBoard") as string, true));
            ActiveStatus.Add(new Status(Application.Current.FindResource("StatusNameTurboRange") as string, Application.Current.FindResource("StatusTurboRange") as string, true));

            foreach (Status stat in ActiveStatus)
            {
                stat.IsActive = true;
            }

            SetupFilter();

            Command = new Dictionary<string, RelayCommand>();

            Command.Add("OpenStatusWindow", new RelayCommand(OpenStatusWindow));
            Command.Add("CloseStatusWindow", new RelayCommand(CloseStatusWindow));
            Command.Add("OpenManual", new RelayCommand(OpenManual));
            Command.Add("Acknowledge", new RelayCommand(Acknowledge));
            Command.Add("ShowOnlyErrors", new RelayCommand(ShowOnlyErrors));

        }

        #endregion

        #region Commands

        public Dictionary<string, RelayCommand> Command { get; set; }


        /// <summary>
        /// Method for opening status window
        /// </summary>
        /// <param name="obj"></param>
        private void OpenStatusWindow(object obj)
        {
            try
            {
                if (Application.Current.Windows[0].WindowState != WindowState.Maximized)
                {
                    StatusWin = new StatusWindow(this)
                    {
                        Left = Application.Current.MainWindow.Left,
                        Top = Application.Current.MainWindow.Top + Application.Current.MainWindow.ActualHeight - 500,

                        //Pass MainViewModel as binding reference
                        DataContext = this
                    };
                }
                else
                {
                    StatusWin = new StatusWindow(this)
                    {
                        Left = 0,
                        Top = SystemParameters.PrimaryScreenHeight - 500,

                        //Pass MainViewModel as binding reference
                        DataContext = this
                    };
                }

                StatusWin.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while opening status window. \nReason: " + ex.Message, "Status window error", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        /// <summary>
        /// Method for closing status window
        /// </summary>
        /// <param name="obj"></param>
        private void CloseStatusWindow(object obj)
        {
            try
            {
                StatusWin.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error occured while closing status window. \nReason: " + ex.Message, "Status window error", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        /// <summary>
        /// Method for openning manual
        /// </summary>
        /// <param name="obj"></param>
        private void OpenManual(object obj)
        {

        }

        /// <summary>
        /// Method for clearing non-active errors
        /// </summary>
        /// <param name="obj"></param>
        private void Acknowledge(object obj)
        {
            foreach(Status stat in ActiveStatus)
            {
                stat.IsActive = false;
            }
        }

        /// <summary>
        /// Method for changing active error filtering
        /// </summary>
        /// <param name="obj"></param>
        private void ShowOnlyErrors(object obj)
        {

        }



        #endregion

    }
}
