package com.miya.print.utils;


import java.nio.charset.Charset;

/**
 * 累积工具类
 */
public class AccumulateUtils {

    /**
     * 80mm打印纸一行允许的最大打印字符数（默认字符大小）
     */
    final static int MAX_WORDS_80MM = 46;

    /**
     * 58mm打印纸一行允许的最大打印字符数（默认字符大小）
     */
    final static int MAX_WORDS_58MM = 32;

    /**
     * 80mm打印纸类型
     */
    public final static int PAGE_MODE_80MM = 0;

    /**
     * 58mm打印纸类型
     */
    public final static int PAGE_MODE_58MM = 1;

    /**
     * 累积字符串
     */
    private static String accumulateStr = "";

    private AccumulateUtils() {
        throw new UnsupportedOperationException();
    }

    /**
     * 累积字符串，自带换行
     *
     * @param pageMode 打印纸类型
     * @param size     字符大小
     * @param align    对齐方式
     * @param str      待累积的新字符串
     * @return 当前累积的字符串
     */
    public static String accumulateStringWithLF(int pageMode, int align, int size, String str) {
        if (str == null) {
            return accumulateStr;
        }
        switch (pageMode) {
            case PAGE_MODE_80MM:
                return accumulateString_80mm(align, size, str, true);
            case PAGE_MODE_58MM:
                return accumulateString_58mm(align, size, str, true);
        }
        return accumulateStr;
    }

    /**
     * 累积字符串
     *
     * @param pageMode 打印纸类型
     * @param size     字符大小
     * @param align    对齐方式
     * @param str      待累积的新字符串
     * @param isFeed   是否走纸
     * @return 当前累积的字符串
     */
    public static String accumulateString(int pageMode, int align, int size, String str, boolean isFeed) {
        if (str == null) {
            return accumulateStr;
        }
        switch (pageMode) {
            case PAGE_MODE_80MM:
                return accumulateString_80mm(align, size, str, isFeed);
            case PAGE_MODE_58MM:
                return accumulateString_58mm(align, size, str, isFeed);
        }
        return accumulateStr;
    }

    /**
     * 立即清除已经累积的字符串
     */
    public static void clearAccmulateString() {
        accumulateStr = "";
    }

    /**
     * 获取字符串的byte长度
     *
     * @param str
     * @return
     */
    private static int getBytesLength(String str) {
        if (str != null) {
            return str.getBytes(Charset.forName("GB2312")).length;
        }
        return 0;
    }

    /**
     * 80mm打印纸累积字符串，自带换行
     *
     * @param align  对齐方式
     * @param size   字符大小
     * @param str    字符串内容
     * @param isFeed 是否走纸
     * @return 当前累积的字符串
     */
    public static String accumulateString_80mm(int align, int size, String str, boolean isFeed) {
        if (str == null) {
            return accumulateStr;
        }
        //先用换行符将其分割
        String[] strList = str.split("\n");
        String targetStr = "";
        for (String tmp : strList) {
            targetStr = targetStr + addSpace_80mm(align, tmp);
            if (str.contains("\n")) {
                //如果包含换行符，此处需补偿换行符
                targetStr = targetStr + "\n";
            }
        }
        accumulateStr = accumulateStr + targetStr;
        if (isFeed) {
            accumulateStr += "\n";
        }
        return accumulateStr;
    }

    /**
     * 58mm打印纸累积字符串，自带换行
     *
     * @param align  对齐方式
     * @param size   字符大小
     * @param str    字符串内容
     * @param isFeed 是否走纸
     * @return 当前累积的字符串
     */
    public static String accumulateString_58mm(int align, int size, String str, boolean isFeed) {
        if (str == null) {
            return accumulateStr;
        }
        //待拼接的字符串
        String[] strList = str.split("\n");
        String targetStr = "";
        for (String tmp : strList) {
            targetStr = targetStr + addSpace_58mm(align, tmp);
            if (str.contains("\n")) {
                //如果包含换行符，此处需补偿换行符
                targetStr = targetStr + "\n";
            }
        }
        accumulateStr = accumulateStr + targetStr;
        if (isFeed) {
            accumulateStr += "\n";
        }
        return accumulateStr;
    }

    /**
     * 获取当前已经累积完的字符串
     *
     * @return 当前已经累积完的字符串
     */
    public static String getAccumulatedString() {
        return accumulateStr;
    }

    /**
     * 添加空格实现对齐逻辑,传参中的字符不允许有换行符(80mm)
     *
     * @param align 对齐方式
     * @param str   字符串,不允许含换行符
     * @return 处理后的字符串
     */
    private static String addSpace_80mm(int align, String str) {
        String spaceStr = "";
        switch (align) {
            case 0:
                //居左
                return str;
            case 1:
                //计算空格数
                for (int i = 0; i < (MAX_WORDS_80MM - getBytesLength(str)) / 2; i++) {
                    spaceStr = spaceStr + " ";
                }
                spaceStr = spaceStr + str;
                return spaceStr;
            case 2:
                //计算空格数
                for (int i = 0; i < MAX_WORDS_80MM - getBytesLength(str); i++) {
                    spaceStr = spaceStr + " ";
                }
                spaceStr = spaceStr + str;
                return spaceStr;
            default:
                return str;
        }
    }

    /**
     * 添加空格实现对齐逻辑,传参中的字符不允许有换行符(58mm)
     *
     * @param align 对齐方式
     * @param str   字符串,不允许含换行符
     * @return 处理后的字符串
     */
    private static String addSpace_58mm(int align, String str) {
        String spaceStr = "";
        switch (align) {
            case 0:
                //居左
                return str;
            case 1:
                //计算空格数
                for (int i = 0; i < (MAX_WORDS_58MM - getBytesLength(str)) / 2; i++) {
                    spaceStr = spaceStr + " ";
                }
                spaceStr = spaceStr + str;
                return spaceStr;
            case 2:
                //计算空格数
                for (int i = 0; i < MAX_WORDS_58MM - getBytesLength(str); i++) {
                    spaceStr = spaceStr + " ";
                }
                spaceStr = spaceStr + str;
                return spaceStr;
            default:
                return str;
        }
    }
}
