package com.icod.yk_printer_test;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.icod.yk_printer_test.utils.UIUtils;
import com.szsicod.print.escpos.PrinterAPI;

public class MarkActivity extends Activity {
	private String[] printer_type_string = { "针式打印机", "热敏打印机"};
	public static final int PRINTER_TYPE_ONE=1;
	public static final int PRINTER_TYPE_TWO=2;
	public static int printerType=0;
	private Spinner spinnerPrinterType;
	private RadioButton radioButtonOne;
	private RadioButton radioButtonTwo;
	private EditText edit_TL;
	private EditText edit_PL;
	private EditText edit_QO;
	private EditText edit_CL;
	private EditText edit_LO;
	private Button buttonMarkTest;
	private Button buttonMarkSet;
	private Button buttonSetTest;
	private boolean isPrinterType=false;
	private ProgressDialog progressDialog;
	private int serialFlowControl = 0;
	static private int openfileDialogId = 0;
	private EditText mGsOne;
	private EditText mGsTwo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mark); 
		final Context context = this;
		 edit_TL=(EditText) findViewById(R.id.edit_ticket_length);
		 edit_PL=(EditText) findViewById(R.id.edit_print_length);
		 edit_QO=(EditText) findViewById(R.id.edit_QO);
		 edit_CL=(EditText) findViewById(R.id.edit_cut_length);
		 edit_LO=(EditText) findViewById(R.id.edit_LO);
		 radioButtonOne=(RadioButton) findViewById(R.id.radioButton_one);
		 radioButtonTwo=(RadioButton) findViewById(R.id.radioButton_two);
		radioButtonOne=(RadioButton) findViewById(R.id.radioButton_one);
		radioButtonTwo=(RadioButton) findViewById(R.id.radioButton_two);
		spinnerPrinterType=(Spinner) findViewById(R.id.spinner_printer_type);
		mGsOne= (EditText) findViewById(R.id.et_gs_1);
		mGsTwo= (EditText) findViewById(R.id.et_gs_2);
		ArrayAdapter<String> adapterTemp = null;
		adapterTemp = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, printer_type_string);
		adapterTemp
		.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerPrinterType.setAdapter(adapterTemp);
		spinnerPrinterType.setVisibility(View.VISIBLE);
		spinnerPrinterType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				String sInfo = parent.getItemAtPosition(position)
						.toString();

				if(sInfo.equals(printer_type_string[0])){
					printerType=PRINTER_TYPE_ONE;
				}else if(sInfo.equals(printer_type_string[1])){
					printerType=PRINTER_TYPE_TWO;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				printerType=PRINTER_TYPE_ONE;
			}
		});

		buttonMarkTest=(Button) findViewById(R.id.button_mark_test);
		buttonMarkTest.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//发送初始化指令
				//setMarkDefault();
				if (PrinterAPI.getInstance().isConnect())
				 PrinterAPI.getInstance().markDefalut();
			}
		});
		buttonMarkSet=(Button) findViewById(R.id.button_mark_set);
		buttonMarkSet.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				 int tl=Integer.parseInt(edit_TL.getText().toString().trim());
				 int pl=Integer.parseInt(edit_PL.getText().toString().trim());
				 int qo=Integer.parseInt(edit_QO.getText().toString().trim());
				 int cl=Integer.parseInt(edit_CL.getText().toString().trim());
				 int lo=Integer.parseInt(edit_LO.getText().toString().trim());
                if (!PrinterAPI.getInstance().isConnect()){

                    Toast.makeText(MarkActivity.this, UIUtils.getString(R.string.no_connect),Toast
                            .LENGTH_LONG)
                            .show();

                }
				if(radioButtonOne.isChecked()){
				//票据格式1

						PrinterAPI.getInstance().markLengthSet
					(printerType, 0, qo, lo, pl, tl, cl);
				}else if(radioButtonTwo.isChecked()){
				//票据格式2

					PrinterAPI.getInstance().markLengthSet
					(printerType, 1, qo, lo, pl, tl, cl);


				}
			}
		});
		buttonSetTest=(Button) findViewById(R.id.button_set_test);
		buttonSetTest.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//测试黑标设置
				if (PrinterAPI.getInstance().isConnect())
				 PrinterAPI.getInstance().pointTest();
			}
		});
		findViewById(R.id.button_cut).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//切纸
				if (PrinterAPI.getInstance().isConnect()){
					PrinterAPI.getInstance().cutPaper(66,0);}else{
                    Toast.makeText(MarkActivity.this, UIUtils.getString(R.string.no_connect),Toast
                            .LENGTH_LONG)
                            .show();

                }
			}
		});
		findViewById(R.id.button_study).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//自学习
				if (PrinterAPI.getInstance().isConnect())
					PrinterAPI.getInstance().sendOrder(new byte[]{0x1b,0x41,1});
			}
		});
		findViewById(R.id.btn_gs_1_set).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (PrinterAPI.getInstance().isConnect())
				{
					byte data[]=new byte[]{0x1d,0x28,0x46,0x04,0,1,0,0,0,0x0a};
                    String string = mGsOne.getText().toString();
                    int value=0;
                    if (string.length()<=0){

                        Toast.makeText(MarkActivity.this,"请输入值",Toast.LENGTH_LONG).show();
                        return;
                    }
                    try {
                        value=Integer.parseInt(string);
                        byte low= (byte) (value/4%256);
                        byte high= (byte) (value/4/256);
                        data[7]=low;
                        data[8]=high;
                        PrinterAPI.getInstance().sendOrder(data);

                    }catch (ClassCastException e){
                        Toast.makeText(MarkActivity.this,"请输入数字",Toast.LENGTH_LONG).show();
                    }


				}else{
                    Toast.makeText(MarkActivity.this, UIUtils.getString(R.string.no_connect),Toast
                            .LENGTH_LONG)
                            .show();

                }
			}
		});findViewById(R.id.btn_gs_2_set).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (PrinterAPI.getInstance().isConnect())
				{
					byte data[]=new byte[]{0x1d,0x28,0x46,0x04,0,2,0,0,0,0x0a};
                    String string = mGsTwo.getText().toString();
                    int value=0;
                    if (string.length()<=0){

                        Toast.makeText(MarkActivity.this,"请输入值",Toast.LENGTH_LONG).show();
                        return;
                    }
                    try {
                        value=Integer.parseInt(string);
                        byte low= (byte) (value/4%256);
                        byte high= (byte) (value/4/256);
                        data[7]=low;
                        data[8]=high;
                        PrinterAPI.getInstance().sendOrder(data);

                    }catch (ClassCastException e){
                        Toast.makeText(MarkActivity.this,"请输入数字",Toast.LENGTH_LONG).show();
                    }


				}else{
                    Toast.makeText(MarkActivity.this, UIUtils.getString(R.string.no_connect),Toast
                            .LENGTH_LONG)
                            .show();

                }
			}
		});
	}
}
