package cn.net.caoyao.cpzshandset.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.DeviceIdConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.DeviceIdConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class DeviceIdConfigActivity extends BaseScanActivity implements OnClickListener, DeviceIdConfigConstact.View {

	private DeviceIdConfigConstact.Presenter presenter;
	private Context context;
	private MyToast myToast;
	private TextView tv_current_device_id;
	private TextView tv_current_scanner_id;
	private TextView et_scanner_device_id;
	private Button btn_device_id_scanner;

	@Override
	protected Handler initHandler() {
		return presenter != null ? presenter.getHandler() : null;
	}

	@Override
	public int loadView() {
		init();
		return R.layout.activity_config_device_id;
	}

	private void init() {
		context = this;
		myToast = new MyToast(context);
		PhoneScanner.isDeviceId = true;		
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
		PhoneScanner.isDeviceId = false;	
	}

	@Override
	public void initTitleView() {
		View image_title_back_common = findViewById(R.id.image_title_back_common);
		image_title_back_common.setVisibility(View.VISIBLE);
		image_title_back_common.setOnClickListener(this);
		TextView tv_title_common = (TextView) findViewById(R.id.tv_title_common);
		tv_title_common.setText("配置设备ID");
		Button btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
		btn_title_more_option.setVisibility(View.VISIBLE);
		btn_title_more_option.setText("完成");
		btn_title_more_option.setOnClickListener(this);
	}

	@Override
	public void initView() {
		tv_current_device_id = (TextView) findViewById(R.id.tv_current_device_id);
		tv_current_scanner_id = (TextView) findViewById(R.id.tv_current_scanner_id);
		et_scanner_device_id = (TextView) findViewById(R.id.et_scanner_device_id);
		btn_device_id_scanner = (Button) findViewById(R.id.btn_device_id_scanner); 
	}

	@Override
	public void initPresenter() {
		presenter = createService.createDeviceIdConfigPresenter(cacheG, context, this, getIntent());
	}

	@Override
	public void setListener() {
		btn_device_id_scanner.setOnClickListener(this);
	}

	@Override
	public void initData() {
		et_scanner_device_id.setText("");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_title_back_common:
			finish();
			break;
		case R.id.btn_title_more_option:
			if (presenter != null) {
				presenter.completeClick();
			}
			break;
		case R.id.btn_device_id_scanner:
			if (presenter != null) {
				presenter.scannerClick();
			}
			break;
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		if (presenter == null) {
			this.presenter = presenter;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (presenter != null) {
			presenter.onActivityResult(requestCode, resultCode, data);
		}
	}
	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		if (reqCode < 0 || intent == null) return;
		startActivityForResult(intent, reqCode);
	}

	@Override
	public void showMsgToast(String msg, int duration) {
		if (StringUtils.isEmpty(msg) || duration < 0) return;
		myToast.show(msg, duration);
	}

	@Override
	public void setCurrentIdText(String currentText) {
		if (tv_current_device_id == null || currentText == null)return;
		tv_current_device_id.setText(currentText+"");
	}

	@Override
	public void setCurrentScanIdText(String currentScanText) {
		if (tv_current_scanner_id == null || currentScanText == null) return;
		Log.e("log0","scan"+currentScanText+"");
		tv_current_scanner_id.setText(currentScanText+"");
	}

	@Override
	public void setEtText(String etText) {
		if (et_scanner_device_id == null || etText == null) return;
		et_scanner_device_id.setText(etText+"");
	}

	@Override
	public String getCurrentIdText() {
		return tv_current_device_id != null ? tv_current_device_id.getText().toString().trim():"";
	}

	@Override
	public String getEtText() {
		return et_scanner_device_id != null ? et_scanner_device_id.getText().toString().trim():"";
	}

	@Override
	public void readQRCode() {
		PhoneScanner.Read(createService, cacheG, context);
	}

	@Override
	public void finishPage() {
		finish();
	}

	@Override
	public void vibrate(int duration) {
		vibrateReadQRComplete(duration);
		
	}

}
