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

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class InputQRCodeActivity extends BaseActivity implements OnClickListener {
	private EditText et_input_label;
	private int resultCode = 90001;
	private MyToast myToast;
	private Context context;

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}
	@Override
	public int loadView() {
		context = this;
		myToast = new MyToast(context);
		return R.layout.activity_input_qrcode;
	}

	@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("输入追溯码");
		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() {
		et_input_label = (EditText) findViewById(R.id.et_input_label);
	}

	@Override
	public void initPresenter() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@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:
			completeClick();
			break;
		}
	}
	private void showMsgToast(String msg,int duration){
		if(StringUtils.isEmpty(msg) || myToast == null) return;
		myToast.show(msg, duration);
	}
	private void completeClick() {
		String qrCode = et_input_label.getText().toString().trim();
		if (StringUtils.isEmpty(qrCode)) {
			showMsgToast("输入的追溯码不能为空", Toast.LENGTH_SHORT);
			return;
		}
		
		if (qrCode.length() != 16) {
			showMsgToast("输入的内容必须为16位数字", Toast.LENGTH_SHORT);
			return;
		}
		if (!StringUtils.isNumber(qrCode)) {
			showMsgToast("您所输入的不是中国追溯的合法标签", Toast.LENGTH_SHORT);
			return;
		}
		Intent intent = new Intent();
		intent.putExtra("InputQRCodeStr", qrCode);
		setResult(resultCode, intent);
		finish();
	}

}
