package org.example.Utils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * 打印指令工具类（对应原C# PrintHelper）
 * 功能：生成各类打印指令的16进制字符串
 */
public final class PrintHelper {

    // 对应C# Encoding.Default（Windows默认编码）
    private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;

    /**
     * 指令类型枚举（对应原C# CommandType）
     */
    public enum CommandType {
        A(1),
        B(2); // B未指定值，默认按顺序为2（和C#一致）

        private final int value;

        CommandType(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }

    // 私有化构造方法，禁止实例化（静态工具类）
    private PrintHelper() {
    }

    // ------------------- 核心重载方法：GetPrintCommandStr -------------------

    /**
     * 字符串转16进制指令（对应原GetPrintCommandStr(StringBuilder, string)）
     */
    public static StringBuilder getPrintCommandStr(StringBuilder stringBuilder, String value) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        // 转换为字节数组（对应C# Encoding.Default.GetBytes）
        byte[] bytes = value.getBytes(DEFAULT_CHARSET);
        for (byte b : bytes) {
            // 16进制格式化（X2 → %02X），末尾加空格
            stringBuilder.append(String.format("%02X ", b));
        }
        return stringBuilder;
    }

    /**
     * 整数转16进制指令（默认双字节参数，对应原isDoubleParameter = true）
     */
    public static StringBuilder getPrintCommandStr(StringBuilder stringBuilder, int value) {
        return getPrintCommandStr(stringBuilder, value, true);
    }

    /**
     * 整数转16进制指令（支持单/双字节参数，对应原GetPrintCommandStr(StringBuilder, int, bool)）
     */
    public static StringBuilder getPrintCommandStr(StringBuilder stringBuilder, int value, boolean isDoubleParameter) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }

        if (isDoubleParameter) {
            // 双字节参数：转4位16进制，拆分后两位和前两位
            String text = String.format("%04X", value); // PadLeft(4, '0') → %04X
            // 截取后两位（substring(text.Length - 2)）
            String arg = text.substring(text.length() - 2);
            // 截取前两位（substring(0, text.Length - 2)）
            String arg2 = text.substring(0, text.length() - 2);
            stringBuilder.append(arg).append(" ");
            stringBuilder.append(arg2).append(" ");
        } else {
            // 单字节参数：转2位16进制
            String arg3 = String.format("%02X", value); // PadLeft(2, '0') → %02X
            stringBuilder.append(arg3).append(" ");
        }
        return stringBuilder;
    }

    // ------------------- 页面启停指令 -------------------

    /**
     * 打印页开始指令（无参数，对应原GetPrintPageStartCommandStr(StringBuilder)）
     */
    public static StringBuilder getPrintPageStartCommandStr(StringBuilder stringBuilder) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 5B 00");
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    /**
     * 打印页开始指令（带参数，对应原GetPrintPageStartCommandStr(StringBuilder, int, int, int, int, int)）
     */
    public static StringBuilder getPrintPageStartCommandStr(StringBuilder stringBuilder, int x, int y, int width, int height, int rotate) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 5B 01 ");
        getPrintPageStartParameterStr(stringBuilder, x, y, width, height, rotate);
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    /**
     * 打印页开始参数拼接（对应原GetPrintPageStartParameterStr）
     */
    public static StringBuilder getPrintPageStartParameterStr(StringBuilder stringBuilder, int x, int y, int width, int height, int rotate) {
        getPrintCommandStr(stringBuilder, x);
        getPrintCommandStr(stringBuilder, y);
        getPrintCommandStr(stringBuilder, width);
        getPrintCommandStr(stringBuilder, height);
        getPrintCommandStr(stringBuilder, rotate, false); // 单字节参数
        return stringBuilder;
    }

    /**
     * 打印页结束指令（对应原GetPrintPageEndCommandStr）
     */
    public static StringBuilder getPrintPageEndCommandStr(StringBuilder stringBuilder) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 5D 00");
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    // ------------------- 打印执行指令 -------------------

    /**
     * 打印执行指令（默认份数，对应原GetPrintPagePrintCommandStr(StringBuilder)）
     */
    public static StringBuilder getPrintPagePrintCommandStr(StringBuilder stringBuilder) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 4F 00");
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    /**
     * 打印执行指令（指定份数，对应原GetPrintPagePrintCommandStr(StringBuilder, int)）
     */
    public static StringBuilder getPrintPagePrintCommandStr(StringBuilder stringBuilder, int printNum) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 4F 01");
        // 空值处理：printNum.ToString() ?? "" → 转换为字符串，空则为空串
        String printNumStr = String.valueOf(printNum);
        getPrintCommandStr(stringBuilder, printNumStr == null ? "" : printNumStr);
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    // ------------------- 跳页指令 -------------------

    /**
     * 跳页指令（无参数，对应原GetPrintPageSkipCommandStr(StringBuilder)）
     */
    public static StringBuilder getPrintPageSkipCommandStr(StringBuilder stringBuilder) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 0C 00");
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    /**
     * 跳页指令（带参数，对应原GetPrintPageSkipCommandStr(StringBuilder, int, int)）
     */
    public static StringBuilder getPrintPageSkipCommandStr(StringBuilder stringBuilder, int stopPosition, int offset) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 0C 01");
        getPrintCommandStr(stringBuilder, stopPosition, false); // 单字节参数
        getPrintCommandStr(stringBuilder, offset); // 双字节参数（默认）
        stringBuilder.append("\r\n");
        return stringBuilder;
    }

    // ------------------- 文本打印指令 -------------------

    /**
     * 文本打印指令（基础版，对应原GetPrintPageStringCommandStr(StringBuilder, string, int, int)）
     */
    public static StringBuilder getPrintPageStringCommandStr(StringBuilder stringBuilder, String value, int x, int y) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 54 00");
        stringBuilder.append("\r\n");
        getPrintCommandStr(stringBuilder, x);
        getPrintCommandStr(stringBuilder, y);
        getPrintCommandStr(stringBuilder, value);
        getPrintCommandStr(stringBuilder, "\0"); // 字符串终止符
        return stringBuilder;
    }

    /**
     * 文本打印指令（完整版，带字体参数，对应原GetPrintPageStringCommandStr(StringBuilder, string, int, int, int, int)）
     */
    public static StringBuilder getPrintPageStringCommandStr(StringBuilder stringBuilder, String value, int x, int y, int fontHeight, int fontType) {
        if (stringBuilder == null) {
            stringBuilder = new StringBuilder();
        }
        stringBuilder.append("1A 54 01");
        stringBuilder.append("\r\n");
        getPrintCommandStr(stringBuilder, x);
        getPrintCommandStr(stringBuilder, y);
        getPrintCommandStr(stringBuilder, fontHeight);
        getPrintCommandStr(stringBuilder, fontType);
        getPrintCommandStr(stringBuilder, value);
        getPrintCommandStr(stringBuilder, "\0"); // 字符串终止符
        stringBuilder.append("\r\n");
        return stringBuilder;
    }
}