package com.printer.demo;

import android.content.Context;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.printer.sdk.PrinterInstance;
import com.printer.sdk.listener.OnPrintListener;
import com.printer.sdk.utils.Utils;
import com.printer.sdk.utils.XLog;

import java.io.UnsupportedEncodingException;

/**
 * 
 * @Description 处理图片打印相关
 */
public class CollectPrintActivity extends BaseActivity implements OnCheckedChangeListener {
	protected static final String TAG = null;
	private Context mContext;
	private LinearLayout header;
	private RadioGroup rg_is_notifacation, rg_is_service_setable;
	private RadioButton rbYes, reNo, rbAllow, rbNotAllow;
	private PrinterInstance myPrinter;
	private TextView tvCollectData;
	private boolean isOpen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_collect_print);
		init();
		XLog.d("yxz", "oncreate");
	}

	@Override
	protected void onResume() {
		super.onResume();
		XLog.d("yxz", "onresume");
		if (!isOpen)
			return;
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if (myPrinter.getSetable()) {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							rbAllow.setChecked(true);
							// rg_is_service_setable.check(R.id.rb_allow);
						}
					});
				} else {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							rbNotAllow.setChecked(true);
							// rg_is_service_setable.check(R.id.rb_not_allow);
						}
					});
				}
			}
		}).start();

	}

	private void init() {

		header = (LinearLayout) findViewById(R.id.ll_header_picture);
		rg_is_notifacation = (RadioGroup) findViewById(R.id.rg_is_notifacation);
		rbYes = (RadioButton) findViewById(R.id.yes);
		reNo = (RadioButton) findViewById(R.id.no);
		rg_is_notifacation.setOnCheckedChangeListener(this);
		rg_is_service_setable = (RadioGroup) findViewById(R.id.rg_is_service_setable);
		rbAllow = (RadioButton) findViewById(R.id.rb_allow);
		rbNotAllow = (RadioButton) findViewById(R.id.rb_not_allow);
		rg_is_service_setable.setOnCheckedChangeListener(this);
		tvCollectData = (TextView) findViewById(R.id.et_collect_data);
		// 设置文本可以滚动
		tvCollectData.setMovementMethod(ScrollingMovementMethod.getInstance());
		initHeader(header);
		// 打开服务远程端口
		open();
	}

	/**
	 * 初始化标题上的信息
	 */
	private void initHeader(LinearLayout header) {
		setHeaderLeftText(header, getString(R.string.back), new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();

			}
		});
		headerConnecedState.setText(getTitleState());
		setHeaderLeftImage(header, new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		setHeaderCenterText(header, getString(R.string.headview_CollectPrint));
		setTitleState("");
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if (group == rg_is_notifacation) {
			switch (checkedId) {
			case R.id.yes:
				collect(true);
				Toast.makeText(mContext, "等待数据上传后执行追加动作", Toast.LENGTH_SHORT).show();
				break;
			case R.id.no:
				collect(false);
				Toast.makeText(mContext, "追加并采集功能关闭", Toast.LENGTH_SHORT).show();
				break;
			}
		}
		if (group == rg_is_service_setable) {
			switch (checkedId) {
			case R.id.rb_allow:
				isServiceSetable(true);
				Toast.makeText(mContext, "打开服务端配置", Toast.LENGTH_SHORT).show();
				break;
			case R.id.rb_not_allow:
				isServiceSetable(false);
				Toast.makeText(mContext, "关闭服务端配置", Toast.LENGTH_SHORT).show();
				break;
			}
		}
	}

	private boolean open() {
		if (isOpen)
			return true;
		myPrinter = PrinterInstance.getPrinterInstance();
		return isOpen = myPrinter.openConnection();
	}

	public void isServiceSetable(boolean isSetable) {
		if (!isOpen) {
			if (isSetable)
				Toast.makeText(mContext, "开口失败，无法设置服务端为可配置", Toast.LENGTH_SHORT).show();
			else
				Toast.makeText(mContext, "开口失败，无法设置服务端为不可配置", Toast.LENGTH_SHORT).show();
			return;
		}
		myPrinter.isSetAble(isSetable);
	}

	public void collect(boolean isCollect) {
		if (!isOpen) {
			Toast.makeText(mContext, "开口失败，无法追加或采集", Toast.LENGTH_SHORT).show();
			return;
		}
		if (!isCollect) {
			myPrinter.setOnPrintListener(null);
			return;
		}
		myPrinter.setOnPrintListener(new OnPrintListener() {
			@Override
			public void doBeforePrint() {
				XLog.i(TAG, "客户端 打印开始前打印开头,发送广播给Service端。");
				try {
					int writeLen = myPrinter.sendBytesData("*************一单数据开始**************\r\n".getBytes("gbk"));
					XLog.i(TAG, "客户端 打印前发送的数据长度writeLen:" + writeLen);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				tvCollectData.setText("采集数据开始\r\n");
			}

			@Override
			public void doAfterPrint() {
				XLog.i(TAG, "客户端 打印结束后追加数据尾，发送广播给Service端。");
				try {
					int writeLen = myPrinter.sendBytesData("*************一单数据结束**************\r\n".getBytes("gbk"));
					XLog.i(TAG, "客户端 打印后发送的数据长度writeLen:" + writeLen);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				tvCollectData.append("\r\n采集数据结束");
			}

			@Override
			public void onReceiveParserData(int type, byte[] data) {
				if (data == null || data.length == 0) {
					XLog.i(TAG, "客户端 上传的采集数据为空");
					return;
				}
				if (type == 0) {
					XLog.i(TAG, "客户端 接收到USB口上传的采集数据长度：" + data.length);
				}
				if (type == 1) {
					XLog.i(TAG, "客户端 接收到NET口上传的采集数据" + data.length);
				}
				if (type == 2) {
					XLog.i(TAG, "客户端 接收到BT口上传的采集数据" + data.length);
				}
				tvCollectData.append(Utils.bytesToHexString(data, data.length));
			}
		});

	}

	// private boolean open() {
	// myPrinter = PrinterInstance.getPrinterInstance();
	// isOpen = myPrinter.openConnection();
	// if (!isOpen) {
	// Toast.makeText(mContext, "开口失败，不能演示监听打印操作", Toast.LENGTH_SHORT).show();
	// return false;
	// }
	// Toast.makeText(mContext, "开口成功，等待数据上传后执行追加动作",
	// Toast.LENGTH_SHORT).show();
	// myPrinter.setOnPrintListener(new OnPrintListener() {
	// @Override
	// public void doBeforePrint() {
	// XLog.i(TAG, "客户端 打印开始前打印开头,发送广播给Service端。");
	// try {
	// int writeLen =
	// myPrinter.sendBytesData("*************一单数据开始**************\r\n".getBytes("gbk"));
	// XLog.i(TAG, "客户端 打印前发送的数据长度writeLen:" + writeLen);
	// } catch (UnsupportedEncodingException e) {
	// e.printStackTrace();
	// }
	// tvCollectData.setText("采集数据开始\r\n");
	// }
	//
	// @Override
	// public void doAfterPrint() {
	// XLog.i(TAG, "客户端 打印结束后追加数据尾，发送广播给Service端。");
	// try {
	// int writeLen =
	// myPrinter.sendBytesData("*************一单数据结束**************\r\n".getBytes("gbk"));
	// XLog.i(TAG, "客户端 打印后发送的数据长度writeLen:" + writeLen);
	// } catch (UnsupportedEncodingException e) {
	// e.printStackTrace();
	// }
	// tvCollectData.append("\r\n采集数据结束");
	// }
	//
	// @Override
	// public void onReceiveParserData(int type, byte[] data) {
	// if (data == null || data.length == 0) {
	// XLog.i(TAG, "客户端 上传的采集数据为空");
	// return;
	// }
	// if (type == 0) {
	// XLog.i(TAG, "客户端 接收到USB口上传的采集数据长度："+data.length);
	// }
	// if (type == 1) {
	// XLog.i(TAG, "客户端 接收到NET口上传的采集数据"+data.length);
	// }
	// if (type == 2) {
	// XLog.i(TAG, "客户端 接收到BT口上传的采集数据"+data.length);
	// }
	// tvCollectData.append(Utils.bytesToHexString(data, data.length));
	// }
	// });
	//// myPrinter.isSetAble(true);
	// return true;
	// }
	private void close() {
		if (!isOpen)
			return;
		myPrinter.closeConnection();
		isOpen = false;
	}

	private void closeAndRelease() {
		close();
		myPrinter.setOnPrintListener(null);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		closeAndRelease();
	}

}
