package com.lukouguoji.gnc.bluetooth.bean;

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

import java.util.ArrayList;

public class TSPL {
  String TAG="TSPL";
  int maxWidthInMM=800;
  int maxHeightInMM=600;
    ///写入指令
    public byte[] tspl_writeCmd(String cmd, boolean end) {
        if (TextUtils.isEmpty(cmd)) {
            return null;
        }
        cmd = end ? (cmd + "\r\n") : cmd;
        Log.d(TAG, "tspl_writeCmd: " + cmd);
        byte[] data;
        try {
            data = cmd.getBytes();
        } catch (Exception var4) {
            Log.e(TAG, "String getBytes('GBK') failed");
            return null;
        }
        return data;
    }

    ///设置纸张和gap
    public ArrayList<byte[]>  tspl_pageSetup(int widthInMM, int heightInMM, int gapInMM) {
        ArrayList<byte[]> list=new ArrayList<>();
        if (widthInMM <= 0 || heightInMM <= 0) {
            return list;
        }
        if (widthInMM > maxWidthInMM) widthInMM = maxWidthInMM;
        if (heightInMM > maxHeightInMM) heightInMM = maxHeightInMM;
        String cmd = String.format("SIZE %d mm, %d mm", widthInMM, heightInMM);
        list.add( tspl_writeCmd(cmd, true));
        list.add(  tspl_writeCmd(String.format("GAP %d mm,0", gapInMM), true));
        return list;
    }

    ///绘制图片
    ///1. 算法中对图片以2kb大小进行了切割，防止打印机内存不足，可以自行修改此值
    ///2. 算法中用经验公式对图片进行了二值化处理，每8个点为1mm
    public ArrayList<byte[]> tspl_drawGraphic(int start_x, int start_y, Bitmap bmp) {
        ArrayList<byte[]> list=new ArrayList<>();
        int bmp_size_x = bmp.getWidth();
        int bmp_size_y = bmp.getHeight();
        int byteWidth = (bmp_size_x - 1) / 8 + 1; //8个点一个byte,所以要先计算byteWidth
        int byteHeight = bmp_size_y;
        if (byteWidth <= 0) return null;
        if (byteHeight <= 0) return null;
        int unitHeight = 2048 / byteWidth;//分包规则，算法可无需过多理解，一个包就unitHeight*byteWidth个点
        int unitCount = (byteHeight - 1) / unitHeight + 1;
        int startY, endY;//每个包y方向起始的位置
        byte[] dataByte;
        int color, A, R, G, B;
        for (int n = 0; n < unitCount; n++) {
            Log.d(TAG, "drawGraphic unit=" + (n + 1) + "/" + unitCount);
            startY = n * unitHeight;
            if ((startY + unitHeight) > bmp_size_y) {
                endY = bmp_size_y;
            } else {
                endY = startY + unitHeight;
            }
            int byteLen = (endY - startY) * byteWidth;//每个包数据的字节个数
            dataByte = new byte[byteLen];
            for (int y = startY; y < endY; y++) {
                for (int x = 0; x < bmp_size_x; x++) {
                    color = bmp.getPixel(x, y);
                    A = color >> 24 & 0xFF;
                    R = color >> 16 & 0xFF;
                    G = color >> 8 & 0xFF;
                    B = color & 0xFF;
                    //经验公式二值化，透明视为白色
                    if (A == 0 || R * 0.3 + G * 0.59 + B * 0.11 > 127) {
                        dataByte[(y - startY) * byteWidth + x / 8] |= (0x80 >> (x % 8));
                    }
                }
            }
            int curUnitHeight = n == unitCount - 1 ? (byteHeight - (n * unitHeight)) : unitHeight;
            String cmdHeader = "BITMAP " + start_x + "," + (start_y + startY) + "," + byteWidth + "," + curUnitHeight + "," + 0 + ",";
            list.add(tspl_writeCmd(cmdHeader, false) );
            list.add(dataByte);
            list.add(new byte[]{0x0D, 0x0A});

        }
        return list;
    }

    /// 打印
    public byte[] tspl_print(int count, boolean isReverse) {
        if (count < 0) count = 1;
        if (count > 65535) count = 65535;
        String cmd = String.format("PRINT %d,1", count);
        return tspl_writeCmd(cmd, true);
    }

}
