using 智慧物流软件系统.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WCS.Model.WebSocketModel;
using HandyControl.Controls;
using WCS.Model;
using 智慧物流软件系统.Api;
using TouchSocket.Core;
using Window = HandyControl.Controls.Window;

namespace 智慧物流软件系统
{
    /// <summary>
    /// 提示框
    /// </summary>
    public partial class WarningWindow : HandyControl.Controls.Window
    {
        public WebSocketMessageModel warning { get; set; }
        public WarningWindow(WebSocketMessageModel _warning)
        {
            InitializeComponent();
            this.Topmost = true; // 设置窗口始终在最上方  
            this.WindowStyle = WindowStyle.None; // 可选，移除窗口的标准边框  
            warning = _warning;
        }

        private void closeClick(object sender, RoutedEventArgs e)
        {
            //this.Close();
            WarningManager.RemoveWarning(warning.Guid);
        }

        ///// <summary>
        ///// 显示提示框
        ///// </summary>
        ///// <param name="content">内容</param>
        ///// <param name="isVisCloseBut">界面右上角是否显示关闭按钮</param>
        ///// <returns>点击的按钮文本</returns>
        //public static WarningWindow Show(string content, WebSocketMessageModel _warning, bool isVisCloseBut = true, Window owner = null)
        //{
        //    return Show(content, string.Empty, _warning, isVisCloseBut);
        //}

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="title">标题</param>
        /// <param name="isVisCloseBut">界面右上角是否显示关闭按钮</param>
        /// <returns>点击的按钮文本</returns>
        public static WarningWindow Show(string content, string title, WebSocketMessageModel _warning, bool isVisCloseBut = true, Window owner = null)
        {
            return Show(content, title, new string[] { "确认", "忽略" }, _warning, isVisCloseBut);
        }

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="title">标题</param>
        /// <param name="isVisCloseBut">界面右上角是否显示关闭按钮</param>
        /// <returns>点击的按钮文本</returns>
        public static WarningWindow ShowNoScanError(string content, string title, WebSocketMessageModel _warning, bool isVisCloseBut = true, Window owner = null)
        {
            content = content + "\r\n确认对应无物料可点击[屏蔽库位]按钮进行库位屏蔽！";
            return Show(content, title, new string[] { "确认","屏蔽库位", "忽略" }, _warning, isVisCloseBut);
        }

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="title">标题</param>
        /// <param name="buttons">按钮内容</param>
        /// <param name="isVisCloseBut">界面右上角是否显示关闭按钮</param>
        /// <returns>点击的按钮文本</returns>
        public static WarningWindow Show(string content, string title, IEnumerable<string> buttons, WebSocketMessageModel _warning, bool isVisCloseBut = true, Window owner = null)
        {
            WarningWindow WaningWindow;
            string clikename = string.Empty;
            WaningWindow = new WarningWindow(_warning);
            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                WaningWindow = new WarningWindow(_warning);
                WaningWindow.DataContext = new { Title = title, Content = content, IsClose = isVisCloseBut };
                WaningWindow.spacingPanel.Children.Clear();

                foreach (var item in buttons)
                {
                    Button button = new Button()
                    {
                        Content = item,
                    };
                    button.Margin = new Thickness(10, 0, 10, 0);
                    button.FontSize = 18;
                    if (item == "屏蔽库位")
                    {
                        button.Foreground = Brushes.Red;
                        button.FontWeight = FontWeights.DemiBold;
                        //WaningWindow.Content = WaningWindow.Content + "\r\n确认对应无物料可点击[屏蔽库位]按钮进行库位屏蔽！";
                    }

                    button.Click += (s, e) =>
                    {
                        clikename = ((Button)s).Content.ToString();
                        Logs.Write($"用户{LocalStatic.CurrentUser}在处理异常报警{_warning.Guid}时点击了" + clikename);
                        #region 调用接口处理异常/消除报警
                        try
                        {
                            var solveType = SolveTypeEnum.忽略;
                            switch (clikename)
                            {
                                case "确认":
                                    solveType = SolveTypeEnum.处理;
                                    break;
                                case "忽略":
                                    solveType = SolveTypeEnum.忽略;
                                    break;
                                case "屏蔽库位":
                                    solveType = SolveTypeEnum.屏蔽库位;
                                    break;
                                default:
                                    solveType = SolveTypeEnum.忽略;
                                    break;
                            }
                            var body = new SolveWarningRequest()
                            {
                                Guid = _warning.Guid,
                                SolveType = solveType,
                                UserName = LocalStatic.CurrentUser,
                                DeviceType = "WCS前端",
                            };
                            var Result = ApiHelp.GetDataFromHttp<ResponseCommon<object>>(LocalFile.Config.ApiIpHost + "warning/solveWarning", body, "POST");
                            if (Result != null && Result.Code == 200)
                            {
                                //消除异常或已不存在该异常
                                WaningWindow.Close();
                            }
                            else if (Result != null && !string.IsNullOrEmpty(Result.Message))
                            {
                                Growl.Warning(Result.Message);
                            }
                            else if (Result == null)
                            {
                                Growl.Warning("调用后台接口失败！");
                            }
                        }
                        catch (Exception ex)
                        {
                            Growl.Warning(ex.Message);
                        }
                        #endregion
                    };

                    WaningWindow.spacingPanel.Children.Add(button);
                }

                WaningWindow.Owner = owner == null ? System.Windows.Application.Current.MainWindow : owner;
                WaningWindow.Show();
            }));
            return WaningWindow;
        }

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