using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar.Extensions;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_BasicInfoService;
using WIDESEAWCS_Common;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseController;
using WIDESEAWCS_DTO.PlacedBlockDTO;
using WIDESEAWCS_DTO.TaskInfo;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_Model.Models;

namespace WIDESEAWCS_Server.Controllers.BasicInfo
{
    [Route("api/Container")]
    [ApiController]
    public class ContainerController : ApiBaseController<IContainerService, Dt_Container>
    {
        private readonly IContainerRepository _containerRepository;
        private readonly IOrderContainerRepository _orderContainerRepository;
        private readonly IOrderDetailsRepository _orderDetailsRepository;
        private readonly IOrderrowsRepository _orderrowsRepository;
        private readonly IContainerItemRepository _containerItemRepository;
        public ContainerController(IContainerService service, IContainerRepository containerRepository, IOrderContainerRepository orderContainerRepository, IOrderDetailsRepository orderDetailsRepository, IOrderrowsRepository orderrowsRepository, IContainerItemRepository containerItemRepository) : base(service)
        {
            _containerRepository = containerRepository;
            _orderContainerRepository = orderContainerRepository;
            _orderDetailsRepository = orderDetailsRepository;
            _orderrowsRepository = orderrowsRepository;
            _containerItemRepository = containerItemRepository;
        }

        [HttpPost, HttpGet, Route("GetTaskPosition"), AllowAnonymous]
        public TaskPosition GetTaskPosition([FromBody] Point3D point3D, int length, int width, int height, int edge)
        {
            PlaceBlockService placeBlockService = new PlaceBlockService(new ContainerSize(), null);
            return placeBlockService.GetTaskPosition(point3D, length, width, height, edge);
        }

        [HttpPost, HttpGet, Route("GetPutStations"), AllowAnonymous]
        public WebResponseContent GetPutStations(string deviceCode)
        {
            try
            {
                List<Dt_Container> containers = _containerRepository.QueryData(x => x.DeviceCode == deviceCode && x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt());
                List<object> list = new List<object>();
                foreach (var container in containers)
                {
                    List<string> containerItemCodes = _containerItemRepository.QueryData(x => x.ContainerId == container.Id).Select(x => x.ItemCode).ToList();

                    Dt_OrderContainer orderContainer = _orderContainerRepository.QueryFirst(x => x.ContainerId == container.Id && x.ContainerCode == container.ContainerCode);
                    if (orderContainer != null)
                    {
                        Orderrows orderrows = _orderrowsRepository.QueryFirst(x => x.id == orderContainer.OrderId);
                        if (orderrows != null)
                        {
                            List<OrderDetails> totalDetails = _orderDetailsRepository.QueryData(x => x.Orderrowsid == orderrows.id);
                            List<OrderDetails> details = totalDetails.Where(x => x.Orderrowsid == orderrows.id && x.Orderdetails_status == PalletingStatusEnmu.PalletingSuccess.ObjToInt() && containerItemCodes.Contains(x.Orderdetails_outid)).ToList();

                            int sortedNum = totalDetails.Where(x => x.Orderrowsid == orderrows.id && x.Orderdetails_status == PalletingStatusEnmu.PalletingSuccess.ObjToInt()).Count();

                            List<object> orderData = new List<object>();
                            foreach (var item in details)
                            {
                                object obj = new
                                {
                                    name = item.Orderdetails_name,
                                    baseName = item.Orderdetails_productName,
                                    size = $"{item.Orderdetails_length}*{item.Orderdetails_width}*{item.Orderdetails_thickness}",
                                    process = "",
                                };
                                orderData.Add(obj);
                            }
                            object data = new
                            {
                                orderCode = orderrows.Orderrows_orderid,
                                orderName = orderrows.Orderrows_name,
                                cusName = orderrows.Orderrows_customer,
                                stationCode = container.ContainerCode,
                                orderTotalNum = totalDetails.Count,
                                sortedNum = sortedNum,
                                unsortedNum = totalDetails.Count - sortedNum,
                                stationSortedNum = details.Count,
                                orderData = orderData,
                                orderId = orderrows.id
                            };
                            list.Add(data);
                        }
                        else
                        {
                            object data = new
                            {
                                stationSortedNum = 0,
                                stationCode = container.ContainerCode,
                                orderTotalNum = 0,
                                sortedNum = 0,
                                unsortedNum = 0,
                                orderData = new List<object>()
                            };
                            list.Add(data);
                        }
                    }
                    else
                    {
                        object data = new
                        {
                            stationCode = container.ContainerCode,
                            orderTotalNum = 0,
                            sortedNum = 0,
                            unsortedNum = 0,
                            stationSortedNum = 0,
                            orderData = new List<object>()
                        };
                        list.Add(data);
                    }
                }

                return WebResponseContent.Instance.OK(data: list);
            }
            catch (Exception ex)
            {
                return new WebResponseContent()
                {
                    Code = 500,
                    Message = ex.Message
                };
            }
        }

        [HttpPost, HttpGet, Route("ReleaseContainer"), AllowAnonymous]
        public WebResponseContent ReleaseContainer([FromBody] int[] keys)
        {
            return Service.AutoReleaseContainer(keys);
        }
    }
}