package com.phlx.wool.util;

import com.phlx.wool.Configs;
import com.phlx.wool.data.Repository;
import com.phlx.wool.db.DbUtil;
import com.phlx.wool.entity.DivisionEntity;
import com.phlx.wool.entity.VillusBuyEntity;
import com.phlx.wool.net.RetrofitClient;
import com.printer.sdk.Barcode;
import com.printer.sdk.PrinterConstants;
import com.printer.sdk.PrinterInstance;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.printer.sdk.PrinterConstants.Command;

import me.goldze.mvvmhabit.utils.SPUtils;

public class PrintLabel {

    //写绒毛购买打印内容
    public static void printVillusBuy(Repository model, PrinterInstance myPrinter, List<VillusBuyEntity> villusBuyEntities, CharSequence[] text) {

        if (text == null || text.length == 0) {
            return;
        }
        for (CharSequence batchNo : text) {
            VillusBuyEntity vbe = null;
            for (VillusBuyEntity villusBuyEntity : villusBuyEntities) {
                if (villusBuyEntity.getBuyBatch().equals(batchNo)) {
                    vbe = villusBuyEntity;
                    break;
                }
            }

            if (vbe != null) {

                DivisionEntity divisionEntity = model.getPertainForCode(vbe.getRevision()).get(0);
                String pertainName = "";
                if (divisionEntity != null) {
                    pertainName = divisionEntity.getName();
                }
                myPrinter.setFont(0, 1, 1, 1, 1);
                myPrinter.setPrinter(Command.ALIGN, Command.ALIGN_CENTER);
                myPrinter.printText("绒毛购买信息\r\n");
                myPrinter.setPrinter(Command.ALIGN, Command.ALIGN_LEFT);
                myPrinter.setLeftMargin(8);
                myPrinter.setFont(0, 0, 0, 0, 1);
//            myPrinter.printText(" " + "\r\n");
                myPrinter.printText("批次:" + vbe.getBuyBatch() + "\r\n");
                myPrinter.printText("苏木:" + pertainName + "\r\n");
                myPrinter.printText("重量:" + vbe.getBuyWeight() + "（千克）\r\n");
                myPrinter.printText("厂家:" + vbe.getBuyFactory() + "\r\n");

                myPrinter.printText("日期:" + vbe.getBuyDate() + "\r\n");
                myPrinter.printText("负责人:" + vbe.getPersonInCharge() + "\r\n");
                myPrinter.printText(" " + "\r\n");
                PrinterInstance.mPrinter.setPrinter(Command.ALIGN, Command.ALIGN_CENTER);
                String url = "http://" + SPUtils.getInstance().getString(Configs.SP_APP_IP, RetrofitClient.baseUrl) + ":" +
                        SPUtils.getInstance().getString(Configs.SP_APP_PORT, RetrofitClient.port)
                        + "/xlgl/qrYr/" + vbe.getProductId();
                Barcode barcode2 = new Barcode(PrinterConstants.BarcodeType.QRCODE, 2, 3, 6, url);
                PrinterInstance.mPrinter.printBarCode(barcode2);

                myPrinter.setPrinter(Command.ALIGN, Command.ALIGN_RIGHT);
                myPrinter.setLeftMargin(0);
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                myPrinter.printText(dateFormat.format(new Date()) + "\r\n");
                myPrinter.printText(" " + "\r\n");
                myPrinter.printText(" " + "\r\n");
                myPrinter.printText(" " + "\r\n");
                myPrinter.printText(" " + "\r\n");
                myPrinter.ringBuzzer(1);
            }
        }
    }
}
