using BO.BO.Bill;
using BWP.WinFormControl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QualityAndOrder
{
  public static class HurryRecordPrint
  {
    public static void Print(HurryRecord entity)
    {
      entity.LiveColonyHouse_Name = "B-12-1,B-12-2,B-12-3,B-12-4";

      PrintAPI.B_GetUSBBufferLen();
      PrintAPI.B_EnumUSB(new byte[128]);
      PrintAPI.B_CreateUSBPort(1);
      PrintAPI.B_Prn_Text_TrueType(255, 15, 38, "宋体", 1, 700, 0, 0, 0, "C1", "急宰单");
      PrintAPI.B_Bar2d_QR(40, 75, 2, 12, 'M', 'A', 0, 0, 0, string.Format("s{0}e", entity.ID));

      PrintAPI.B_Prn_Text_TrueType(300, 90, 32, "宋体", 1, 600, 0, 0, 0, "C2", string.Format("磅单号：{0}", entity.B3WeighBill_ID));
      PrintAPI.B_Prn_Text_TrueType(300, 135, 32, "宋体", 1, 600, 0, 0, 0, "C3", string.Format("供应商：{0}", entity.Supplier_Name));
      PrintAPI.B_Prn_Text_TrueType(300, 180, 32, "宋体", 1, 600, 0, 0, 0, "C4", string.Format("头  数：{0}", entity.HurryNumber));
      var names = GetHouseNames(entity.LiveColonyHouse_Name);
      PrintAPI.B_Prn_Text_TrueType(300, 225, 32, "宋体", 1, 600, 0, 0, 0, "C5", string.Format("圈  舍：{0}", names.Any() ? names[0] : string.Empty));
      names.RemoveAt(0);
      var p = 6;
      var x = 270;
      foreach (var v in names)
      {
        PrintAPI.B_Prn_Text_TrueType(303, x, 32, "宋体", 1, 600, 0, 0, 0, "C" + p, string.Format("       {0}", names.Any() ? names[0] : string.Empty));
        p += 1;
        x += 45;
      }
      PrintAPI.B_Prn_Text_TrueType(300, x, 32, "宋体", 1, 600, 0, 0, 0, "C" + p, string.Format("时  间：{0}", entity.Time.ToString("MM/dd HH:mm")));

      PrintAPI.B_Set_Direction('B');
      PrintAPI.B_Print_Out(1);
      PrintAPI.B_ClosePrn();
    }

    static List<string> GetHouseNames(string houseName)
    {
      var rst = new List<string>();
      if (string.IsNullOrEmpty(houseName))
        return rst;
      var arr = houseName.Split(new char[] { ',' });
      var temp = string.Empty;
      foreach (var item in arr)
      {
        if(string.IsNullOrEmpty(temp))
          temp = item;
        else
        {
          rst.Add(string.Format("{0},{1}", temp, item));
          temp = string.Empty;
        }
      }
      if (!string.IsNullOrEmpty(temp))
        rst.Add(temp);
      return rst;
    }
  }
}
