using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace WeighAndGrading
{
  public static class PrintAPI
  {
    [DllImport("Winpplb.dll")]
    private static extern int B_GetUSBBufferLen();

    [DllImport("Winpplb.dll")]
    private static extern int B_EnumUSB(byte[] buf);

    [DllImport("Winpplb.dll")]
    private static extern int B_CreateUSBPort(int nPort);

    [DllImport("Winpplb.dll")]
    private static extern int B_Set_Direction(char direction);

    [DllImport("Winpplb.dll")]
    private static extern int B_Prn_Text_TrueType(int x, int y, int FSize, string FType,
        int Fspin, int FWeight, int FItalic, int FUnline, int FStrikeOut, string id_name,
        string data);

    [DllImport("Winpplb.dll")]
    private static extern int B_Prn_Barcode(int x, int y, int ori, string type, int narrow,
        int width, int height, char human, string data);

    [DllImport("Winpplb.dll")]
    private static extern int B_Bar2d_QR(int x, int y, int model, int scl, char error,
        char dinput, int c, int d, int p, string data);

    [DllImport("Winpplb.dll")]
    private static extern int B_Print_Out(int labset);

    [DllImport("Winpplb.dll")]
    private static extern void B_ClosePrn();


    public static void Print(PrintEntity entity)
    {
      B_GetUSBBufferLen();
      B_EnumUSB(new byte[128]);
      B_CreateUSBPort(1);

      B_Prn_Text_TrueType(110, 31, 31, "宋体", 1, 700, 0, 0, 0, "C1", entity.AccountingUnit_Name);
      B_Prn_Text_TrueType(42, 75, 31, "宋体", 1, 700, 0, 0, 0, "C2", "合格证");
      B_Prn_Text_TrueType(440, 75, 31, "宋体", 1, 700, 0, 0, 0, "C3", entity.Goods_Name);

      B_Prn_Text_TrueType(42, 125, 27, "宋体", 1, 400, 0, 0, 0, "C4", string.Format("生产日期：{0}", entity.Date.ToString("yyyy/MM/dd")));
      B_Prn_Text_TrueType(42, 165, 27, "宋体", 1, 400, 0, 0, 0, "C5", string.Format("检 验 员：{0}", entity.Checker));
      B_Prn_Text_TrueType(42, 205, 27, "宋体", 1, 400, 0, 0, 0, "C6", string.Format("保 质 期：{0}", entity.StoreCondition));

      B_Prn_Text_TrueType(42, 255, 23, "宋体", 1, 400, 0, 0, 0, "C7", string.Format("产地：{0} 电话：{1}", entity.Place, entity.TelNumber));
      B_Prn_Text_TrueType(42, 290, 23, "宋体", 1, 400, 0, 0, 0, "C8", string.Format("地址：{0}", entity.Address));

      B_Prn_Barcode(42, 330, 0, "1", 3, 22, 35, 'N', entity.BarCode);
      B_Prn_Text_TrueType(175, 370, 25, "宋体", 1, 500, 0, 0, 0, "C9", entity.BarCode);
      B_Bar2d_QR(430, 120, 2, 7, 'M', 'A', 0, 0, 0, entity._2DQRCode);

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

  public class PrintEntity
  {
    public string AccountingUnit_Name { get; set; }

    public string Goods_Name { get; set; }

    public DateTime Date { get; set; }

    public string Checker { get; set; }

    public string StoreCondition { get; set; }

    public string Place { get; set; }

    public string TelNumber { get; set; }

    public string Address { get; set; }

    public string BarCode { get; set; }

    public string _2DQRCode { get; set; }
  }
}
