using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using BPA.Message;
using Microsoft.Toolkit.Mvvm.ComponentModel;
using HBLConsole.Service;
using HBLConsole.Model;
using BPA.Message.Enum;

namespace HBLConsole.ViewModel
{
    public class ViewModelBase : ObservableObject
    {

        public ViewModelBase()
        {
            MessageLog.GetInstance.InfoNotify = new Action<string>((s) =>
            {
                LogMessage = MessageLog.GetInstance.LogInfo;
            });
        }

        #region 静态通知属性

        public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
        private static void OnStaticPropertyChanged([CallerMemberName] string PropName = "")
        {
            StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(PropName));
        }


        public static bool NetworkConnectState { get { return _mNetworkConnectState; } set { _mNetworkConnectState = value; OnStaticPropertyChanged(); } }
        private static bool _mNetworkConnectState;


        /// <summary>
        /// 日志消息
        /// </summary>
        public static string LogMessage { get { return _mLogMessage; } set { _mLogMessage = value; OnStaticPropertyChanged(); } }
        private static string _mLogMessage = string.Empty;

        /// <summary>
        /// 当前订单量
        /// </summary>
        public static int OrderCount { get { return _mOrderCount; } set { _mOrderCount = value; OnStaticPropertyChanged(); } }
        private static int _mOrderCount;

        /// <summary>
        /// 订单状态列表
        /// </summary>
        public static ObservableCollection<OrderData> orderStatusLists { get; set; } = new ObservableCollection<OrderData>();

        /// <summary>
        /// 等待取餐列表
        /// </summary>
        public static ObservableCollection<OrderData> WaitTakeMeal { get; set; } = new ObservableCollection<OrderData>();

        /// <summary>
        /// 显示当前窗体名称
        /// </summary>
        public static string WindowName { get { return _mWindowName; } set { _mWindowName = value; OnStaticPropertyChanged(); } }
        private static string _mWindowName = string.Empty;


        #region 流程监控属性
        /// <summary>
        /// 流程条件监控
        /// </summary>
        public static ObservableCollection<ProcessConditions> processConditions { get; set; } = new ObservableCollection<ProcessConditions>();
        #endregion

        #endregion

        /// <summary>
        /// 界面切换显示属性
        /// </summary>
        public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
        private FrameworkElement _mMainContent;

        #region MQTT 大屏取餐通知委托
        /// <summary>
        /// MQTT 大屏取餐通知委托
        /// </summary>
        public void MqttReceive()
        {
            ActionOperate.GetInstance.Register(new Action<object>((o) =>
            {
                if (o is MorkOrderPush morkOrderpush)
                {
                    System.Windows.Application.Current?.Dispatcher.BeginInvoke((Action)delegate
                    {
                        var result = orderStatusLists.FirstOrDefault(p => p.OrderPush.SuborderId == morkOrderpush.SuborderId);
                        if (result == null)
                        {
                            orderStatusLists.Add(new OrderData()
                            {
                                OrderPush = morkOrderpush,
                                OrderStatus = ORDER_STATUS.WAIT,
                                StartDate = DateTime.Now.ToString("HH:mm:ss")
                            });
                        }
                    });
                    
                }
            }), "AddOrder");

            ActionOperate.GetInstance.Register(new Action<object>((o) =>
            {
                if (o is OrderStatusChange orderStatusChange)
                {
                    System.Windows.Application.Current?.Dispatcher.BeginInvoke((Action)delegate
                    {
                        int index = Array.FindIndex(orderStatusLists.ToArray(),p => p.OrderPush.SuborderId == orderStatusChange.SuborderId);

                        if (index >= 0) orderStatusLists.ElementAt(index).OrderStatus = orderStatusChange.CookingStatus;
                        switch (orderStatusChange.CookingStatus)
                        {
                            case ORDER_STATUS.COOKING:

                                break;
                            case ORDER_STATUS.COMPLETED_COOK:
                                if (index >= 0)
                                {
                                    orderStatusLists.ElementAt(index).EndDate = DateTime.Now.ToString("HH:mm:ss");
                                    TimeSpan timeSpan = DateTime.Now.Subtract(Convert.ToDateTime(orderStatusLists.ElementAt(index).StartDate));
                                    orderStatusLists.ElementAt(index).CompleteDate = timeSpan.TotalSeconds.ToString();
                                    WaitTakeMeal.Insert(0,orderStatusLists.ElementAt(index));
                                    orderStatusLists.RemoveAt(index);
                                }

                                break;
                            case ORDER_STATUS.COMPLETED_TAKE:
                                var re = WaitTakeMeal.FirstOrDefault(p => p.OrderPush.SuborderId == orderStatusChange.SuborderId);
                                if (re != null) WaitTakeMeal.Remove(re);
                                break;
                            default:
                                break;
                        }
                    });
                    
                }
            }), "OrderStatusChange");
        }
        #endregion
    }
}
