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

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.QueryConstact;
import cn.net.caoyao.cpzshandset.service.presenter.QueryConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20019ResBean;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class QueryActivity extends BaseScanActivity implements OnClickListener,
		QueryConstact.View {
	private Context context;
	private MyToast myToast;
	private QueryConstact.Presenter presenter;
	private TextView tv_title_common;
	private LinearLayout query_layout_biao_qh, query_layout_chan_pmc,
			query_layout_feng_xs, query_layout_xiang_bqh,
			query_layout_shou_cxf, query_layout_sheng_crq, query_layout_ru_ksj,
			query_layout_chu_ksj, query_layout_bei_zxx, query_layout_xiao_sqy;
	// 标签号，产品名称，分销商，箱标签号,首次消费,生产日期，入库时间,出库时间,备注,销售区域.

	private Button query_button_cha_x; // 查询按钮.

	// 标签号，产品名称，分销商，箱标签号,首次消费,生产日期，入库时间,出库时间,备注,销售区域.
	private TextView query_textview_biao_qh, query_textview_chan_pmc,
			query_textview_feng_xs, query_textview_xiang_bqh,
			query_textview_shou_cxf, query_textview_sheng_crq,
			query_textview_ru_ksj, query_textview_chu_ksj,
			query_textview_bei_zxx, query_textview_xiao_sqy;

	@Override
	public int loadView() {

		init();
		return R.layout.activity_query;
	}

	private void init() {
		context = this;
		myToast = new MyToast(context);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (presenter != null) {
			presenter.onResume();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}

	@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);
		tv_title_common = (TextView) findViewById(R.id.tv_title_common);// 标题
	}

	@Override
	public void initView() {

		// 标签号，产品名称，分销商，箱标签号,首次消费,生产日期，入库时间,出库时间,备注,销售区域
		query_layout_biao_qh = (LinearLayout) findViewById(R.id.query_layout_biao_qh);
		query_layout_chan_pmc = (LinearLayout) findViewById(R.id.query_layout_chan_pmc);
		query_layout_feng_xs = (LinearLayout) findViewById(R.id.query_layout_feng_xs);
		query_layout_xiang_bqh = (LinearLayout) findViewById(R.id.query_layout_xiang_bqh);
		query_layout_shou_cxf = (LinearLayout) findViewById(R.id.query_layout_shou_cxf);
		query_layout_sheng_crq = (LinearLayout) findViewById(R.id.query_layout_sheng_crq);
		query_layout_ru_ksj = (LinearLayout) findViewById(R.id.query_layout_ru_ksj);
		query_layout_chu_ksj = (LinearLayout) findViewById(R.id.query_layout_chu_ksj);
		query_layout_bei_zxx = (LinearLayout) findViewById(R.id.query_layout_bei_zxx);
		query_layout_xiao_sqy = (LinearLayout) findViewById(R.id.query_layout_xiao_sqy);

		// 查询按钮
		query_button_cha_x = (Button) findViewById(R.id.query_button_cha_x);

		// 标签号，产品名称，分销商，箱标签号,首次消费,生产日期，入库时间,出库时间,备注,销售区域
		query_textview_biao_qh = (TextView) findViewById(R.id.query_textview_biao_qh);
		query_textview_chan_pmc = (TextView) findViewById(R.id.query_textview_chan_pmc);
		query_textview_feng_xs = (TextView) findViewById(R.id.query_textview_feng_xs);
		query_textview_xiang_bqh = (TextView) findViewById(R.id.query_textview_xiang_bqh);
		query_textview_shou_cxf = (TextView) findViewById(R.id.query_textview_shou_cxf);
		query_textview_sheng_crq = (TextView) findViewById(R.id.query_textview_sheng_crq);
		query_textview_ru_ksj = (TextView) findViewById(R.id.query_textview_ru_ksj);
		query_textview_chu_ksj = (TextView) findViewById(R.id.query_textview_chu_ksj);
		query_textview_bei_zxx = (TextView) findViewById(R.id.query_textview_bei_zxx);
		query_textview_xiao_sqy = (TextView) findViewById(R.id.query_textview_xiao_sqy);

	}

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

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

	@Override
	public void initData() {

	}

	@Override
	public void setPresenter(Presenter presenter) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		// TODO Auto-generated method stub

	}

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

	@Override
	public void setPageTitle(String homeTitle) {
		if (tv_title_common == null || homeTitle == null)
			return;
		tv_title_common.setText(homeTitle + "");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.net.caoyao.cpzshandset.service.presenter.QueryConstact.View#setScanData
	 * (cn.net.cpzslibs.prot.handset.bean.res.Prot20019ResBean)
	 */
	@Override
	public void setScanData(Prot20019ResBean resBean) {
		/*
		 * query_layout_biao_qh, query_layout_chan_pmc, query_layout_feng_xs,
		 * query_layout_xiang_bqh, query_layout_shou_cxf,
		 * query_layout_sheng_crq, query_layout_ru_ksj, query_layout_chu_ksj,
		 * query_layout_bei_zxx,query_layout_xiao_sqy;
		 */

		/*
		 * query_textview_biao_qh,query_textview_chan_pmc,
		 * query_textview_feng_xs, query_textview_xiang_bqh,
		 * query_textview_shou_cxf, query_textview_sheng_crq,
		 * query_textview_ru_ksj, query_textview_chu_ksj,
		 * query_textview_bei_zxx,query_textview_xiao_sqy;
		 */

		setGone();
		if (isEmpty(resBean.getLabel_id())) {// 标签号
			query_layout_biao_qh.setVisibility(View.VISIBLE);
			query_textview_biao_qh.setText(resBean.getLabel_id());
		}
		if (isEmpty(resBean.getProduct_name())) {// 产品名称
			query_layout_chan_pmc.setVisibility(View.VISIBLE);
			query_textview_chan_pmc.setText(resBean.getProduct_name());
		}
		if (isEmpty("")) {// 分销商
			query_layout_feng_xs.setVisibility(View.VISIBLE);
			query_textview_feng_xs.setText("");
		}
		if (isEmpty(resBean.getPackage_id())) {// 箱标签号
			query_layout_xiang_bqh.setVisibility(View.VISIBLE);
			query_textview_xiang_bqh.setText(resBean.getPackage_id());
		}
		if (isEmpty(resBean.getFirst_consume_time())) {// 首次消费
			query_layout_shou_cxf.setVisibility(View.VISIBLE);
			query_textview_shou_cxf.setText("");
		}
		if (isEmpty(resBean.getProduce_time())) {// 生产日期
			query_layout_sheng_crq.setVisibility(View.VISIBLE);
			query_textview_sheng_crq.setText(resBean.getProduce_time().substring(0,10));
		}
		if (isEmpty("")) {// 入库时间
			query_layout_ru_ksj.setVisibility(View.VISIBLE);
			query_textview_ru_ksj.setText("");
		}
		if (isEmpty(resBean.getPop_time())) {// 出库时间
			query_layout_chu_ksj.setVisibility(View.VISIBLE);
			query_textview_chu_ksj.setText(resBean.getPop_time().substring(0,10));
		}
		if (isEmpty("")) {// 备注信息
			query_layout_bei_zxx.setVisibility(View.VISIBLE);
			query_textview_bei_zxx.setText("");
		}
		if (isEmpty(resBean.getArea_name())) {// 销售区域
			query_layout_xiao_sqy.setVisibility(View.VISIBLE);
			query_textview_xiao_sqy.setText(resBean.getArea_name());
		}

	}

	@Override
	public void finishPage() {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.query_button_cha_x:
			if (presenter != null) {
				presenter.btnScannerClick();
			}
			break;
		case R.id.image_title_back_common:
			finish();

			break;

		default:
			break;
		}
	}

	/**
	 * 非空判断
	 * 
	 * @param str
	 * @return
	 */
	private boolean isEmpty(String str) {
		if (!"".equals(str) && null != str) {
			return true;
		}
		return false;
	}

	/**
	 * 先隐身，在输出。
	 */
	private void setGone() {
		/*
		 * query_layout_biao_qh, query_layout_chan_pmc, query_layout_feng_xs,
		 * query_layout_xiang_bqh, query_layout_shou_cxf,
		 * query_layout_sheng_crq, query_layout_ru_ksj, query_layout_chu_ksj,
		 * query_layout_bei_zxx,query_layout_xiao_sqy;
		 */

		query_layout_biao_qh.setVisibility(View.GONE);
		query_layout_chan_pmc.setVisibility(View.GONE);
		query_layout_feng_xs.setVisibility(View.GONE);
		query_layout_xiang_bqh.setVisibility(View.GONE);
		query_layout_shou_cxf.setVisibility(View.GONE);
		query_layout_sheng_crq.setVisibility(View.GONE);
		query_layout_ru_ksj.setVisibility(View.GONE);
		query_layout_chu_ksj.setVisibility(View.GONE);
		query_layout_bei_zxx.setVisibility(View.GONE);
		query_layout_xiao_sqy.setVisibility(View.GONE);

	}

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

	@Override
	protected Handler initHandler() {
		// TODO Auto-generated method stub
		return presenter != null? presenter.getHandler(): null;
	}

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