using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WCS.Model.ApiModel.User;
using WCS.Model.ApiModel;
using WCS.Model;
using WCS.Model.WebSocketModel;
using 智慧物流软件系统.ViewModel;

namespace 智慧物流软件系统
{
    public static class WarningManager
    {
        public static List<WarningWindow> warnings = new List<WarningWindow>();

        public static object flag = new object();

        public static void AddWarning(WebSocketMessageModel warningModel)
        {
            lock (flag)
            {
                Logs.Write($"【添加报警信息弹窗】报警类型：{warningModel.WarningType} 报警信息:{warningModel.WarningMessage} Guid:{warningModel.Guid}", LogsType.WebSocket);
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    if (warningModel.WarningType == WarningTypeEnum.入库自检未扫描上架 || warningModel.WarningType == WarningTypeEnum.出库自检未扫描上架 || warningModel.WarningType == WarningTypeEnum.自检未扫描上架)
                    {
                        warnings.Add(WarningWindow.ShowNoScanError(warningModel.WarningMessage, "报警信息", warningModel));
                    }
                    else
                    {
                        warnings.Add(WarningWindow.Show(warningModel.WarningMessage, "报警信息", warningModel));
                    }
                }));
            }
        }

        public static void RemoveWarning(Guid guid)
        {
            lock (flag)
            {

                try
                {
                    var warningWindow = warnings.Where(t => t.warning.Guid == guid).First();
                    if (warningWindow != null)
                    {
                        Logs.Write($"【移除报警信息弹窗】报警类型：{warningWindow.warning.WarningType} 报警信息:{warningWindow.warning.WarningMessage} Guid:{warningWindow.warning.Guid}", LogsType.WebSocket);
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            warningWindow?.Close();
                            warnings.Remove(warningWindow);
                        }));
                    }
                    else
                    {
                        Logs.Write($"【移除报警信息弹窗失败】Guid为:{guid}的窗体未找到！");
                    }
                }
                catch
                {

                }
            }
        }

        public static void InitWarning()
        {
            #region 调用接口
            #endregion
        }
    }
}
