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;

import java.net.ContentHandler;

public class PrintLabel100 {


	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 - 5 * margin_vertical);
	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 = { 9 * MULTIPLE, 9 * MULTIPLE, 9 * MULTIPLE,9 * MULTIPLE,9 * MULTIPLE };
	private static int startX;
	private static int startX1;
	private static Context mContext;
	public PrintLabel100(Context context){
		this.mContext=context;
	}
	public  synchronized static void doPrint(PrinterInstance iPrinter){
		iPrinter.pageSetup(LablePaperType.Size_100mm,page_width, page_height);
		drawBox(iPrinter);
		drawVerticalSeparator(iPrinter);
		drawRowContent(iPrinter);
		iPrinter.print(PRotate.Rotate_0, 1);

	}

	private static void drawBox(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);
		drawHorizontalSeparator(iPrinter);

	}
	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;
			int end_x = top_right_x;
			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)*3;
		startX1 = top_left_x + ((top_right_x - top_left_x) / 10)*5;
		int startY = top_left_y+row_height[0];
		int endY =  top_left_y+row_height[0]*5;
		iPrinter.drawLine(line_width_border, startX, startY,startX, 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.CENTER,
				mContext.getString(R.string.lable_text_100mm_text1),LableFontSize.Size_64, 1, 0, 0, 0, PRotate.Rotate_0);
		//第二行内容 左边栏内容
		iPrinter.drawText(top_left_x, top_left_y+row_height[0], startX, top_left_y+row_height[0]*2, PAlign.CENTER, PAlign.CENTER,
				mContext.getString(R.string.lable_text_100mm_text2), LableFontSize.Size_32, 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.lable_text_100mm_text3),  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]*3, PAlign.CENTER, PAlign.CENTER,
				mContext.getString(R.string.lable_text_100mm_text4),  LableFontSize.Size_32, 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.lable_text_100mm_text5),  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]*4, PAlign.CENTER, PAlign.CENTER,
								mContext.getString(R.string.lable_text_100mm_text6),  LableFontSize.Size_32, 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.START, PAlign.CENTER,
								mContext.getString(R.string.lable_text_100mm_text7),  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]*5, PAlign.CENTER, PAlign.CENTER,
						mContext.getString(R.string.lable_text_100mm_text8),  LableFontSize.Size_32, 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.lable_text_100mm_text9),  LableFontSize.Size_32, 0, 0, 0, 0, PRotate.Rotate_0);
		//第6行左侧条码
		iPrinter.drawBarCode(top_left_x+35, top_left_y+row_height[0]*5, startX1+35, bottom_right_y-40, PAlign.CENTER, PAlign.CENTER, 0, 0,
				mContext.getString(R.string.lable_text_100mm_text10),  PBarcodeType.CODE128, 1, 80, PRotate.Rotate_0);
		//第6行左侧条码下面的内容
		iPrinter.drawText(top_left_x, top_left_y+row_height[0]*5+90, startX1, bottom_right_y, PAlign.CENTER, PAlign.CENTER,
								mContext.getString(R.string.lable_text_100mm_text11), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);
//		iPrinter.drawQrCode(startX1+10, top_left_y+row_height[0]*5+30,
//				"https://www.baidu.com/",
//				PRotate.Rotate_0, startX, startX);

		//第6行左侧条码下面的内容
		iPrinter.drawText(startX1+180, top_left_y+row_height[0]*5, bottom_right_x, bottom_right_y, PAlign.CENTER, PAlign.CENTER,
										mContext.getString(R.string.lable_text_100mm_text12), LableFontSize.Size_24, 0, 0, 0, 0, PRotate.Rotate_0);


	}




}
