using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.SelfCheck;
using WCS.Model.WebSocketModel;

namespace WCS.BLL.Services.Service
{
    public class SelfCheckService : ISelfCheckService
    {
        public async Task<ResponseBase> StartSelfCheckByShelfCode(StartSelfCheckByShelfCodeRequest request)
        {
            //获取货架
            var shelfs = ShelfManager.Shelves
                                    .Where(t => request.ShelfCodes.Contains(t.ShelfCode))
                                    .ToList();

            if (shelfs.Count < request.ShelfCodes.Count)
            {
                foreach (var shelf in shelfs)
                {
                    request.ShelfCodes.RemoveAll(t => t == shelf.ShelfCode);
                }
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = $"货架{string.Join(",", request.ShelfCodes)}不存在!",
                };
            }

            foreach (var shelf in shelfs)
            {
                shelf.ShelfCheck();
            }
            return new ResponseCommon()
            {
                Code = 200,
                Message = $"货架{string.Join(",", request.ShelfCodes)}已开始自检!",
            };
        }

        public async Task<ResponseBase> StartSelfCheckByGroupName(List<string> GroupNames)
        {
            if (GroupNames == null || GroupNames.Count == 0)
            {
                return new ResponseCommon()
                {
                    Code = 201,
                    Message = $"参数为空！",
                };
            }
            //获取货架
            var shelfs = ShelfManager.Shelves
                                    .Where(t => GroupNames.Contains(t.GroupName))
                                    .ToList();
            var cleintIps = shelfs.Select(t => t.ClientIp).Distinct().ToList();

            foreach (var ip in cleintIps)
            {
                var currentIdShelfs = shelfs.Where(t => t.ClientIp == ip)
                                            .Where(t => t.CurrentMode == HardWare.Mode.待机模式)
                                            .ToList();
                Task.Run(() =>
                {
                    foreach (var shelf in currentIdShelfs)
                    {

                        Task.Run(() =>
                        {
                            var warningModel = new WebSocketMessageModel()
                            {
                                IsWarning = false,
                                ClientIsReceived = true,
                                WarningType = WarningTypeEnum.通知自检进度,
                                StoreId = 0,
                                StoreCode = "",
                                ShelfCode = shelf.ShelfCode,
                                ShelfId = shelf.ShelfId,
                                ClientIp = shelf.WebSocketIpAddress,
                                WarningMessage = $"货架【{shelf.ShelfCode}】开始自检"
                            };
                            WarningManager.SendWarning(warningModel);
                        });

                        shelf.ShelfCheck();

                        //Thread.Sleep(8);

                        Task.Run(() =>
                        {
                            var warningModel = new WebSocketMessageModel()
                            {
                                IsWarning = false,
                                ClientIsReceived = true,
                                WarningType = WarningTypeEnum.通知自检进度,
                                StoreId = 0,
                                StoreCode = "",
                                ShelfCode = shelf.ShelfCode,
                                ShelfId = shelf.ShelfId,
                                ClientIp = shelf.WebSocketIpAddress,
                                WarningMessage = $"货架【{shelf.ShelfCode}】自检命令发送完毕"
                            };
                            WarningManager.SendWarning(warningModel);
                        });
                        Thread.Sleep(8);
                    }
                });
            }
            return new ResponseCommon()
            {
                Code = 200,
                Message = $"已成功开始自检！",
            };
        }


    }
}
