package com.genlot.ninglottery.module.hardwaretest;

import android.os.Bundle;

import com.blankj.utilcode.util.ToastUtils;
import com.genlot.devicesdk.Printer;
import com.genlot.nativesdk.SystemNI;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.base.BaseFragment;
import com.genlot.ninglottery.game.GameManager;
import com.genlot.ninglottery.model.Constant;
import com.genlot.ninglottery.toolkit.LogTool;
import com.genlot.ninglottery.toolkit.StringTool;
import com.genlot.ninglottery.toolkit.TimeUtils;

import java.util.Locale;

import butterknife.OnClick;

/**
 * @author ZhuYongKang
 * @description 类描述
 * @date 2019/10/31
 */
public class PrintTestFragment extends BaseFragment {

    @OnClick(R.id.bt_confirm)
    public void print() {
        Printer.preload(Printer.DEVICE_YK_T91, Printer.INTERFACE_USB);
        Printer.preloadUSBAttr(Constant.PRINT_VID, Constant.PRINT_PID);
        Printer.openPrinter(activity);
        Printer.initPrinter();
        //打印特征码
        Printer.setFontSize(0);
        Printer.setLeftMargin(50);
        String featureCode = "43BE48917EF1E92BD6659412B789";
        StringBuilder feature = new StringBuilder();
        feature.append(featureCode);
        feature.insert(4, "-");
        feature.insert(9, "-");
        feature.insert(14, "-");
        feature.insert(19, "-");
        feature.insert(24, "-");
        feature.insert(29, "-");
        Printer.printString(feature.toString());
        Printer.lineFeed(1);

        //打印图标
        Printer.setFontSize(17);
        Printer.setFontBold(1);//1 加粗
        Printer.printString("中 国 福 利 彩 票");
        Printer.lineFeed(1);

        //打印游戏名称
        Printer.setFontAlignment(1);//居中
        Printer.setFontSize(16);//00010000 高四位表示宽度,低四位表示高度
        Printer.printString("打印机自检");
        Printer.lineFeed(1);

        //打印玩法 期号
        Printer.setFontAlignment(0);//居左
        Printer.setLeftMargin(10);
        Printer.setFontSize(0);
        Printer.setFontBold(0);
        String str = "期号:" + 191001001;
        Printer.printString(str);
        Printer.setAbsX(200);
        str = "多  期:" +  1;
        Printer.printString(str);
        Printer.setAbsX(350);
        str = "玩法:" + "单式";
        Printer.printString(str);
        Printer.lineFeed(1);

        //打印站号 流水号
        str = "站号:" + SystemNI.getTerminalID();
        Printer.printString(str);
        Printer.setAbsX(200);
        str = "流水号:" + 0;
        Printer.printString(str);
        Printer.lineFeed(1);

        //打印金额 销售时间
        str = "金额:" + String.format(Locale.getDefault(),"%.2f", 10.00) + "元";
        Printer.printString(str);
        Printer.setAbsX(200);
        str = "销售时间:" + TimeUtils.getDetailTimeMil(System.currentTimeMillis());
        Printer.printString(str);
        Printer.lineFeed(1);
        String[] betNums = {"①", "②", "③", "④", "⑤"};
        //打印投注号码
        for (int i = 0; i < 5; i++) {
            Printer.printString(betNums[i] + " ");
            Printer.setFontSize(16);
            Printer.setFontBold(1);//1 加粗
            StringBuilder sb = new StringBuilder();
            String[] numRed = {"01","02","03","04","05","06"};
            for (String red : numRed) {
                sb.append(StringTool.format2(Integer.parseInt(red))).append(" ");
            }
            Printer.printString(sb.toString());
            Printer.setFontSize(0);
            Printer.setFontBold(0);
            if (i == 0) {
                Printer.printString("[" + 1 + "倍]");
            }
            Printer.lineFeed(1);
        }

        //打印开奖时间
        Printer.setFontSize(0);
        str = "开奖日期:" + TimeUtils.getDetailTimeMil(System.currentTimeMillis());
        Printer.printString(str);
        Printer.lineFeed(1);
        //打印站地址
        str = "站地址:" + GameManager.getStationAddress();
        Printer.printString(str);
        Printer.lineFeed(1);

        //打印验票码
        StringBuffer verifyCode = new StringBuffer();
        verifyCode.append("DAC49D8812341234AA22AA4433224455");
        verifyCode.insert(8, "-");
        verifyCode.insert(17, "-");
        verifyCode.insert(26, "-");

        str = "验票码:" + verifyCode.toString();
        Printer.printString(str);
        Printer.lineFeed(1);

        //公益金提示语
        Printer.printString("感谢你为公益事业贡献2元");
        Printer.lineFeed(2);
        //打印PDF417
        String srcString = featureCode + ";;;;";
        String encryptFeatureCode = SystemNI.rc4EncryptData(srcString);
        LogTool.logI("encryptFeatureCode=" + encryptFeatureCode);

        Printer.setPDF417(16, 10, 2, 4, 3);
        Printer.printPDF417(encryptFeatureCode);

        Printer.lineFeed(3);
        int ret = Printer.cutPaper();
        Printer.closePrinter();
        if (ret == 0) {
            ToastUtils.showShort("打印机正常工作");
        } else {
            ToastUtils.showShort("打印机异常,请检查！");
        }
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_print_test;
    }

    @Override
    public void onCreateViewFinished(Bundle state) {

    }
}
