package com.printer.demo.utils;

import android.content.Context;
import android.util.Log;

import com.printer.demo.R;
import com.printer.sdk.PrinterConstants.LableFontSize;
import com.printer.sdk.PrinterConstants.LablePaperType;
import com.printer.sdk.PrinterConstants.PAlign;
import com.printer.sdk.PrinterConstants.PBarcodeType;
import com.printer.sdk.PrinterConstants.PRotate;
import com.printer.sdk.PrinterInstance;

public class PrintLablel {

    private static int MULTIPLE = 11;
    private static final int line_width_border = 1;
    private static final int page_width = 75 * MULTIPLE;
    private static final int page_height = 80 * MULTIPLE;
    private static final int margin_horizontal = (int) (1.5 * MULTIPLE);
    private static final int top_left_x = margin_horizontal;
    private static final int margin_vertical = 3 * MULTIPLE;
    private static final int top_left_y = margin_vertical;// 32
    private static final int border_width = page_width - 2 * margin_horizontal;
    private static final int border_height = (int) (page_height - 8
            * margin_vertical - 8);
    private static final int top_right_x = top_left_x + border_width;
    private static final int bottom_left_y = top_left_y + border_height;
    private static final int bottom_right_y = bottom_left_y;
    private static final int bottom_right_x = top_right_x;
    private static final int row36_column1_width = 10 * MULTIPLE;
    private static final int row37_column3_width = 20 * MULTIPLE;
    private static final int row36_sep1_x = top_left_x + row36_column1_width;
    private static final int row37_sep2_x = top_right_x - row37_column3_width;
    private static final int[] row_height = {6 * MULTIPLE, 6 * MULTIPLE,
            6 * MULTIPLE, 6 * MULTIPLE, 6 * MULTIPLE, 6 * MULTIPLE,
            6 * MULTIPLE, 6 * MULTIPLE};
    private static int startX;
    private static int startX1;
    private static  Context mContext;
    public  PrintLablel(Context context){
        this.mContext=context;
    }
    public synchronized static void doPrint(PrinterInstance iPrinter) {
        iPrinter.pageSetup(LablePaperType.Size_80mm, 75*8, page_height);
        drawBigBox(iPrinter);
        drawHorizontalSeparator(iPrinter);
        drawVerticalSeparator(iPrinter);
        drawRowContent(iPrinter);
        iPrinter.print(PRotate.Rotate_0, 1);

    }

    private static void drawBigBox(PrinterInstance iPrinter) {
        int border_top_left_y = top_left_y;
        // int border_top_left_y = top_left_y + row_height[0];
        iPrinter.drawBorder(3, top_left_x, top_left_y, bottom_right_x,
                bottom_right_y);
        Log.i("fdh", "bottom_right_x0:" + bottom_right_x);

    }

    private static void drawHorizontalSeparator(PrinterInstance iPrinter) {

        int temp = top_left_y; //
        // int temp = top_left_y+row_height[0]; //
        for (int i = 0; i < row_height.length; i++) {
            temp += row_height[i];
            int start_x = top_left_x + 10;
            int end_x = (int) (top_left_x + ((top_right_x - top_left_x) / 10) * 6.9);
            Log.i("temp", "第" + (i + 1) + "次");
            iPrinter.drawLine(line_width_border, start_x, temp, end_x, temp,
                    false);
            /*
			 * if(i!= 3){ iPrinter.drawLine(line_width_border, start_x, temp,
			 * end_x, temp); }else{ iPrinter.drawLine(line_width_border,
			 * row37_sep2_x, temp, end_x, temp); }
			 */

        }
    }

    private static void drawVerticalSeparator(PrinterInstance iPrinter) {

        startX = top_left_x + ((top_right_x - top_left_x) / 10) * 2;
        startX1 = (int) (top_left_x + ((top_right_x - top_left_x) / 10) * 6.9);
        int startY = top_left_y + row_height[0];
        int endY = top_left_y + row_height[0] * 8;
        iPrinter.drawLine(line_width_border, startX, startY, startX, endY,
                false);
        iPrinter.drawLine(line_width_border * 2, top_left_x + 10, startY,
                top_left_x + 10, endY, false);
        iPrinter.drawLine(line_width_border * 2, startX1, startY,
                startX1, endY, false);

        // iPrinter.drawLine(line_width_border, startX1, endY, startX1,
        // bottom_right_y, false);

    }

    private static void drawRowContent(PrinterInstance iPrinter) {

        // 第一行内容
        iPrinter.drawText(top_left_x, top_left_y, top_right_x, top_left_y
                        + row_height[0], PAlign.CENTER, PAlign.END,mContext.getString(R.string.label_text_80mm_text1),
                LableFontSize.Size_64, 1, 0, 0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y, top_right_x, top_left_y
                        + row_height[0], PAlign.END, PAlign.CENTER, mContext.getString(R.string.label_text_80mm_text2),
                LableFontSize.Size_24, 1, 0, 0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y, top_right_x, top_left_y
                        + row_height[0], PAlign.END, PAlign.END, mContext.getString(R.string.label_text_80mm_text3),
                LableFontSize.Size_16, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第二行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0], startX,
                top_left_y + row_height[0] + row_height[0] / 2, PAlign.CENTER,
                PAlign.END, mContext.getString(R.string.label_text_80mm_text4), LableFontSize.Size_24, 0, 0, 0, 0,
                PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0]
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 2, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text5), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);
        // Log.i("sp", "startX:"+startX+"top_right_x:"+top_right_x);
        // 第二行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0], bottom_right_x,
                top_left_y + row_height[0] * 2, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text6), LableFontSize.Size_32, 0, 0, 0, 0, PRotate.Rotate_0);

        // 第三行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 2, startX,
                top_left_y + row_height[0] * 2 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text7), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 2
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 3, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text8), LableFontSize.Size_24, 0, 0, 0, 0,
                PRotate.Rotate_0);
        // 第三行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 2, top_right_x,
                top_left_y + row_height[0] * 3, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text9), LableFontSize.Size_32, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第四行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 3, startX,
                top_left_y + row_height[0] * 3 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text10), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 3
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 4, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text11), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);

        // 第四行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 3, top_right_x,
                top_left_y + row_height[0] * 4, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text12), LableFontSize.Size_32, 0, 0, 0, 0,
                PRotate.Rotate_0);
        // 第五行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 4, startX,
                top_left_y + row_height[0] * 4 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text13), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 4
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 5, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text14), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);

        // 第五行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 4, top_right_x,
                top_left_y + row_height[0] * 5, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text15), LableFontSize.Size_32, 0, 0, 0, 0,
                PRotate.Rotate_0);
        // 第六行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 5, startX,
                top_left_y + row_height[0] * 5 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text16), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 5
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 6, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text17), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第六行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 5, top_right_x,
                top_left_y + row_height[0] * 6, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text18), LableFontSize.Size_32, 0, 0, 0, 0,
                PRotate.Rotate_0);
        // 第七行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 6, startX,
                top_left_y + row_height[0] * 6 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text19), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 6
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 7, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text20), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第七行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 6, top_right_x,
                top_left_y + row_height[0] * 7, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text21), LableFontSize.Size_32, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第八行内容 左边栏内容
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 7, startX,
                top_left_y + row_height[0] * 7 + row_height[0] / 2,
                PAlign.CENTER, PAlign.END, mContext.getString(R.string.label_text_80mm_text22), LableFontSize.Size_24, 0, 0,
                0, 0, PRotate.Rotate_0);
        iPrinter.drawText(top_left_x, top_left_y + row_height[0] * 7
                        + row_height[0] / 2 + 4, startX,
                top_left_y + row_height[0] * 8, PAlign.CENTER, PAlign.START,
                mContext.getString(R.string.label_text_80mm_text23), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);
        // 第八行内容 右边栏内容
        iPrinter.drawText(startX, top_left_y + row_height[0] * 7, top_right_x,
                top_left_y + row_height[0] * 8, PAlign.CENTER, PAlign.CENTER,
                mContext.getString(R.string.label_text_80mm_text24), LableFontSize.Size_32, 0, 0, 0, 0, PRotate.Rotate_0);
        //打印第九行条码

        Log.i("fdh", "bottom_right_x:" + bottom_right_x);
        iPrinter.drawBarCode(top_left_x, top_left_y + row_height[0] * 8, 576, bottom_right_y, PAlign.CENTER, PAlign.CENTER, 0, 0,
                mContext.getString(R.string.label_text_80mm_text25), PBarcodeType.CODE128, 1, 70, PRotate.Rotate_0);

    }

}
