package com.printer.demo.utils;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import com.printer.sdk.CanvasDrawPrint;
import com.printer.sdk.PrinterInstance;

public class PrinterUtils {

    private static final int DEFAULT_TEXT_SIZE = 20;


    private static final TextPaint DEFAULT_PAINT;
    private static final TextPaint BOLD_PAINT;
    private static final TextPaint ITALIC_PAINT;
    private static final TextPaint DOUBLE_WIDTH_PAINT;
    private static final float DOUBLE_HEIGHT = 2.0f;//设置倍高
    private static final float DOUBLE_WIDTH = 1.0f;//设置倍宽

    static {
        // 初始化默认字体样式
        DEFAULT_PAINT = new TextPaint();
        DEFAULT_PAINT.setColor(Color.BLACK); // 设置文本颜色为黑色
        DEFAULT_PAINT.setTextSize(DEFAULT_TEXT_SIZE); // 设置文本大小
        DEFAULT_PAINT.setTypeface(Typeface.DEFAULT); // 设置默认字体

        // 初始化加粗字体样式
        BOLD_PAINT = new TextPaint(DEFAULT_PAINT);
        BOLD_PAINT.setTypeface(Typeface.DEFAULT_BOLD); // 设置字体为加粗

        // 初始化斜体字体样式
        ITALIC_PAINT = new TextPaint(DEFAULT_PAINT);
        ITALIC_PAINT.setTypeface(Typeface.create(DEFAULT_PAINT.getTypeface(), Typeface.ITALIC)); // 设置字体为斜体
        // 初始化倍宽字体样式
        DOUBLE_WIDTH_PAINT = new TextPaint(DEFAULT_PAINT);
        DOUBLE_WIDTH_PAINT.setTextScaleX(DOUBLE_WIDTH); // 设置字体宽度为默认的两倍
    }

    public static Bitmap createDynamicTableContentBitmap(CanvasDrawPrint mCanvasDrawPrint, String[][] tableData, int rowPadding,
                                                         int columnPadding, int[] columnWidths, Layout.Alignment[] alignments, TextPaint defaultPaint) {
        Bitmap bitmap = mCanvasDrawPrint.getCanvasImage();
        int bitmapWidth = bitmap.getWidth();
        int maxHeight = bitmap.getHeight();
        int numRows = tableData.length;
        int numCols = tableData[0].length;
        int countWidth = 0;

        // 计算总列宽
        for (int width : columnWidths) {
            countWidth += width;
        }

        // 检查并调整列宽数组的长度
        if (columnWidths.length < numCols) {
            int[] newColumnWidths = new int[numCols];
            System.arraycopy(columnWidths, 0, newColumnWidths, 0, columnWidths.length);
            int avgColWidth = bitmapWidth / numCols;
            for (int i = columnWidths.length; i < numCols; i++) {
                newColumnWidths[i] = avgColWidth;
            }
            columnWidths = newColumnWidths;
        } else if (columnWidths.length > numCols) {
            int[] newColumnWidths = new int[numCols];
            System.arraycopy(columnWidths, 0, newColumnWidths, 0, numCols);
            columnWidths = newColumnWidths;
        }

        // 检查并调整对齐方式数组的长度
        if (alignments.length < numCols) {
            Layout.Alignment[] newAlignments = new Layout.Alignment[numCols];
            System.arraycopy(alignments, 0, newAlignments, 0, alignments.length);
            for (int i = alignments.length; i < numCols; i++) {
                newAlignments[i] = Layout.Alignment.ALIGN_NORMAL;  // 默认左对齐
            }
            alignments = newAlignments;
        } else if (alignments.length > numCols) {
            Layout.Alignment[] newAlignments = new Layout.Alignment[numCols];
            System.arraycopy(alignments, 0, newAlignments, 0, numCols);
            alignments = newAlignments;
        }

        // 动态计算所需高度
        int totalHeight = rowPadding;  // 开始绘制的 Y 坐标
        for (int row = 0; row < numRows; row++) {
            int maxLineHeight = getMaxLineHeight(numCols, columnWidths, tableData[row], defaultPaint, columnPadding);
            totalHeight += maxLineHeight * DOUBLE_HEIGHT + rowPadding;
        }

        // 如果计算出的总高度超过最大高度，则使用计算出的高度
        int bitmapHeight = Math.max(totalHeight, maxHeight);
        if (bitmapHeight > maxHeight) {
            bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_4444);
        }

        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);  // 设置背景颜色为白色

        // 绘制内容
        int y = rowPadding;  // 开始绘制的 Y 坐标
        for (int row = 0; row < numRows; row++) {
            int x = 0;  // 每行开始绘制的 X 坐标，不需要左边距
            for (int col = 0; col < numCols; col++) {
                String text = (col < tableData[row].length) ? tableData[row][col] : "";  // 检查是否超出列数

                int cellWidth = columnWidths[col] - columnPadding * 2;

                // 使用 StaticLayout 处理文本换行
                StaticLayout staticLayout = new StaticLayout(
                        text,
                        defaultPaint,
                        cellWidth,
                        alignments[col],
                        1.0f,
                        0.0f,
                        false
                );

                // 绘制文本
                canvas.save();
                canvas.scale(1, DOUBLE_HEIGHT, x + columnPadding, y); // 仅设置倍高
                canvas.translate(x + columnPadding, y);
                staticLayout.draw(canvas);
                canvas.restore();

                x += columnWidths[col];  // 移动到下一个列的起始位置
            }

            // 计算当前行的最大行高
            int maxLineHeight = getMaxLineHeight(numCols, columnWidths, tableData[row], defaultPaint, columnPadding);
            y += maxLineHeight * DOUBLE_HEIGHT + rowPadding;  // 移动到下一行的起始位置
        }
        return bitmap;
    }

    private static int getMaxLineHeight(int numCols, int[] colWidths, String[] rowData, TextPaint paint, int columnPadding) {
        int maxLineHeight = 0;
        for (int col = 0; col < numCols; col++) {
            String text = (col < rowData.length) ? rowData[col] : "";  // 检查是否超出列数
            int cellWidth = colWidths[col] - columnPadding * 2;
            StaticLayout staticLayout = new StaticLayout(
                    text,
                    paint,
                    cellWidth,
                    Layout.Alignment.ALIGN_NORMAL,
                    1.0f,
                    0.0f,
                    false
            );
            maxLineHeight = Math.max(maxLineHeight, staticLayout.getHeight());
        }
        return maxLineHeight;
    }

}
