class printClass {
    // 打印高度
    _height;
    // 打印宽度
    _width;
    // 打印X轴起点,默认：0
    _initX;
    // 打印Y轴起点,默认：0
    _initY;
    // 打印图片高度,默认：200
    _imgHeight;
    // 打印图片宽度,默认：200
    _imgWidth;
    // 文字大小,默认：2
    _fontSize;
    // 表格的标题
    _columnNames;
    // 表格标题的高度
    _columnNameHeight;
    // 表格的宽度
    _tableWidth;
    // 表格内容的宽度
    _cellWidth;
    // 表格内容的高度
    _cellHeight;
    // 表格线与内容之间的间隙
    _contentSpacing;
    // 页数
    _pageNum;
    // 画布对象
    _ctx;
    // 打印机默认预留高度
    _defaultHeight;

    // 构造函数
    constructor(width , height , initX, initY) {
        this._height = height || 0;
        this._width = width || 0;
        this._initX = initX || 0;
        this._initY = initY || 0;
        this._imgHeight = 200;
        this._imgWidth = 200;
        this._fontSize = 2;
        this._pageNum = 0;
        this._param = {};
        this._defaultHeight = 0;
    }

    // 初始化打印参数对象
    initPrintParam(){
        this._param = {
            print: [{
                width: this._width, //  打印宽度
                height: this._height,//  打印高度
                content: []
            },
            ]
        };
    }

    // 获取打印参数对象
    getPrintParam(){ return this._param; }

    // 获取打印页高度
    getHeight() { return this._height; }

    // 获取打印页宽度
    getWidth() { return this._width; }

    // 设置打印机默认预留高度
    setDefalutHeight(height){ this._defaultHeight = height; }

    // 设置打印页面大小
    setPageSize(height, width) { this._height = height; this._width = width; }

    // 设置打印页面区域
    setPrintArea(initX, initY, height, width) {
        this._initX = initX;
        this._initY = initY;
        this._height = height;
        this._width = width;
    }

    // 设置画布对象
    setCanvasCtx(ctx){
        this._ctx = ctx;
    }

    // 设置图片大小
    setImageSize(height, width) { this._imgHeight = height; this._imgWidth = width; }

    // 设置字体大小
    setFontSize(size) { this._fontSize = size; }

    // 打印一行文本
    drawText(text, startX, startY, rotate, bold, reverse, undeline) {
        let printParam = {
            "method":"drawText",
            "value":{}
        };
        printParam.value.text_x = startX;
        printParam.value.text_y = startY;
        printParam.value.text = text;
        printParam.value.fontSize = this._fontSize;
        printParam.value.rotate = rotate || 0;
        printParam.value.bold = bold || false;
        printParam.value.reverse = reverse || false;
        printParam.value.undeline = undeline || false;
        this._param.print[this._pageNum].content.push(printParam);
        return printParam;
    }

    // 打印多行文本
    drawTextBox(text, startX, startY, width, height, rotate, bold, reverse, undeline) {
        let printParam = {
            "method":"drawTextBox",
            "value":{}
        };
        printParam.value.height = height * this.autoLineWrap(text,width);
        printParam.value.text_x = startX;
        printParam.value.text_y = startY;
        printParam.value.text = text;
        printParam.value.width = width;
        printParam.value.fontSize = this._fontSize;
        printParam.value.rotate = rotate || 0;
        printParam.value.bold = bold || false;
        printParam.value.reverse = reverse || false;
        printParam.value.undeline = undeline || false;
        this._param.print[this._pageNum].content.push(printParam);
        return printParam;
    }

    // 打印线条
    drawLine(lineWidth, startX, startY, endX, endY, fullLine) {
        let printParam = {
            "method":"drawLine",
            "value":{}
        };
        printParam.value.lineWidth = lineWidth || 1;
        printParam.value.start_x = startX;
        printParam.value.start_y = startY;
        printParam.value.end_x = endX;
        printParam.value.end_y = endY;
        printParam.value.fullline = fullLine || true;
        this._param.print[this._pageNum].content.push(printParam);
        return printParam;
    }

    // 打印图片
    drawImage(startX, startY, sizeX, sizeY, url) {
        let printParam = {
            "method":"drawGraphic",
            "value":{}
        };
        printParam.value.start_x = startX;
        printParam.value.start_y = startY;
        printParam.value.bmp_size_x = sizeX;
        printParam.value.bmp_size_y = sizeY;
        printParam.value.bmp = url;
        this._param.print[this._pageNum].content.push(printParam);
        return printParam;
    }

    // 打印边框
    drawBox(lineWidth, startX, startY, endX, endY) {
        let printParam = {
            "method":"drawBox",
            "value":{}
        };
        printParam.value.lineWidth = lineWidth || 1;
        printParam.value.top_left_x = startX;
        printParam.value.top_left_y = startY;
        printParam.value.bottom_right_x = endX;
        printParam.value.bottom_right_y = endY;
        this._param.print[this._pageNum].content.push(printParam);
    }

    // 设置表格内容宽度
    setRowWidth(data,tableWidth,startX,cellNumber) {
        let array = [];
        tableWidth = tableWidth || this._tableWidth;
        cellNumber = cellNumber || this._columnNames.length;
        if (Object.prototype.toString.call(data) === '[object Array]') {
            let sum = 0;
            let count = 0

            data.forEach((item) => {
                if (item != '') {
                    sum += item;
                    count++;
                }
            })
            let cellWidth = (tableWidth - sum - startX) / (cellNumber - count);
            data.forEach((item) => {
                if (item == '') {array.push(cellWidth);return true;}
                array.push(item);
            })
        } else if (!data) {
            data = (( tableWidth ) / cellNumber);
            array = new Array(cellNumber).fill(data);
        } else {
            array = new Array(cellNumber).fill(data);
        }
        this._cellWidth = array;
    }

    // 设置表格内容高度
    setRowHeight(height){
        this._cellHeight = height;
    }

    // 初始化表格属性
    initTableSetting(tableWidth,columnNames,columnNameHeight,contentSpacing){
        this._tableWidth = tableWidth;
        this._columnNames = columnNames;
        this._columnNameHeight = columnNameHeight;
        this._contentSpacing = contentSpacing || 0;
    }

    /**
     * 使用drawTable前，需要初始化表格属性，即执行initTableSetting（）方法
     * 执行setRowWidth（）方法，可设置统一的宽度，可通过传入数组，设置不同的宽度
     * 执行setRowHeight（）方法，设置表格之间的高度
     * **/
    drawTable(columnData,startX,startY) {
        //  表头
        this.addRow(this._columnNames,startX,startY,this._cellWidth,this._columnNameHeight);
        startY += this._columnNameHeight;
        //  内容
        for (let i = 0; i < columnData.length; i++) {
            // 判断下一行内容是否超出页面大小
            if (this.checkIsOverFlow(startY,this._cellHeight)) {
                this._param.print.push({
                    width: this._width, //  打印宽度
                    height: this._height,//  打印高度
                    content: []
                });
                this._pageNum++;
                columnData.splice(0,i);
                console.log("超出页面后的columnData"+columnData);
                this.drawTable(columnData,startX,this._initY);
                return;
            }
            // 添加一行内容
            this.addRow(columnData[i],startX,startY,this._cellWidth,this._cellHeight);
            startY += this._cellHeight;
        }
    }

    // 绘制表格边框
    drawTableBorder(cellNumber,startX,startY,endX,endY,cellWidth,cellHeight,lineWidth,fullLineX,fullLineY){
        //  初始化属性
        fullLineX = fullLineX || true;
        fullLineY = fullLineY || true;
        lineWidth = lineWidth || 1;
        //  横线
        this.drawLine(lineWidth,startX,startY,endX,startY,fullLineX);
        this.drawLine(lineWidth,startX,endY,endX,endY,fullLineX);
        //  竖线
        for (let i = 0;i < cellNumber;i++){
            this.drawLine(lineWidth,startX,startY,startX,endY,fullLineY);
            startX += cellWidth[i];
        }
        //  最右一条竖线
        this.drawLine(lineWidth,startX,startY,startX,endY,fullLineY);
    }

    // 表格添加一行内容
    addRow(rowData, startX, startY, cellWidth, cellHeight) {
        let text_x = startX + this._contentSpacing;
        let text_y = startY + this._contentSpacing;
        let obj = {};
        cellHeight = cellHeight || this._cellHeight;
        cellWidth = this._cellWidth;
        //  填充一行内容
        rowData.forEach((item, i) => {
            obj = this.drawTextBox(item,text_x, text_y, cellWidth[i] - 2 * this._contentSpacing, cellHeight);
            text_x += cellWidth[i];
            if(obj.height > cellHeight) cellHeight = obj.height;
        })
        //  绘制表格边框
        let endX = startX + this._tableWidth;
        let endY = startY + cellHeight;
        this.drawTableBorder(rowData.length,startX,startY,endX,endY,cellWidth,cellHeight,1,true,false);
    }

    // 检查内容是否超出打印页面
    checkIsOverFlow(startY,lineHeight){
        if(startY + lineHeight >= this._height - this._defaultHeight) return true;
        return false;
    }

    // 判断内容是否换行
    autoLineWrap(text,txtWidth){
        let lineWidth = 0;
        let flag = 1;
        if (this._ctx.measureText(text).width > txtWidth) {
            for (let i = 0; i < text.length; i++) {
                lineWidth += this._ctx.measureText(text[i]).width;
                if (lineWidth > txtWidth) {
                    lineWidth = 0;
                    flag++;
                }
                if (i == text.length - 1) {
                    return flag / 2;
                }
            }
        }
        return flag;
    }
}
export default printClass