using CrazyStudio.Core.Common.Eitities.Page;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using XPrint.Image.Tools;
using XPrintServer.Api.Dto;
using XPrintServer.Api.Hubs;
using XPrintServer.Business.Enums;
using XPrintServer.Business.Services;
using XPrintServer.DataModel.Models;

namespace XPrintServer.Api.Controllers.Common
{

    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrderInfoController : ControllerBase
    {

        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly OrderService _orderService;
        private readonly OrderInfoService _orderInfoService;
        private readonly ActionLogService _actionLogService;
        private readonly IHubContext<MsgHub> _hubContext;

        public OrderInfoController(IHttpContextAccessor httpContextAccessor, OrderService orderService, OrderInfoService orderInfoService, ActionLogService actionLogService, IHubContext<MsgHub> hubContext)
        {
            _httpContextAccessor = httpContextAccessor;
            _orderService = orderService;
            _orderInfoService = orderInfoService;
            _actionLogService = actionLogService;
            _hubContext = hubContext;
        }


        [HttpPost]
        public async Task<ActionResult<PageResult<OrderInfo>>> OrderInfoList([FromBody] PageOrderStatusData pageData)
        {
            try
            {
                //var now = DateTime.Now;
                //if (pageData.StartTime == null)
                //{
                //    pageData.StartTime = now.AddDays(-7);
                //    pageData.EndTime = now;
                //}

                //查询一周内的已支付订单
                var infoResult = await _orderInfoService.GetOrderInfoListForTime(pageData, pageData.StartTime, pageData.EndTime);
                return Ok(infoResult);
            }
            catch (Exception ex)
            {
                await _actionLogService.AddActionLog(new ActionLog { Title = "OrderList Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }


        /// <summary>
        /// 批量更改资源状态为已生产
        /// </summary>
        /// <param name="pageData"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<bool>> ProductionBatchFinish([FromBody] HashSet<Guid> orderInfoIds)
        {
            try
            {
                //开始事务
                await _orderService.Context.Ado.BeginTranAsync();
                //查询12个月的已支付订单
                var finishOrderIds = await _orderInfoService.BatchChangeOrderInfoStatusToProductionFinish(orderInfoIds, ResourceStatus.ProductionFinish);
                bool isOK = await _orderService.BatchChangeOrderStatusToProductionStatus(finishOrderIds, OrderStatus.ProductionFinish);

                //提交事务
                await _orderService.Context.Ado.CommitTranAsync();
                return Ok(isOK);
            }
            catch (Exception ex)
            {
                await _orderService.Context.Ado.RollbackTranAsync();
                await _actionLogService.AddActionLog(new ActionLog { Title = "ProductionBatchFinish Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }

        /// <summary>
        /// 批量更改资源状态为已生产
        /// </summary>
        /// <param name="pageData"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<bool>> ProductionBatchDelete([FromBody] HashSet<Guid> orderInfoIds)
        {
            try
            {
                //开始事务
                await _orderService.Context.Ado.BeginTranAsync();
                //查询已支付订单
                var finishOrderIds = await _orderInfoService.BatchChangeOrderInfoStatusToProductionFinish(orderInfoIds, ResourceStatus.Delete);
                bool isOK = await _orderService.BatchChangeOrderStatusToProductionStatus(finishOrderIds, OrderStatus.ProductionFinish);

                //提交事务
                await _orderService.Context.Ado.CommitTranAsync();
                return Ok(isOK);
            }
            catch (Exception ex)
            {
                await _orderService.Context.Ado.RollbackTranAsync();
                await _actionLogService.AddActionLog(new ActionLog { Title = "ProductionBatchFinish Error", Content = ex.Message + " " + ex.StackTrace, DateTime = DateTime.Now });
                return StatusCode(500, ex.Message);
            }
        }
    }
}
