using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HBLConsole.Abstract;
using BPA.Message;
using HBLConsole.Model;
using HBLConsole.Service;
using BPA.Message.Enum;
using HBLConsole.GVL;
using BPA.Utility;
using Newtonsoft.Json;

namespace HBLConsole.Business.MessageServer
{
    public class Base : AbstractMessageServer
    {
        public override void AddOrder<T>(T orderInfo)
        {
            if (orderInfo == null) return;
            if (orderInfo is MorkOrderPush morkOrderpush)
            {
                Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Add(new OrderData()
                {
                    OrderStatus = ORDER_STATUS.WAIT,
                    IsSelected = true,
                    OrderPush = morkOrderpush
                });
                ActionManagerment.GetInstance.Send("AddOrder", morkOrderpush);
                ActionManagerment.GetInstance.Send("MorksParse", morkOrderpush);
            }
        }

        public override void GetBatchingInfo<T>(T batchingInfo)
        {
            if (batchingInfo == null) return;
            if (batchingInfo is OrderMaterialDelivery BatchingInfos)
            {
                Json<BatchingInfoPar>.GetInstance.Base.orderMaterialDelivery = BatchingInfos;
            }
        }

        public override void GetBatchingInfo(int ClientId)
        {
            string result = string.Empty;
            try
            {
                var jsondata = new { ClientId };
                string header = $"[{InternetInfo.GetInstance.StockServer}/GetItemInfo]_[{DateTime.Now.Ticks}]".AESEncrypt();
                string url = $"{InternetInfo.GetInstance.ApiAddress}{InternetInfo.GetInstance.StockServer}/GetItemInfo";
                result = APIHelper.GetInstance.HttpRequest(url, header, jsondata, RequestType.POST);
            }
            catch (Exception ex)
            {
                MessageLog.GetInstance.Show(ex.ToString());
            }
            Json<BatchingInfoPar>.GetInstance.Base.orderMaterialDelivery = JsonConvert.DeserializeObject<OrderMaterialDelivery>(result);

            MessageLog.GetInstance.Show("【物料信息】");
            Json<BatchingInfoPar>.GetInstance.Base.orderMaterialDelivery?.BatchingInfo?.ForEach(x =>
            {
                MessageLog.GetInstance.Show($"{x.BatchingLoc}号位置：{x.BatchingCount}");
            });
        }

        public override void GetRecipeBom<T>(T recipeBomInfo)
        {
            if (recipeBomInfo == null) return;
            if (recipeBomInfo is RecipeBoms recipeBom)
            {
                Json<BatchingInfoPar>.GetInstance.Base.recipeBoms = recipeBom;
            }
            MessageLog.GetInstance.Show("接收到辅料信息");
        }

        public override bool OrderStatusChange(string subOrderId, ORDER_STATUS status)
        {
            string result = string.Empty;
            OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = status, SuborderId = subOrderId };
            try
            {
                string header = $"[{InternetInfo.GetInstance.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
                string url = $"{InternetInfo.GetInstance.ApiAddress}{InternetInfo.GetInstance.OrderServer}/order/robotstatuschange";
                result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
            }
            catch (Exception ex)
            {
                MessageLog.GetInstance.Show(ex.ToString());
            }
            var res = JsonConvert.DeserializeObject<OrderStatusChangeRsp>(result);
            return res?.Result == 2;
        }
    }
}
