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

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
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.PackConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.PackConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class PackConfigActivity extends BaseScanActivity implements PackConfigConstact.View, OnClickListener {
	private Context context;
	private MyToast myToast;
	private PackConfigConstact.Presenter presenter;
	private TextView tv_title_common;
	private EditText et_package_label, et_pack_count, et_pack_config_describe;
	private View btn_package_label_scanner, btn_patch_config_batch, btn_patch_config_single,lin_package_label;
	private Button btn_title_more_option;
	private ViewGroup lin_pack_config_custom;

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

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (presenter != null) {
			presenter.onResume();
		}
	}
	@Override
	public int loadView() {
		init();
		return R.layout.activity_pack_config_template;
	}

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

	@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);
		btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
	}

	@Override
	public void initView() {
		et_package_label = (EditText) findViewById(R.id.et_package_label);
		btn_package_label_scanner = findViewById(R.id.btn_package_label_scanner);
		et_pack_count = (EditText) findViewById(R.id.et_pack_count);
		lin_pack_config_custom = (ViewGroup) findViewById(R.id.lin_pack_config_custom);
		et_pack_config_describe = (EditText) findViewById(R.id.et_pack_config_describe);
		btn_patch_config_batch = findViewById(R.id.btn_patch_config_batch);
		btn_patch_config_single = findViewById(R.id.btn_patch_config_single);
		lin_package_label = findViewById(R.id.lin_package_label);
	}

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

	@Override
	public void setListener() {
		btn_package_label_scanner.setOnClickListener(this);
		btn_patch_config_batch.setOnClickListener(this);
		btn_patch_config_single.setOnClickListener(this);
		btn_title_more_option.setOnClickListener(this);
	}

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

	}

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

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		if (intent != null) {
			startActivityForResult(intent, reqCode);
		}
	}

	@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);
	}

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

	@Override
	public void addCustomView(View view) {
		if (lin_pack_config_custom == null || view == null)
			return;
		lin_pack_config_custom.setVisibility(View.VISIBLE);
		lin_pack_config_custom.removeAllViews();
		lin_pack_config_custom.addView(view);
	}

	@Override
	public String getPackageLabel() {
		return et_package_label == null ? "" : et_package_label.getText().toString().trim();
	}

	@Override
	public void setPackageLabel(String packageLbelStr) {
		if (et_package_label == null || packageLbelStr == null)
			return;
		et_package_label.setText(packageLbelStr);

	}

	@Override
	public String getPreditCount() {
		return et_pack_count == null ? "0" : et_pack_count.getText().toString().trim();
	}

	@Override
	public void setPreditCount(String preditCountStr) {
		if (et_pack_count == null || preditCountStr == null)
			return;
		et_pack_count.setText(preditCountStr + "");
	}

	@Override
	public String getDescribe() {
		return et_pack_config_describe == null ? "" : et_pack_config_describe.getText().toString().trim();
	}

	@Override
	public void setDescribe(String describeStr) {
		if (et_pack_config_describe == null || describeStr == null)
			return;
		et_pack_config_describe.setText(describeStr + "");
	}
	
	@Override
	public void readQRCode() {
		PhoneScanner.Read(createService, cacheG, context);
	}

	@Override
	public void setSinglePackBtnVisiable(int visiable) {
		if (btn_patch_config_single != null) {
			btn_patch_config_single.setVisibility(visiable);
		}
		
	}

	@Override
	public void setBatchPackBtnVisiable(int visiable) {
		if (btn_patch_config_batch != null) {
			btn_patch_config_batch.setVisibility(visiable);
		}
	}

	@Override
	public void setMoreOptionBtnVisiable(int visibility) {
		if (btn_title_more_option == null) return;
		btn_title_more_option.setVisibility(visibility);
	}

	@Override
	public void setMoreOptionBtnStr(String moreStr) {
		if (btn_title_more_option == null || moreStr == null) return;
		btn_title_more_option.setText(moreStr);
	}
	
	@Override
	public void setPackageLabelVisibility(int visibility) {
		if(lin_package_label == null)return;
		lin_package_label.setVisibility(visibility);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_title_more_option:
			if (presenter == null)return;
			presenter.moreOptionClick();
			break;
		case R.id.image_title_back_common:
			if (presenter == null)return;
			presenter.backClick();
			break;
		case R.id.btn_package_label_scanner:
			if (presenter == null)return;
			presenter.btnScannerClick();
			break;
		case R.id.btn_patch_config_batch:
			if (presenter == null)return;
			presenter.batchPackClick();
			break;
		case R.id.btn_patch_config_single:
			if (presenter == null)return;
			presenter.singlePackClick();
			break;

		}
	}

}
