package com.yeechart.dotMatrix.signage;

import com.aliyuncs.utils.StringUtils;
import com.yeechart.dotMatrix.canvas.DotMatrixCanvasUtil;
import com.yeechart.dotMatrix.canvas.DotMatrixDrawShapeUtil;
import com.yeechart.dotMatrix.canvas.DotMatrixDrawtextUtil;
import com.yeechart.dotMatrix.canvas.DotMatrixImageUtil;
import com.yeechart.dotMatrix.font.XZPFontLoaderUtil;
import com.yeechart.dotMatrix.signage.bean.Ter020201DriverBean;
import com.yeechart.dotMatrix.signage.bean.Ter020201InfoVo;
import com.yeechart.dotMatrix.signage.bean.TiClockBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Component
public class SignageDrawLeftUtil {
    
    @Autowired
    private DotMatrixDrawtextUtil dotMatrixDrawtextUtil;
    @Autowired
    private XZPFontLoaderUtil xZPFontLoaderUtil;


    /*********** 左屏 默认的 点阵数据的形成  ********************/

    /**
     * 绘制左屏 关机界面的点阵图
     *
     * @return 二进制数据
     */

    public  int[][] canvasLiftOFFBitmapArrays() {
        String resourcePath = "image/device/020201/init_left_default.png";

        return DotMatrixImageUtil.twoToCanvasBitmapArrays(DotMatrixImageUtil.canvasImageBitmapArrays(resourcePath, SignageDrawUtil.height, SignageDrawUtil.width, 140), SignageDrawUtil.height, SignageDrawUtil.width);

    }


    /******************* 左屏业务图  ********************/


    /**
     * 将左屏NFC图标绘制到画布中
     *
     * @param canvasBitmapArrays
     */
    public  void canvasBitmapLeftNFCIMG(int[][] canvasBitmapArrays, int startRow, int startColumn) {
        String resourcePath = "image/device/020201/nfc.png";
        int mfcIconWidth = 46;//width/10*4/5;   44
        int mfcIconHeight = 37;//width/25*2/2*4/5;
        byte[] bitmapByte = DotMatrixImageUtil.canvasImageBitmapArrays(resourcePath, mfcIconWidth, mfcIconHeight, 150);//140
        int[][] canvas = DotMatrixImageUtil.twoToCanvasBitmapArrays(bitmapByte, mfcIconWidth, mfcIconHeight);
        DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, canvas, SignageDrawUtil.width / 2 - mfcIconHeight / 2 + startRow, SignageDrawUtil.height - SignageDrawUtil.width / 40 - mfcIconWidth + startColumn);
    }

    /**
     * 清除 画布中 左屏NFC图标
     *
     * @param canvasBitmapArrays
     */
    public  void cleanCanvasBitmapLeftNFCIMG(int[][] canvasBitmapArrays, int startRow, int startColumn) {

        int mfcIconWidth = 46;
        int mfcIconHeight = 37;
        //清除旧数据痕迹
        int[][] cleanBinaryArray = new int[mfcIconHeight][mfcIconWidth];

        DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray,
                SignageDrawUtil.width / 2 - mfcIconHeight / 2 + startRow,
                SignageDrawUtil.height - SignageDrawUtil.width / 40 - mfcIconWidth + startColumn
        );
    }

    /**
     * 绘制 左屏 运行状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays check为false时 此处可以直接传null
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftRunState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showTextFontSize;
        int showNoChineseFontSize;
        int startRowA;
        int startColumnA;
        if (isClean) {
            //清除旧数据痕迹

            int[][] cleanBinaryArray = new int[rectangleBitmapArrays.length][SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)];//rectangleBitmapArrays[0].length
            startRowA = SignageDrawUtil.width / 40;
            startColumnA = SignageDrawUtil.height / 2 - cleanBinaryArray[0].length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray, startRowA, startColumnA);

        }

        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getRun() == null || ter020201DriverBean.getRun() == 0) {
            String text = ter020201DriverBean.getRAlias();
            if (StringUtils.isEmpty(text)) {
                text = "运行";
            }
            showTextFontSize = SignageDrawUtil.textFontSize;
            startRowA = SignageDrawUtil.width / 40;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }

            int[][] fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize);
//            if(check){
            startRowA = SignageDrawUtil.width / 40 + rectangleBitmapArrays.length / 2 - fontBinaryArray.length / 2;
//            }
            startColumnA = SignageDrawUtil.height / 2 - fontBinaryArray[0].length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);

        }
    }

    /**
     * 绘制 左屏 待料状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftWaitState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showNoChineseFontSize;
        int showTextFontSize;
        int startRowA;
        int startColumnA;
        if (isClean) {
            //清除旧数据痕迹
            int[][] cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * (Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck))][rectangleBitmapArrays[0].length];//rectangleBitmapArrays.length
            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray,
                    SignageDrawUtil.width / 4 - cleanBinaryArray.length / 2,
                    SignageDrawUtil.width / 40);
        }
        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getWait() == null || ter020201DriverBean.getWait() == 0) {
            String text = ter020201DriverBean.getWAlias();
            if (StringUtils.isEmpty(text)) {
                text = "待料";
            }

            showTextFontSize = SignageDrawUtil.textFontSize;
            startColumnA = SignageDrawUtil.width / 40;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }

            int[][] fontBinaryArray;

            if (xZPFontLoaderUtil.isAlphanumeric(text)) {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingNonChinese(text, showNoChineseFontSize);
            } else {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize, false);
            }

//            if(check){

            startColumnA = SignageDrawUtil.width / 40 + rectangleBitmapArrays[0].length / 2 - fontBinaryArray[0].length / 2;

//            }
            startRowA = SignageDrawUtil.width / 4 - fontBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);


        }
    }

    /**
     * 绘制 左屏 保养状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftUpkeepState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showNoChineseFontSize;
        int showTextFontSize;
        int startRowA;
        int startColumnA;

        if (isClean) {
            //清除旧数据痕迹
            int[][] cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * (Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck))][rectangleBitmapArrays[0].length];//rectangleBitmapArrays.length

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray,
                    SignageDrawUtil.width / 4 * 3 - cleanBinaryArray.length / 2,
                    SignageDrawUtil.width / 40
            );
        }

        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getUpkeep() == null || ter020201DriverBean.getUpkeep() == 0) {
            String text = ter020201DriverBean.getUAlias();
            if (StringUtils.isEmpty(text)) {
                text = "保养";
            }

            showTextFontSize = SignageDrawUtil.textFontSize;
            startColumnA = SignageDrawUtil.width / 40;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }
            int[][] fontBinaryArray;

            if (xZPFontLoaderUtil.isAlphanumeric(text)) {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingNonChinese(text, showNoChineseFontSize);
            } else {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize, false);
            }

//            if(check){

            startColumnA = SignageDrawUtil.width / 40 + rectangleBitmapArrays[0].length / 2 - fontBinaryArray[0].length / 2;

//            }
            startRowA = SignageDrawUtil.width / 4 * 3 - fontBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);

        }
    }

    /**
     * 绘制 左屏 封存/维修状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftMaintainState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showNoChineseFontSize;
        int showTextFontSize;
        int startRowA;
        int startColumnA;

        if (isClean) {
            //清除旧数据痕迹
            int[][] cleanBinaryArray = new int[rectangleBitmapArrays.length][SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)];//rectangleBitmapArrays[0].length
            startRowA = SignageDrawUtil.width - SignageDrawUtil.width / 40 - cleanBinaryArray.length;
            startColumnA = SignageDrawUtil.height / 2 - cleanBinaryArray[0].length / 2;
            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray, startRowA, startColumnA);
        }


        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getMaintain() == null || ter020201DriverBean.getMaintain() == 0) {
            String text = ter020201DriverBean.getMAlias();
            if (StringUtils.isEmpty(text)) {
                text = "维修";
            }

            showTextFontSize = SignageDrawUtil.textFontSize;
            startRowA = SignageDrawUtil.width - SignageDrawUtil.width / 40 - showTextFontSize;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }
            int[][] fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize);
//            if(check){

            startRowA = SignageDrawUtil.width - SignageDrawUtil.width / 40 - rectangleBitmapArrays.length / 2 - fontBinaryArray.length / 2;
//            }
            startColumnA = SignageDrawUtil.height / 2 - fontBinaryArray[0].length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);


        }
    }


    /**
     * 绘制 左屏 故障状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftFaultState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showNoChineseFontSize;
        int showTextFontSize;
        int startRowA;
        int startColumnA;
        if (isClean) {
            //清除旧数据痕迹
            int[][] cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * (Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck))][rectangleBitmapArrays[0].length];//rectangleBitmapArrays.length
            startRowA = SignageDrawUtil.width - SignageDrawUtil.width / 4 - cleanBinaryArray.length / 2;
            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - cleanBinaryArray[0].length / 2;//-  cleanBinaryArray.length/2
            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray, startRowA, startColumnA);
        }

        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getFault() == null || ter020201DriverBean.getFault() == 0) {
            String text = ter020201DriverBean.getFAlias();
            if (StringUtils.isEmpty(text)) {
                text = "故障";
            }

            showTextFontSize = SignageDrawUtil.textFontSize;
            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - showTextFontSize;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }

            int[][] fontBinaryArray;

            if (xZPFontLoaderUtil.isAlphanumeric(text)) {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingNonChinese(text, showNoChineseFontSize);
            } else {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize, false);
            }

//            if(check){

            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - rectangleBitmapArrays[0].length / 2 - fontBinaryArray[0].length / 2;

//            }
            startRowA = SignageDrawUtil.width - SignageDrawUtil.width / 4 - fontBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);


        }
    }


    /**
     * 绘制 左屏 停机状态
     *
     * @param canvasBitmapArrays
     * @param check
     * @param rectangleBitmapArrays
     * @param ter020201InfoVo
     */
    public  void canvasBitmapLeftHaltState(int[][] canvasBitmapArrays, boolean check, int[][] rectangleBitmapArrays, Ter020201InfoVo ter020201InfoVo, boolean isClean) {
        int showNoChineseFontSize;
        int showTextFontSize;
        int startRowA;
        int startColumnA;
        if (isClean) {
            //清除旧数据痕迹
            int[][] cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * (Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck))][rectangleBitmapArrays[0].length];//rectangleBitmapArrays.length
            startRowA = SignageDrawUtil.width / 4 - cleanBinaryArray.length / 2;
            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - cleanBinaryArray[0].length / 2;//
            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, cleanBinaryArray, startRowA, startColumnA);
        }

        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        if (ter020201DriverBean.getHalt() == null || ter020201DriverBean.getHalt() == 0) {
            String text = ter020201DriverBean.getHAlias();
            if (StringUtils.isEmpty(text)) {
                text = "停机";
            }
            showTextFontSize = SignageDrawUtil.textFontSize;
            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - showTextFontSize;
            showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;
            if (check) {
                showTextFontSize = SignageDrawUtil.textFontSize;//textFontSizeCheck;
                showNoChineseFontSize = SignageDrawUtil.noChineseFontSize3;//noChineseFontSizeCheck;
            }

            int[][] fontBinaryArray;

            if (xZPFontLoaderUtil.isAlphanumeric(text)) {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingNonChinese(text, showNoChineseFontSize);
            } else {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingText(text, showTextFontSize, showNoChineseFontSize, false);
            }

//            if(check){

            startColumnA = SignageDrawUtil.height - SignageDrawUtil.width / 40 - rectangleBitmapArrays[0].length / 2 - fontBinaryArray[0].length / 2;

//            }
            startRowA = SignageDrawUtil.width / 4 - fontBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);


        }
    }


    /**
     * 绘制左屏 状态选中的矩形选中框
     *
     * @param canvasBitmapArrays
     * @param state
     */
    public  void canvasBitmapLeftCheckState(int[][] canvasBitmapArrays, int state, int[][] rectangleBitmapArrays) {

        int rectangleColumn = 0;
        int rectangleRow = 0;
        switch (state) {
            case 0:
                rectangleColumn = SignageDrawUtil.height / 2 - rectangleBitmapArrays[0].length / 2;
                rectangleRow = SignageDrawUtil.width / 40;
                break;
            case 1:
                rectangleColumn = SignageDrawUtil.width / 40;
                rectangleRow = SignageDrawUtil.width / 4 - rectangleBitmapArrays.length / 2;

                break;
            case 2:
                rectangleColumn = SignageDrawUtil.width / 40;
                rectangleRow = SignageDrawUtil.width / 4 * 3 - rectangleBitmapArrays.length / 2;
                break;
            case 3:
                rectangleColumn = SignageDrawUtil.height / 2 - rectangleBitmapArrays[0].length / 2;
                rectangleRow = SignageDrawUtil.width - SignageDrawUtil.width / 40 - rectangleBitmapArrays.length;
                break;
            case 4:
                rectangleColumn = SignageDrawUtil.height - SignageDrawUtil.width / 40 - rectangleBitmapArrays[0].length;
                rectangleRow = SignageDrawUtil.width / 4 * 3 - rectangleBitmapArrays.length / 2;
                break;
            case 5:
                rectangleColumn = SignageDrawUtil.height - SignageDrawUtil.width / 40 - rectangleBitmapArrays[0].length;
                rectangleRow = SignageDrawUtil.width / 4 - rectangleBitmapArrays.length / 2;
                break;
        }


        DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, rectangleBitmapArrays, rectangleRow, rectangleColumn, 1);

    }

    /**
     * 获取 左屏 状态选中的矩形选择框 图形画布数组
     *
     * @param vertical 是否生成竖向的选中
     * @return
     */
    public  int[][] getRoundedRectangularArray(boolean vertical) {
        int[][] rectangleBitmapArrays = new int[1][1];
        if (!vertical) {
            rectangleBitmapArrays = DotMatrixDrawShapeUtil.createRoundedRectangularArray(SignageDrawUtil.height / 35 * 11, SignageDrawUtil.width / 10, SignageDrawUtil.width / 80);
        } else {
            rectangleBitmapArrays = DotMatrixDrawShapeUtil.createRoundedRectangularArray(SignageDrawUtil.width / 10, SignageDrawUtil.height / 35 * 11, SignageDrawUtil.width / 80);
        }
        return rectangleBitmapArrays;
    }


    /**
     * 清除状态区域内容 (以选中状态清除)
     *
     * @param canvasBitmapArrays
     * @param state              -1为全部清除
     */
    public  void cleanCanvasBitmapLeftState(int[][] canvasBitmapArrays, int state) {
        int rectangleColumn = 0;
        int rectangleRow = 0;

        int[][] cleanBinaryArray;

        if (state == -1 || state == 0) {

            cleanBinaryArray = new int[SignageDrawUtil.width / 10][SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)];

            rectangleColumn = SignageDrawUtil.height / 2 - cleanBinaryArray[0].length / 2;
            rectangleRow = SignageDrawUtil.width / 40;
            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }

        if (state == -1 || state == 1) {

            cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)][SignageDrawUtil.width / 10];

            rectangleColumn = SignageDrawUtil.width / 40;
            rectangleRow = SignageDrawUtil.width / 4 - cleanBinaryArray.length / 2;
            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }

        if (state == -1 || state == 2) {

            cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)][SignageDrawUtil.width / 10];

            rectangleColumn = SignageDrawUtil.width / 40;
            rectangleRow = SignageDrawUtil.width / 4 * 3 - cleanBinaryArray.length / 2;


            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }

        if (state == -1 || state == 3) {

            cleanBinaryArray = new int[SignageDrawUtil.width / 10][SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)];

            rectangleColumn = SignageDrawUtil.height / 2 - cleanBinaryArray[0].length / 2;
            rectangleRow = SignageDrawUtil.width - SignageDrawUtil.width / 40 - cleanBinaryArray.length;

            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }

        if (state == -1 || state == 4) {

            cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)][SignageDrawUtil.width / 10];

            rectangleColumn = SignageDrawUtil.height - SignageDrawUtil.width / 40 - cleanBinaryArray[0].length;
            rectangleRow = SignageDrawUtil.width / 4 * 3 - cleanBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }

        if (state == -1 || state == 5) {

            cleanBinaryArray = new int[SignageDrawUtil.leftStateMax * Math.max(SignageDrawUtil.textFontSizeCheck, SignageDrawUtil.noChineseFontSizeCheck)][SignageDrawUtil.width / 10];

            rectangleColumn = SignageDrawUtil.height - SignageDrawUtil.width / 40 - cleanBinaryArray[0].length;
            rectangleRow = SignageDrawUtil.width / 4 - cleanBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, cleanBinaryArray, rectangleRow, rectangleColumn, 0);
        }


    }


    /*********** 绘制 左图图表  ********************/


    /**
     * 绘制 图表0 ------ 指针款
     *
     * @param state
     * @param ter020201InfoVo
     */
    public  int[][] canvasBitmapLeftChart0(int state, Ter020201InfoVo ter020201InfoVo) {

        //指针图像
//        int stateWidth = 350;
//        int stateHeight = 350;

        int stateWidth = SignageDrawUtil.height - SignageDrawUtil.width / 10 * 2 - SignageDrawUtil.width / 40 * 2;
        int stateHeight = SignageDrawUtil.width - SignageDrawUtil.width / 10 * 2 - SignageDrawUtil.width / 40 * 2;

        int[][] canvasBitmapArrays = new int[stateHeight][stateWidth];


        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        String centerText = null;
        switch (state) {
            case 0:
                if (ter020201DriverBean.getRun() == null || ter020201DriverBean.getRun() == 0) {
                    centerText = ter020201DriverBean.getRAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "运行";
                    }
                }
                break;
            case 1:
                if (ter020201DriverBean.getWait() == null || ter020201DriverBean.getWait() == 0) {
                    centerText = ter020201DriverBean.getWAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "待料";
                    }
                }

                break;
            case 2:
                if (ter020201DriverBean.getUpkeep() == null || ter020201DriverBean.getUpkeep() == 0) {
                    centerText = ter020201DriverBean.getUAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "保养";
                    }
                }
                break;
            case 3:
                if (ter020201DriverBean.getMaintain() == null || ter020201DriverBean.getMaintain() == 0) {
                    centerText = ter020201DriverBean.getMAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "维修";//封存
                    }
                }
                break;
            case 4:
                if (ter020201DriverBean.getFault() == null || ter020201DriverBean.getFault() == 0) {
                    centerText = ter020201DriverBean.getFAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "故障";
                    }
                }
                break;
            case 5:
                if (ter020201DriverBean.getHalt() == null || ter020201DriverBean.getHalt() == 0) {
                    centerText = ter020201DriverBean.getHAlias();
                    if (StringUtils.isEmpty(centerText)) {
                        centerText = "停机";
                    }
                }
                break;

        }

        int startColumnA;
        int startRowA;


        //绘制中心选择的文字
        if (centerText != null) {
            int[][] fontBinaryArray;

            if (xZPFontLoaderUtil.isAlphanumeric(centerText)) {
                fontBinaryArray = dotMatrixDrawtextUtil.drawingText(centerText, SignageDrawUtil.noChineseFontCenterCheck2, SignageDrawUtil.noChineseFontCenterCheck2);
            } else {
                int textRows = centerText.length() / 2;//文字显示的行数
                if (centerText.length() % 2 > 0) {
                    textRows = textRows + 1;
                }
                //依据比例放大缩小文字的大小
                int textSize = SignageDrawUtil.textFontSizeCenterCheck - (textRows - 1) * 10;
                int noChineseTextSize = SignageDrawUtil.noChineseFontCenterCheck - (textRows - 1) * 4;


                //获取字符串中的每一个字符
                char[] centerTextArray = centerText.toCharArray();

                List<int[][]> fontBinaryArrayList = new ArrayList<>();

                int sumHeight = 0;
                int maxWidth = 0;
                int maxHeight = 0;

                for (int i = 0; i < textRows; i++) {
                    String str = String.valueOf(centerTextArray[i * 2]);
                    if (centerTextArray.length > i * 2 + 1) {
                        str = str + String.valueOf(centerTextArray[i * 2 + 1]);
                    }

                    int[][] fontBinaryArray2 = dotMatrixDrawtextUtil.drawingText(str, textSize, noChineseTextSize);

                    sumHeight = sumHeight + fontBinaryArray2.length;

                    if (fontBinaryArray2[0].length > maxWidth) {
                        maxWidth = fontBinaryArray2[0].length;
                    }

                    if (fontBinaryArray2.length > maxHeight) {
                        maxHeight = fontBinaryArray2.length;
                    }

                    fontBinaryArrayList.add(fontBinaryArray2);

                }

                fontBinaryArray = new int[sumHeight]
                        [maxWidth];

                for (int i = 0; i < fontBinaryArrayList.size(); i++) {
                    int[][] fontBinaryArray2 = fontBinaryArrayList.get(i);

                    DotMatrixCanvasUtil.mergeDotMatrices(
                            fontBinaryArray,
                            fontBinaryArray2,
                            maxHeight * i,
                            fontBinaryArray[0].length / 2 - fontBinaryArray2[0].length / 2);
                }

            }


            startColumnA = stateWidth / 2 - fontBinaryArray[0].length / 2;
            startRowA = stateHeight / 2 - fontBinaryArray.length / 2;

            DotMatrixCanvasUtil.mergeDotMatrices(canvasBitmapArrays, fontBinaryArray, startRowA, startColumnA);
        }


        String resourcePath = "image/device/020201/left_state_" + state + ".png";

        byte[] stateBitmapByte = DotMatrixImageUtil.canvasImageBitmapArrays(resourcePath, 350, 350, 140);

        int[][] stateBitmapArray = DotMatrixImageUtil.twoToCanvasBitmapArrays(stateBitmapByte, 350, 350);

        startColumnA = stateWidth / 2 - 350 / 2;
        startRowA = stateHeight / 2 - 350 / 2;
        DotMatrixCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays, stateBitmapArray, startRowA, startColumnA, 1);


        //先绘制出圆形  注释掉 自己绘制的逻辑 暂时采用图片的方式 原因是因为 将指针进行旋转时由于三角函数的小数点关系会导致图像丢失很多数据点 后续有时间再优化
//        int[][] radiusArray = xzpBitmapDrawingUtil.createCustomRadiusArray(width/8);
//        startColumnB =  width/2 - radiusArray[0].length/2;
//        startRowB =  height/2 - radiusArray.length/2;
//
//
//        //绘制指针
//        int[][]  triangleArray = xzpBitmapDrawingUtil.createCustomTriangleArray(width/4 - width/15,width/7,width/8);
//
//        startColumnA =  width/2 - triangleArray[0].length/2;//;
//        startRowA =  height/2 -  radiusArray[0].length/2 -triangleArray.length +  height/50;;// //;
//        XZPCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays,triangleArray,startRowA,startColumnA,0);
//
//
//        //绘制出圆形
//        XZPCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays,radiusArray,startRowB,startColumnB,1);
//
//
//
//        //绘制第二个指针
//        int[][]  triangleArray2 = xzpBitmapDrawingUtil.rotateDotMatrix(triangleArray,60);
//        startColumnA = 0; //width/2 - triangleArray2[0].length;//;
//        startRowA = 0; //height/2 -  radiusArray[0].length -triangleArray2.length;;// //;
//        XZPCanvasUtil.mergeDotMatricesConfuse(canvasBitmapArrays,triangleArray2,startRowA,startColumnA,0);
//


        return canvasBitmapArrays;

    }


    /**
     * 绘制 图表1 ------ 打卡数据图表款
     * //     * @param canvasBitmapArrays
     *
     * @param ter020201InfoVo
     */
    public  int[][] canvasBitmapLeftChart1(Ter020201InfoVo ter020201InfoVo, boolean isOne) {

        List<Long> checkData = new ArrayList<>();
        checkData.add(0L);
        checkData.add(0L);
        checkData.add(0L);
        checkData.add(0L);

        Long[] checkHistoryArray = ter020201InfoVo.getWorkingHoursHistoryArray();

        if (checkHistoryArray != null) {
            checkData.set(0, checkHistoryArray[0]);
            checkData.set(1, checkHistoryArray[1]);
            checkData.set(2, checkHistoryArray[2]);
            checkData.set(3, checkHistoryArray[3]);
        }

        return drawLeftBarChart(checkData, isOne);

        //绘制图表
//        xzpBitmapDrawingUtil.drawLeftBarChart(canvasBitmapArrays,
//                checkData,
//                height/3,
//                width/4,
//                true
//        );
    }

    /**
     * 绘制 图表1 ------ 打卡数据图表款 柱状图的下标文字
     * //     * @param canvasBitmapArrays
     *
     * @param ter020201InfoVo
     */
    public  int[][] canvasBitmapLeftChart1Text(//int[][] canvasBitmapArrays,
                                                     Ter020201InfoVo ter020201InfoVo, boolean isOne) {
        List<String> checkNameData = new ArrayList<>();

        TiClockBean tiClock = ter020201InfoVo.getTiClock();
        if (tiClock == null) {
            tiClock = new TiClockBean();
        }

        String centerText = "";
        if (tiClock.getProduce() == null || tiClock.getProduce() == 0) {
            centerText = tiClock.getPAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "生产";
            }
        }
        checkNameData.add(centerText);

        centerText = "";
        if (tiClock.getMaintain() == null || tiClock.getMaintain() == 0) {
            centerText = tiClock.getMAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "维修";
            }
        }
        checkNameData.add(centerText);

        centerText = "";
        if (tiClock.getInspect() == null || tiClock.getInspect() == 0) {
            centerText = tiClock.getIAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "巡检";
            }
        }
        checkNameData.add(centerText);

        centerText = "";
        if (tiClock.getUpkeep() == null || tiClock.getUpkeep() == 0) {
            centerText = tiClock.getUAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "保养";
            }
        }
        checkNameData.add(centerText);


        List<Long> checkData = Arrays.asList(ter020201InfoVo.getWorkingHoursHistoryArray());

        //绘制图表文字
        return drawLeftBarChartText(checkNameData, checkData, isOne);

    }


    /**
     * 绘制 图表2 ------ 状态记录数据图表款
     * //     * @param canvasBitmapArrays
     *
     * @param ter020201InfoVo
     */
    public  int[][] canvasBitmapLeftChart2(//int[][] canvasBitmapArrays,
                                                 Ter020201InfoVo ter020201InfoVo, boolean isOne) {

        List<Long> stateData = new ArrayList<>();
        stateData.add(0L);
        stateData.add(0L);
        stateData.add(0L);
        stateData.add(0L);
        stateData.add(0L);
        stateData.add(0L);

        Long[] stateHistoryArray = ter020201InfoVo.getStateHistoryArray();
        if (stateHistoryArray != null) {
            stateData.set(0, stateHistoryArray[0]);
            stateData.set(1, stateHistoryArray[1]);
            stateData.set(2, stateHistoryArray[2]);
            stateData.set(3, stateHistoryArray[3]);
            stateData.set(4, stateHistoryArray[4]);
            stateData.set(5, stateHistoryArray[5]);
        }

        return drawLeftBarChart(stateData, isOne);

        //绘制图表1 状态表
//        xzpBitmapDrawingUtil.drawLeftBarChart(canvasBitmapArrays,
//                stateData,
//                height/3,
//                width/7,
//                true
//        );
    }


    /**
     * 绘制 图表2 ------ 状态记录数据图表款 底部文字
     * //     * @param canvasBitmapArrays
     *
     * @param ter020201InfoVo
     */
    public  int[][] canvasBitmapLeftChart2Text(//int[][] canvasBitmapArrays,
                                                     Ter020201InfoVo ter020201InfoVo, boolean isOne) {

        List<String> stateNameData = new ArrayList<>();//状态名称

        Ter020201DriverBean ter020201DriverBean = ter020201InfoVo.getTer020201DriverBean();
        if (ter020201DriverBean == null) {
            ter020201DriverBean = new Ter020201DriverBean();
        }
        String centerText = "";
        if (ter020201DriverBean.getRun() == null || ter020201DriverBean.getRun() == 0) {
            centerText = ter020201DriverBean.getRAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "运行";
            }
        }
        stateNameData.add(centerText);

        centerText = "";
        if (ter020201DriverBean.getWait() == null || ter020201DriverBean.getWait() == 0) {
            centerText = ter020201DriverBean.getWAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "待料";
            }
        }
        stateNameData.add(centerText);


        centerText = "";
        if (ter020201DriverBean.getHalt() == null || ter020201DriverBean.getHalt() == 0) {
            centerText = ter020201DriverBean.getHAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "停机";
            }
        }
        stateNameData.add(centerText);


        centerText = "";
        if (ter020201DriverBean.getUpkeep() == null || ter020201DriverBean.getUpkeep() == 0) {
            centerText = ter020201DriverBean.getUAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "保养";
            }
        }
        stateNameData.add(centerText);


        centerText = "";
        if (ter020201DriverBean.getFault() == null || ter020201DriverBean.getFault() == 0) {
            centerText = ter020201DriverBean.getFAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "故障";
            }
        }
        stateNameData.add(centerText);

        centerText = "";
        if (ter020201DriverBean.getMaintain() == null || ter020201DriverBean.getMaintain() == 0) {
            centerText = ter020201DriverBean.getMAlias();
            if (StringUtils.isEmpty(centerText)) {
                centerText = "维修";//封存
            }
        }
        stateNameData.add(centerText);


        List<Long> stateData = Arrays.asList(ter020201InfoVo.getStateHistoryArray());

        return drawLeftBarChartText(stateNameData,
                stateData, isOne);
//        //绘制图表1 状态表  文字
//        xzpBitmapDrawingUtil.drawLeftBarChartText(canvasBitmapArrays,
//                stateNameData,
//                stateData,
//                height/3,
//                width/7,
//                true
//        );
    }


    /**
     * 绘制 左屏 柱状图图表 柱状图文字
     *
     * @param stateNameData 状态名称数据
     * @param stateData     状态数据 只使用到了长度无视内容
     */
    public  int[][] drawLeftBarChartText(List<String> stateNameData,
                                               List<Long> stateData,
                                               boolean isOne
    ) {
        int barChartWidth2 = SignageDrawUtil.barChartWidth;
        int barChartInterval2 = SignageDrawUtil.barChartInterval;
        if (isOne) {
            barChartWidth2 = SignageDrawUtil.barChartWidth * 2;
            barChartInterval2 = SignageDrawUtil.barChartInterval * 2;
        }

        //绘制状态表

        //左侧Y轴的数据大小
        int dataPart = stateData.size();//柱状图数据段

        //绘制出底部的文字
        int[][] xAxisCanvasBitmapArrays = new int[Math.max(SignageDrawUtil.textFontSize2Height, SignageDrawUtil.noChineseFontSizeHeight) * 7]
                [barChartWidth2 * dataPart + barChartInterval2 * dataPart];

        for (int i = 0; i < stateNameData.size(); i++) {
            String xData = stateNameData.get(i);

            if (!StringUtils.isEmpty(xData)) {

                int startColumnX = i * (barChartWidth2 + barChartInterval2) + barChartInterval2 / 2 + barChartWidth2 / 2;
                int startRow = Math.max(SignageDrawUtil.textFontSize2Height, SignageDrawUtil.noChineseFontSizeHeight);

                //判断是否是纯英文或者数字
                if (xZPFontLoaderUtil.isAlphanumeric(xData)) {
                    int[][] fontBinaryArray = dotMatrixDrawtextUtil.drawingNonChinese(xData, SignageDrawUtil.noChineseFontSize);
                    DotMatrixCanvasUtil.mergeDotMatrices(
                            xAxisCanvasBitmapArrays,
                            fontBinaryArray,
                            startRow,
                            startColumnX - fontBinaryArray[0].length / 2);
                } else {

                    //获取字符串中的每一个字符
                    char[] xDataArray = xData.toCharArray();
                    int lastRow = 0;
                    for (char str : xDataArray) {

                        int[][] fontBinaryArray = dotMatrixDrawtextUtil.drawingText(
                                String.valueOf(str),
                                SignageDrawUtil.textFontSize2,
                                SignageDrawUtil.noChineseFontSize,
                                false);


                        int startColumnX2 = startColumnX - fontBinaryArray[0].length / 2;
                        if (startColumnX2 <= 0) {
                            startColumnX2 = 0;
                        }

                        startRow = startRow + lastRow;
                        lastRow = fontBinaryArray.length;

                        DotMatrixCanvasUtil.mergeDotMatrices(
                                xAxisCanvasBitmapArrays,
                                fontBinaryArray,
                                startRow,
                                startColumnX2);

                    }
                }


            }


        }


        return xAxisCanvasBitmapArrays;

//        //绘制底部文字
//        mergeDotMatrices(largeMatrix,xAxisCanvasBitmapArrays,
//                startRow + barChartHigh + textFontSize2,
//                startColumn);

    }

    /**
     * 绘制 左屏 柱状图图表 柱状图
     *
     * @param stateData 状态数据
     */
    public  int[][] drawLeftBarChart(List<Long> stateData, boolean isOne) {

        int numberSize2 = SignageDrawUtil.numberSize;
        int numberHigh2 = SignageDrawUtil.numberHigh;
        int barChartWidth2 = SignageDrawUtil.barChartWidth;
        int barChartInterval2 = SignageDrawUtil.barChartInterval;

        if (isOne) {
            numberSize2 = SignageDrawUtil.numberSize * 2;
            numberHigh2 = SignageDrawUtil.numberHigh * 2;

            barChartWidth2 = SignageDrawUtil.barChartWidth * 2;
            barChartInterval2 = SignageDrawUtil.barChartInterval * 2;

        }

        //绘制状态表

        //左侧Y轴的数据大小
        long maxNumber = Collections.max(stateData);
        long minNumber = Collections.min(stateData);


        int dataPart = stateData.size();//柱状图数据段

        //得出图像点阵的大小
        int chartHigh = SignageDrawUtil.barChartHigh;
        int chartWidth = dataPart * barChartWidth2 + barChartInterval2 * dataPart;


        //绘制柱状图+柱子上面的文字
        int[][] barChartBitmapArrays = new int[chartHigh + numberHigh2 * 2][chartWidth];//numberHigh2*2 是 文字和间隔


        if (maxNumber > 0) { //无视没有数值的情况
            for (int i = 0; i < stateData.size(); i++) {

                long number = stateData.get(i) * (barChartBitmapArrays.length - numberHigh2 * 2) / maxNumber;

                if (number <= 0) {
                    number = 0;
                }

                for (int j = numberHigh2 * 2; j < barChartBitmapArrays.length; j++) {//  0 - numberHigh2*2

                    if (j >= barChartBitmapArrays.length - number) {// - numberHigh2*2

                        for (int k = 0; k < barChartBitmapArrays[0].length; k++) {

                            int leftInterval = barChartInterval2 / 2 + i * (barChartWidth2 + barChartInterval2);
                            int rightInterval = barChartWidth2 + barChartInterval2 / 2 + i * (barChartWidth2 + barChartInterval2);

                            if (k >= leftInterval
                                    && k <= rightInterval) {
                                barChartBitmapArrays[j][k] = 1;
                            }

                        }

                    }
                }

                //在柱状图的上面显示 文字
                int textStart = barChartInterval2 / 2 + i * (barChartWidth2 + barChartInterval2) + barChartWidth2 / 2;//左侧间隔加柱子的一半

                //文字本身的大小
                int fontSize = numberSize2 * String.valueOf(stateData.get(i)).length();

                //文字的高度
//                int row = (int) (barChartBitmapArrays.length- number - numberHigh2*2 - numberHigh2/2  - numberHigh2);//  barChartInterval/2 放大时会超出
                int row = (int) (barChartBitmapArrays.length - number - numberHigh2 - numberHigh2);

                dotMatrixDrawtextUtil.drawingText(barChartBitmapArrays, String.valueOf(stateData.get(i)), row, textStart - fontSize / 2, numberSize2);

            }

        }


        //绘制出底部的实线
        DotMatrixDrawShapeUtil.drawLine(barChartBitmapArrays, 1, 0, 0, barChartBitmapArrays.length - 1, 0, chartWidth);

        return barChartBitmapArrays;

        //绘制图表
//        mergeDotMatrices(largeMatrix,barChartBitmapArrays,
//                startRow,
//                startColumn);

    }

}
