package com.miya.print.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import com.blankj.utilcode.util.RegexUtils;

import java.nio.charset.Charset;

/**
 * Created by fengyu on 2018/2/23.
 */

public class Page88MmPrintUtils {

    /**
     * 打印纸一行最大的字节
     */
    private static final int LINE_BYTE_SIZE = 46;
    private static final int LINE_BIG_BYTE_SIZE = 35;
    private static final int LEFT_LENGTH = 28;

    private static final int RIGHT_LENGTH = 18;

    /**
     * 左侧汉字最多显示几个文字
     */
    private static final int LEFT_TEXT_MAX_LENGTH = 10;

    /**
     * 要是数字和字母最多显示15个
     */
    private static final int LEFT_TEXT_MAX_LENGTH_15 = 15;

    /**
     * 小票打印菜品的名称，上限调到8个字
     */
    public static final int MEAL_NAME_MAX_LENGTH = 10;

    /**
     * 复位打印机
     */
    public static final byte[] RESET = {0x1b, 0x40};

    /**
     * 左对齐
     */
    public static final byte[] ALIGN_LEFT = {0x1b, 0x61, 0x00};

    /**
     * 中间对齐
     */
    public static final byte[] ALIGN_CENTER = {0x1b, 0x61, 0x01};

    /**
     * 右对齐
     */
    public static final byte[] ALIGN_RIGHT = {0x1b, 0x61, 0x02};

    /**
     * 选择加粗模式
     */
    public static final byte[] BOLD = {0x1b, 0x45, 0x01};

    /**
     * 取消加粗模式
     */
    public static final byte[] BOLD_CANCEL = {0x1b, 0x45, 0x00};

    /**
     * 宽高加倍
     */
    public static final byte[] DOUBLE_HEIGHT_WIDTH = {0x1d, 0x21, 0x11};

    /**
     * 宽加倍
     */
    public static final byte[] DOUBLE_WIDTH = {0x1d, 0x21, 0x10};

    /**
     * 高加倍
     */
    public static final byte[] DOUBLE_HEIGHT = {0x1d, 0x21, 0x01};

    /**
     * 字体不放大
     */
    public static final byte[] NORMAL = {0x1d, 0x21, 0x00};

    /**
     * 设置默认行间距
     */
    public static final byte[] LINE_SPACING_DEFAULT = {0x1b, 0x32};

    /**
     * 设置行间距
     */
//	public static final byte[] LINE_SPACING = {0x1b, 0x32};//{0x1b, 0x33, 0x14};  // 20的行间距（0，255）


//	final byte[][] byteCommands = {
//			{ 0x1b, 0x61, 0x00 }, // 左对齐
//			{ 0x1b, 0x61, 0x01 }, // 中间对齐
//			{ 0x1b, 0x61, 0x02 }, // 右对齐
//			{ 0x1b, 0x40 },// 复位打印机
//			{ 0x1b, 0x4d, 0x00 },// 标准ASCII字体
//			{ 0x1b, 0x4d, 0x01 },// 压缩ASCII字体
//			{ 0x1d, 0x21, 0x00 },// 字体不放大
//			{ 0x1d, 0x21, 0x11 },// 宽高加倍
//			{ 0x1b, 0x45, 0x00 },// 取消加粗模式
//			{ 0x1b, 0x45, 0x01 },// 选择加粗模式
//			{ 0x1b, 0x7b, 0x00 },// 取消倒置打印
//			{ 0x1b, 0x7b, 0x01 },// 选择倒置打印
//			{ 0x1d, 0x42, 0x00 },// 取消黑白反显
//			{ 0x1d, 0x42, 0x01 },// 选择黑白反显
//			{ 0x1b, 0x56, 0x00 },// 取消顺时针旋转90°
//			{ 0x1b, 0x56, 0x01 },// 选择顺时针旋转90°
//	};

    /**
     * 打印两列
     *
     * @param leftText  左侧文字
     * @param rightText 右侧文字
     * @return
     */
    @SuppressLint("NewApi")
    public static String printTwoData(String leftText, String rightText) {
        StringBuilder sb = new StringBuilder();
        int leftTextLength = getBytesLength(leftText);
        int rightTextLength = getBytesLength(rightText);
        sb.append(leftText);

        // 计算两侧文字中间的空格
        int marginBetweenMiddleAndRight = LINE_BYTE_SIZE - leftTextLength - rightTextLength;

        for (int i = 0; i < marginBetweenMiddleAndRight; i++) {
            sb.append(" ");
        }
        sb.append(rightText);
        return sb.toString();
    }

    /**
     * 打印居中带分隔符的文字
     *
     * @return
     */
    @SuppressLint("NewApi")
    public static String printOneCenterData(String text) {
        StringBuilder sb = new StringBuilder();
        int leftTextLength = getBytesLength(text);
        // 计算两侧文字中间的空格
        int marginBetweenMiddleAndRight = (LINE_BYTE_SIZE - leftTextLength) / 2;

        for (int i = 0; i < marginBetweenMiddleAndRight; i++) {
            sb.append(" ");
        }
        sb.append(text);
        for (int i = 0; i < marginBetweenMiddleAndRight; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }

    /**
     * 中间是文字  两边是======
     *
     * @param centerText
     * @return
     */
    @SuppressLint("NewApi")
    public static String printCenterData(String centerText) {
        StringBuilder sb = new StringBuilder();
        int centerLength = getBytesLength(centerText);
        int leftLength = (LINE_BYTE_SIZE - centerLength) / 2;
        int rightLength = LINE_BYTE_SIZE - leftLength - centerLength;
        for (int i = 0; i < leftLength; i++) {
            sb.append("=");
        }
        sb.append(centerText);
        StringBuilder sa = new StringBuilder();
        for (int i = 0; i < rightLength; i++) {
            sa.append("=");
        }
        sb.append(sa.toString());
        return sb.toString();
    }


    /**
     * 中间是文字
     *
     * @param centerText
     * @return
     */
    @SuppressLint("NewApi")
    public static String printCenterData(String centerText, String divideStr) {
        StringBuilder sb = new StringBuilder();
        int centerLength = getBytesLength(centerText);
        int leftLength = (LINE_BYTE_SIZE - centerLength) / 2;
        int rightLength = LINE_BYTE_SIZE - leftLength - centerLength;
        for (int i = 0; i < leftLength; i++) {
            sb.append(divideStr);
        }
        sb.append(centerText);
        StringBuilder sa = new StringBuilder();
        for (int i = 0; i < rightLength; i++) {
            sa.append(divideStr);
        }
        sb.append(sa.toString());
        return sb.toString();
    }


    /**
     * 打印两列
     *
     * @param leftText  左侧文字
     * @param rightText 右侧文字
     * @return
     */
    @SuppressLint("NewApi")
    public static String printTwoDataBigSize(String leftText, String rightText) {
        StringBuilder sb = new StringBuilder();
        int leftTextLength = getBytesLength(leftText);
        int rightTextLength = getBytesLength(rightText);
        sb.append(leftText);

        // 计算两侧文字中间的空格
        int marginBetweenMiddleAndRight = LINE_BIG_BYTE_SIZE - leftTextLength - rightTextLength;

        for (int i = 0; i < marginBetweenMiddleAndRight; i++) {
            sb.append(" ");
        }
        sb.append(rightText);
        return sb.toString();
    }

    public static byte[] printDivideLine() {
        StringBuilder sb = new StringBuilder();
        String divide = "-";
        int length = getBytesLength(divide);
        for (int i = 0, j = LINE_BYTE_SIZE / length; i < j; i++) {
            sb.append(divide);
        }
        return sb.toString().getBytes();
    }

    public static String printDivideLineString() {
        StringBuilder sb = new StringBuilder();
        String divide = "-";
        int length = getBytesLength(divide);
        for (int i = 0, j = LINE_BYTE_SIZE / length; i < j; i++) {
            sb.append(divide);
        }
        return sb.toString();
    }

    public static String printDivideSymbolString(String symbol) {
        StringBuilder sb = new StringBuilder();
        String divide = symbol;
        int length = getBytesLength(divide);
        for (int i = 0, j = LINE_BYTE_SIZE / length; i < j; i++) {
            sb.append(divide);
        }
        return sb.toString();
    }

    /**
     * 打印三列
     *
     * @param leftText   左侧文字
     * @param middleText 中间文字
     * @param rightText  右侧文字
     * @return
     */
    @SuppressLint("NewApi")
    public static String printThreeData(String leftText, String middleText, String rightText) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
        if (RegexUtils.isMatch("^[a-zA-Z_0-9]+$", leftText)) {
            if (leftText.length() > LEFT_TEXT_MAX_LENGTH_15) {
                leftText = leftText.substring(0, LEFT_TEXT_MAX_LENGTH_15) + "..";
            }
        } else {
            if (leftText.length() > LEFT_TEXT_MAX_LENGTH) {
                leftText = leftText.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
            }
        }
        int leftTextLength = getBytesLength(leftText);
        int middleTextLength = getBytesLength(middleText);
        int rightTextLength = getBytesLength(rightText);

        sb.append(leftText);
        // 计算左侧文字和中间文字的空格长度
        int marginBetweenLeftAndMiddle = LEFT_LENGTH - leftTextLength - middleTextLength / 2;

        for (int i = 0; i < marginBetweenLeftAndMiddle; i++) {
            sb.append(" ");
        }
        sb.append(middleText);

        // 计算右侧文字和中间文字的空格长度
        int marginBetweenMiddleAndRight = RIGHT_LENGTH - middleTextLength / 2 - rightTextLength;

        for (int i = 0; i < marginBetweenMiddleAndRight; i++) {
            sb.append(" ");
        }

        // 打印的时候发现，最右边的文字总是偏右一个字符，所以需要删除一个空格
        sb.delete(sb.length() - 1, sb.length()).append(rightText);
        return sb.toString();
    }


    /**
     * 打印四列
     *
     * @param leftText   左侧文字
     * @param secondText 第二个文字
     * @param thirdText  第三个文字
     * @param rightText  右侧文字
     * @return
     */
    @SuppressLint("NewApi")
    public static String printFourData(String leftText, String secondText, String thirdText, String rightText) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
        if (RegexUtils.isMatch("^[a-zA-Z_0-9]+$", leftText)) {
            if (leftText.length() > LEFT_TEXT_MAX_LENGTH_15) {
                leftText = leftText.substring(0, LEFT_TEXT_MAX_LENGTH_15) + "..";
            } else {
                StringBuffer stringBuffer = new StringBuffer(leftText);
                for (int i = 0; i < 17 - leftText.length(); i++) {
                    stringBuffer.append(" ");
                }
                leftText = stringBuffer.toString();
            }
        } else {
            if (leftText.length() > LEFT_TEXT_MAX_LENGTH) {
                leftText = leftText.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
            }
        }
        int leftTextLength = getBytesLength(leftText);
        int secondTextLength = getBytesLength(secondText);
        int thirdTextLength = getBytesLength(thirdText);
        int rightTextLength = getBytesLength(rightText);

        sb.append(leftText);
        // 计算文字间的空格长度
        int marginBetweenLetters = LINE_BYTE_SIZE - leftTextLength - secondTextLength
                - thirdTextLength - rightTextLength;

        sb.append("       ");//8
        sb.append(secondText);
        for (int i = 0; i <= marginBetweenLetters - 11; i++) {
            sb.append(" ");
        }
        sb.append(thirdText);
        sb.append("   ");//3
        sb.append(rightText);
        return sb.toString();
    }

    /**
     * 打印四列
     *
     * @param firstText  第一个文字
     * @param secondText 第二个文字
     * @param thirdText  第三个文字
     * @param fourthText 第四个文字
     * @param fifthText  第五个文字
     * @return
     */
    @SuppressLint("NewApi")
    public static String printFiveData(String firstText, String secondText, String thirdText, String fourthText, String fifthText) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
        if (firstText.length() > LEFT_TEXT_MAX_LENGTH) {
            firstText = firstText.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
        }
        int firstTextLength = getBytesLength(firstText);
        int secondTextLength = getBytesLength(secondText);
        int thirdTextLength = getBytesLength(thirdText);
        int fourthTextLength = getBytesLength(fourthText);
        int fifthTextLength = getBytesLength(fifthText);

        sb.append(firstText);
        // 计算剩余的空格长度
        int marginBetweenLetters = LINE_BYTE_SIZE - firstTextLength - secondTextLength
                - thirdTextLength - fourthTextLength - fifthTextLength;

        sb.append("       ");//8
        sb.append(secondText);
        for (int i = 0; i <= marginBetweenLetters - 14; i++) {
            sb.append(" ");
        }
        sb.append(thirdText);
        sb.append("   ");//3
        sb.append(fourthText);
        sb.append("   ");//3
        sb.append(fifthText);
        return sb.toString();
    }

    /**
     * 获取数据长度
     *
     * @param msg
     * @return
     */
    @SuppressLint("NewApi")
    private static int getBytesLength(String msg) {
        return msg.getBytes(Charset.forName("GB2312")).length;
    }

    /**
     * 格式化菜品名称，最多显示MEAL_NAME_MAX_LENGTH个数
     *
     * @param name
     * @return
     */
    public static String formatMealName(String name) {
        if (TextUtils.isEmpty(name)) {
            return name;
        }
        if (name.length() > MEAL_NAME_MAX_LENGTH) {
            return name.substring(0, 8) + "..";
        }
        return name;
    }

    /**
     * 打印居中文字
     *
     * @param str 待打印文字
     * @return
     */
    public static String printCenterText(String str) {
        StringBuilder sb = new StringBuilder();
        String divide = "-";
        int length = getBytesLength(divide);
        int strLen = getBytesLength(str);
        for (int i = 0; i < (LINE_BYTE_SIZE / length) / 2 - strLen / 4 - 1; i++) {
            sb.append(" ");
        }
        sb.append(str);
        for (int i = 0; i < (LINE_BYTE_SIZE / length) / 2 - strLen / 4 - 1; i++) {
            sb.append(" ");
        }
        return sb.toString();
    }

    /**
     * 打印带分割线的居中字符
     * 例:-------谢谢惠顾-------
     *
     * @param str 待打印
     * @return
     * @author pupu
     */
    public static String printCenterTextWithDivideLine(String str) {
        StringBuilder sb = new StringBuilder();
        String divide = "-";
        int length = getBytesLength(divide);
        int strLen = getBytesLength(str);
        for (int i = 0; i < (LINE_BYTE_SIZE / length) / 2 - strLen / 4 - 1; i++) {
            sb.append("-");
        }
        sb.append(str);
        for (int i = 0; i < (LINE_BYTE_SIZE / length) / 2 - strLen / 4 - 1; i++) {
            sb.append("-");
        }
        return sb.toString();
    }

    /**
     * 打印4列,4列文字都居中打印
     *
     * @param str1
     * @param str2
     * @param str3
     * @param str4
     * @return
     * @author pupu
     */
    public static String printFourDataWithCenterAlign(String str1, String str2, String str3, String str4) {
        StringBuilder sb = new StringBuilder();
        int MAX_WORD = LINE_BYTE_SIZE / 2 + 1;
        //最左侧文字最多显示6个文字
        if (str1.length() > MAX_WORD / 4) {
            str1 = str1.substring(0, MAX_WORD / 4) + "..";
        }
        if (str2.length() > MAX_WORD / 4) {
            str2 = str2.substring(0, MAX_WORD / 4) + "..";
        }
        if (str3.length() > MAX_WORD / 4) {
            str3 = str3.substring(0, MAX_WORD / 4) + "..";
        }
        if (str4.length() > MAX_WORD / 4) {
            str4 = str4.substring(0, MAX_WORD / 4) + "..";
        }
        int strLen1 = getBytesLength(str1) / 2;
        int strLen2 = getBytesLength(str2) / 2;
        int strLen3 = getBytesLength(str3) / 2;
        int strLen4 = getBytesLength(str4) / 2;

        //计算空格
        //str1
        for (int i = 0; i < (MAX_WORD / 4 - strLen1) / 2; i++) {
            sb.append("  ");
        }
        sb.append(str1);
        for (int i = 0; i < (MAX_WORD / 4 - strLen1) / 2; i++) {
            sb.append("  ");
        }
        //str2
        for (int i = 0; i < (MAX_WORD / 4 - strLen2) / 2; i++) {
            sb.append("  ");
        }
        sb.append(str2);
        for (int i = 0; i < (MAX_WORD / 4 - strLen2) / 2; i++) {
            sb.append("  ");
        }
        //str3
        for (int i = 0; i < (MAX_WORD / 4 - strLen3) / 2; i++) {
            sb.append("  ");
        }
        sb.append(str3);
        for (int i = 0; i < (MAX_WORD / 4 - strLen3) / 2; i++) {
            sb.append("  ");
        }
        //str4
        for (int i = 0; i < (MAX_WORD / 4 - strLen4) / 2; i++) {
            sb.append("  ");
        }
        sb.append(str4);
        for (int i = 0; i < (MAX_WORD / 4 - strLen4) / 2; i++) {
            sb.append("  ");
        }

        return sb.toString();
    }
}
