using BO.BO.Bill;
using Forks.JsonRpc.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using BO.Utils;

namespace BO.Utils.BillRpc
{
  public static class OrderDetailRpc
  {
    static JavaScriptSerializer serializer = new JavaScriptSerializer();
    public static List<NeedOrderEntity> GetNeedOrderWeightBill(DateTime date, bool? showType = null)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetNeedOrderWeightBill";
      var result = RpcFacade.Call<string>(method, date, showType);
      result = result.ESerializeDateTime();
      return serializer.Deserialize<List<NeedOrderEntity>>(result);
    }

    public static int GetLastNumber(long weightId, long orerID)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetLastNumber";
      return RpcFacade.Call<int>(method, weightId, orerID);
    }

    public static int GetMaxOrder(DateTime date)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetMaxOrder";
      return RpcFacade.Call<int>(method, date);
    }

    public static int GetCurrentOrder(long id)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetCurrentOrder";
      return RpcFacade.Call<int>(method, id);
    }

    public static List<OrderDetail> GetOrderDetail(DateTime date)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetOrderDetail";
      var result = RpcFacade.Call<string>(method, date);
      result = result.ESerializeDateTime();
      return serializer.Deserialize<List<OrderDetail>>(result);
    }

    public static void Insert(OrderDetail insert)
    {
      insert.AccountingUnit_ID = ButcherAppContext.Context.UserConfig.AccountingUnit_ID;
      insert.Creator = ButcherAppContext.Context.UserConfig.UserName;
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/Insert";
      insert.ID = RpcFacade.Call<long>(method, serializer.Serialize(insert));
    }

    public static void UpdateNumber(long id, int number)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/UpdateNum";
      RpcFacade.Call<int>(method, id, number);
    }

    public static void Delete(long id)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/Delete";
      RpcFacade.Call<int>(method, id);
    }

    public static void ChangeShowType(long weightBillID, bool show)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/ChangeShowType";
      RpcFacade.Call<int>(method, weightBillID, show);
    }

    public static List<HurryRecord> GetHurryRecordList(long weightBillID)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetHurryRecordList";
      var result = RpcFacade.Call<string>(method, weightBillID);
      result = result.ESerializeDateTime();
      return serializer.Deserialize<List<HurryRecord>>(result);
    }

    public static bool DeleteHurryRecord(long id)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/DeleteHurryRecord";
      return RpcFacade.Call<long?>(method, id) == -1;
    }

    public static HurryRecord InsertHurryRecord(SelectHurryList entity, out bool withError)
    {
      var detail = new HurryRecord();
      detail.HurryNumber = entity.HurryNumber;
      detail.Time = DateTime.Now;
      detail.WeightBill_ID = entity.WeightBill_ID;
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/InsertHurryRecord";
      var r = RpcFacade.Call<string>(method, serializer.Serialize(detail)).Split(new char[] { '|' });
      withError = r[0] == "-1";
      detail.ID = long.Parse(r[1]);
      detail.Supplier_Name = entity.Supplier_Name;
      detail.B3WeighBill_ID = entity.B3WeighBill_ID;
      detail.LiveColonyHouse_Name = entity.LiveColonyHouse_Name;
      return detail;
    }

    public static int GetHurryRecordNumber(long weightBillId)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetHurryRecordNumber";
      return RpcFacade.Call<int>(method, weightBillId);
    }

    public static List<SelectHurryList> GetSelectHurryList(DateTime butcherDate)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetSelectHurryList";
      return serializer.Deserialize<List<SelectHurryList>>(RpcFacade.Call<string>(method, butcherDate));
    }

    public static void SetOrderState(long id, int state)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/SetOrderState";
      RpcFacade.Call<int>(method, id, state);
    }

    public static HurryRecord GetHurryRecord(long id)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetHurryRecord";
      var result = RpcFacade.Call<string>(method, id);
      return serializer.Deserialize<HurryRecord>(result);
    }

    public static void InsertByHurryRecord(OrderDetail orderDetail, long hurryOrderID)
    {
      orderDetail.AccountingUnit_ID = ButcherAppContext.Context.UserConfig.AccountingUnit_ID;
      orderDetail.Creator = ButcherAppContext.Context.UserConfig.UserName;
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/InsertByHurryRecord";
      orderDetail.ID = RpcFacade.Call<long>(method, serializer.Serialize(orderDetail), hurryOrderID);
    }

    public static int GetCurrentOrderPlanNumber(long orderID)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetCurrentOrderPlanNumber";
      return RpcFacade.Call<int>(method, orderID);
    }

    public static int GetFinishNumbers(DateTime date)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/OrderDetailRpc/GetFinishNumbers";
      return RpcFacade.Call<int>(method, date);
    }

    public static List<DropPigOrderList> GetDropPigOrderList(DateTime date)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/DropPigRpc/GetDropPigOrderList";
      var result = RpcFacade.Call<string>(method, date);
      result = result.ESerializeDateTime();
      return serializer.Deserialize<List<DropPigOrderList>>(result);
    }

    public static int InsertDropPig(long orderID, int number, short type)
    {
      const string method = "/MainSystem/B3ClientService/Rpcs/BillRpc/DropPigRpc/InsertDropPig";
      return RpcFacade.Call<int>(method, orderID, number, type);
    }
  }
}
