package xn.hxp.utils;

import android.graphics.Bitmap;
import android.util.Log;

import com.blankj.utilcode.util.LogUtils;
import com.caysn.autoreplyprint.AutoReplyPrint;
import com.sun.jna.Pointer;

import xn.hxp.R;
import xn.hxp.ui.PrintBean;

public enum PrintTool {
    INSTANCE;

    private Pointer pointer;

    public void print(boolean isBelong, PrintBean printBean) {
        try {
            AutoReplyPrint.INSTANCE.CP_Port_AddOnPortOpenedEvent((handle, name, private_data) -> {
                LogUtils.d("打印机打开成功");
                Bitmap bitmap = BitmapUtils.INSTANCE.generateBitmap(isBelong, printBean);
                AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(pointer);
                LogUtils.d("打印机退纸成功");
                AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(pointer,
                        bitmap.getWidth(),
                        bitmap.getHeight(),
                        bitmap,
                        AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
                        AutoReplyPrint.CP_ImageCompressionMethod_None
                );
                LogUtils.d("打印机打印图片成功");
                AutoReplyPrint.INSTANCE.CP_Label_PagePrint(pointer, 1);
                LogUtils.d("打印机文字成功");
                if (AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(pointer)) {
                    LogUtils.d("打印机切割成功");
                    AudioPlayer.getInstance().play(R.raw.huaxuepinbiaoqian);
                }
            }, Pointer.NULL);
            LogUtils.d("打开打印机");
            pointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1);
        } catch (Exception e) {
            LogUtils.d(Log.getStackTraceString(e));
        }
    }

    public void print(String string) {
        try {
            AutoReplyPrint.INSTANCE.CP_Port_AddOnPortOpenedEvent((handle, name, private_data) -> {
                Bitmap bitmap = BitmapUtils.INSTANCE.airBottlePrint(string);
                AutoReplyPrint.INSTANCE.CP_Label_BackPaperToPrintPosition(pointer);
                AutoReplyPrint.CP_Pos_PrintRasterImageFromData_Helper.PrintRasterImageFromBitmap(pointer,
                        bitmap.getWidth(),
                        bitmap.getHeight(),
                        bitmap,
                        AutoReplyPrint.CP_ImageBinarizationMethod_Thresholding,
                        AutoReplyPrint.CP_ImageCompressionMethod_None
                );
                AutoReplyPrint.INSTANCE.CP_Label_PagePrint(pointer, 1);
                if (AutoReplyPrint.INSTANCE.CP_Pos_HalfCutPaper(pointer)) {
                    AudioPlayer.getInstance().play(R.raw.zhantiefrid);
                }
                AutoReplyPrint.INSTANCE.CP_Port_Close(pointer);
            }, Pointer.NULL);
            pointer = AutoReplyPrint.INSTANCE.CP_Port_OpenUsb("VID:0x0FE6,PID:0x811E", 1);
        } catch (Exception e) {
            LogUtils.d(Log.getStackTraceString(e));
        }
    }

}
