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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.ActivateBean;
import cn.net.caoyao.cpzshandset.bean.PiCiBean;
import cn.net.caoyao.cpzshandset.bean.ProductListBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.ActivateBll;
import cn.net.caoyao.cpzshandset.service.presenter.ActivateConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.ActivateConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.activate.kia.KIADanActivateConfigPresenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.activate.kia.KIAPiActivateConfigPresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class ActivateConfigActivity extends BaseScanActivity implements ActivateConfigConstact.View, OnClickListener {
	private int choiceProductReqCode = 10001;
	private ActivateConfigConstact.Presenter presenter;
	private Context context;
	private TextView tv_title_common, tv_activate_config_template_p_id,tv_activate_config_pici_id, tv_activate_config_level,btn_activate_config_pici_choice,tv_activate_config_template_p_name,
			tv_activate_config_template_p_nid,tv_activate_config_create_time_content,tv_activate_config_product_id;
	private View btn_activate_config_template_choice,lin_activate_config_create_time,btn_activate_config_chose_time,btn_activate_config_batch_activate,btn_activate_config_with_package_relation_activate
	,tv_activate_config_product_name,tv_activate_config_product_tiaoma;
	private ViewGroup lin_activate_config_custom;
	private MyToast myToast;
	private Button btn_title_more_option,btn_activate_config_single_activate;
	public static  long id_pici=0;
	public static  long id_product=0;
	public static  String name_pici="";
	public static  String name_product="";
	public static  String productBatchType="";
	public static  long companyid=0;
	ActivateBean aBean;
	@Override
	protected void onResume() {
		super.onResume();
		if (presenter != null) {
			presenter.onResume();
		}
	}
	
	@Override
	protected void onDestroy() {
		if (myToast != null) {
			myToast.cancel();
		}
		id_pici=0;
		id_product=0;
		name_pici="";
		name_product="";
		productBatchType="";
		companyid=0;
		super.onDestroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (presenter != null) {
			presenter.onActivityResult(requestCode, resultCode, data);
		}
//		String productBatchType="";

		if (requestCode == choiceProductReqCode && resultCode == 1001) {
			if (data != null) {
				ProductListBean.DataBean choicedProduct = (ProductListBean.DataBean) data.getSerializableExtra("choicedProduct");
				tv_activate_config_template_p_id.setText(choicedProduct.getProductName());
				id_product=choicedProduct.getId();
				name_product=choicedProduct.getProductName();
				companyid=choicedProduct.getCompanyId();
				//刷新批次号
				tv_activate_config_pici_id.setText("");
				id_pici=0;
				name_pici="";
				productBatchType="";
				tv_activate_config_level.setText("");
			}
		}

		if (requestCode == choiceProductReqCode && resultCode == 1010) {
			if (data != null) {
				PiCiBean.DataBean choicedPiCi = (PiCiBean.DataBean) data.getSerializableExtra("choicedPiCi");
				tv_activate_config_pici_id.setText(choicedPiCi.getProductBatchCode()+"");
				id_pici=choicedPiCi.getId();
				name_pici=(choicedPiCi.getProductBatchCode())+"";
				productBatchType=choicedPiCi.getProductBatchType();
				tv_activate_config_level.setText(productBatchType+"");
				Log.i("我点击了","我拿到了回调111111===>"+choicedPiCi.getProductBatchCode()+"===>"+choicedPiCi.getId());
			}
		}
	}

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

	@Override
	public int loadView() {
		init();
		return R.layout.activity_activate_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);
		btn_title_more_option.setOnClickListener(this);
	}

	@Override
	public void initView() {
		tv_activate_config_template_p_id = (TextView) findViewById(R.id.tv_activate_config_template_p_id);
		tv_activate_config_pici_id = (TextView) findViewById(R.id.tv_activate_config_pici_id);
		tv_activate_config_level = (TextView) findViewById(R.id.tv_activate_config_level);
		btn_activate_config_pici_choice = (TextView) findViewById(R.id.btn_activate_config_pici_choice);
		btn_activate_config_template_choice = findViewById(R.id.btn_activate_config_template_choice);
		tv_activate_config_template_p_name = (TextView) findViewById(R.id.tv_activate_config_template_p_name);
		tv_activate_config_template_p_nid = (TextView) findViewById(R.id.tv_activate_config_template_p_nid);
		lin_activate_config_create_time = findViewById(R.id.lin_activate_config_create_time); //生产日期
		tv_activate_config_create_time_content = (TextView) findViewById(R.id.tv_activate_config_create_time_content);
		btn_activate_config_chose_time = findViewById(R.id.btn_activate_config_chose_time);
		lin_activate_config_custom = (ViewGroup) findViewById(R.id.lin_activate_config_custom);
		btn_activate_config_batch_activate = findViewById(R.id.btn_activate_config_batch_activate);
		btn_activate_config_single_activate = (Button) findViewById(R.id.btn_activate_config_single_activate);
		btn_activate_config_with_package_relation_activate = findViewById(R.id.btn_activate_config_with_package_relation_activate);
		
		tv_activate_config_product_name = findViewById(R.id.tv_activate_config_product_name);//用于隐藏产品名称
		tv_activate_config_product_tiaoma = findViewById(R.id.tv_activate_config_product_tiaoma);//用于隐藏商品条码
		tv_activate_config_product_id = (TextView) findViewById(R.id.tv_activate_config_product_id);//

	}

	@Override
	public void initPresenter() {
		int optionMode = getIntent().getIntExtra("optionMode", 0);
		switch (optionMode) {
		case 25:
		case 26:
			presenter = createService.createPackBottleBoxConfigPresenter(cacheG, context, this, getIntent());
			break;
		case 18:
			presenter = super.createService.createActivateWithRelationConfigPresenter(cacheG, context, this, getIntent());
			break;
		case 17:
			//激活打包
			presenter = super.createService.createActivateWithPackConfigPresenter(cacheG, context, this, getIntent());
			break;
			
		case 30:
			//单标激活
			presenter = new KIADanActivateConfigPresenter(context, this,getIntent());
			break;	
		case 31:
			//批量激活
			presenter =  new KIAPiActivateConfigPresenter(context, this,getIntent());
			break;		
		default:
			presenter = super.createService.createActivateConfigPresenter(cacheG, context, this, getIntent());
			break;
		}
		
	}

	@Override
	public void setListener() {
		btn_activate_config_template_choice.setOnClickListener(this);
		btn_activate_config_chose_time.setOnClickListener(this);
		btn_activate_config_batch_activate.setOnClickListener(this);
		btn_activate_config_single_activate.setOnClickListener(this);
		btn_activate_config_with_package_relation_activate.setOnClickListener(this);
		btn_activate_config_pici_choice.setOnClickListener(this);
	}

	@Override
	public void initData() {
		if ( btn_title_more_option.getVisibility()==View.VISIBLE){
//			//修改
			btn_activate_config_single_activate.setVisibility(View.GONE);
			btn_activate_config_batch_activate.setVisibility(View.GONE);
		}else {
//			//增加
			btn_activate_config_single_activate.setVisibility(View.VISIBLE);
			btn_activate_config_batch_activate.setVisibility(View.VISIBLE);
		}

	}

	@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(myToast == null || StringUtils.isEmpty(msg)) return;
		myToast.show(msg, duration);
	}

	@Override
	public void setPageTitle(String homeTitle) {
		if (tv_title_common == null || StringUtils.isEmpty(homeTitle)) return;
		tv_title_common.setText(homeTitle);

	}

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

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

	@Override
	public void setPId(String pidStr) {
		if (tv_activate_config_template_p_id == null || pidStr == null)
			return;
		tv_activate_config_template_p_id.setText(pidStr + "");
	}
	@Override
	public String getPICI_PId() {
		return tv_activate_config_pici_id == null ? ""
				: tv_activate_config_pici_id.getText().toString().trim();
	}

	@Override
	public void setPICI_PId(String pidStr) {
		if (tv_activate_config_pici_id == null || pidStr == null)
			return;
		tv_activate_config_pici_id.setText(pidStr + "");
	}

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

	@Override
	public void setLevel(String pidStr) {
		if (tv_activate_config_level == null || pidStr == null)
			return;
		tv_activate_config_level.setText(pidStr + "");
	}




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

	@Override
	public void setPName(String pNameStr) {
		if (tv_activate_config_template_p_name == null || StringUtils.isEmpty(pNameStr))
			return;
		tv_activate_config_template_p_name.setText(pNameStr + "");
	}

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

	@Override
	public void setPNid(String pNidStr) {
		if (tv_activate_config_template_p_nid == null || StringUtils.isEmpty(pNidStr))
			return;
		tv_activate_config_template_p_nid.setText(pNidStr+"");
	}

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

	@Override
	public void setProductDate(String productDate) {
		if(tv_activate_config_create_time_content == null || StringUtils.isEmpty(productDate)) return;
		tv_activate_config_create_time_content.setText(productDate+"");
	}

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

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

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

	@Override
	public TextView getDateTextView() {
		return tv_activate_config_create_time_content;
	}

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

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

	@Override
	public void setMoreOptionBtnVisiable(int visibility) {
		if (btn_title_more_option != null) {
			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 readQRCode() {
		PhoneScanner.Read(createService, cacheG, context);		
		
	}

	@Override
	public void setPackageRelationActivateBtnVisiable(int visibility) {
		if(btn_activate_config_with_package_relation_activate == null) return;
		btn_activate_config_with_package_relation_activate.setVisibility(visibility);
		
	}
	
	@Override
	public void setSingleActivateBtnStr(String singleBtnStr) {
		if (btn_activate_config_single_activate == null || singleBtnStr == null) return;
		btn_activate_config_single_activate.setText(singleBtnStr+"");
		
	}
	private TipsDialog tipsDialog;
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_title_more_option:
			if (name_pici.equals("") || name_product.equals("")){
				Toast.makeText(ActivateConfigActivity.this,"请填写完整的激活配置信息！",Toast.LENGTH_LONG).show();
			}else {
				if (presenter != null) {
					presenter.moreOptionClick();
				}
			}

			break;
		case R.id.image_title_back_common:
			// 返回按钮
			if (presenter != null) {
				presenter.backClick();
			}
			break;
		case R.id.btn_activate_config_template_choice:
			// 选择产品
			if (presenter != null) {
				presenter.choiceProductClick();
			}
			break;
		case R.id.btn_activate_config_chose_time:
			//选择生产日期
			if (presenter != null) {
				presenter.choiceProductDateClick();
			}
			break;
		case R.id.btn_activate_config_batch_activate:
			//批量激活
//			if (presenter != null) {
//				presenter.batchActivateClick();
//			}
			if (name_pici.equals("") || name_product.equals("")){
				Toast.makeText(ActivateConfigActivity.this,"请填写完整的激活配置信息！",Toast.LENGTH_LONG).show();
			}else {
				ActivateBll activateBll = new ActivateBll(context);
				Intent intent = new Intent(context, BatchLabelScannerActivity.class);
				intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT);
				organizeActivateBean(Config.BATCH_OPTION_LABEL_TYPE);
				if ( btn_title_more_option.getVisibility()==View.VISIBLE){
					//修改
					activateBll.updateActivateById(aBean);
				}else {
					//增加
					int activateId = activateBll.insertActivate(aBean);
					aBean.setId(activateId);;
				}
				id_pici=0;
				id_product=0;
				name_pici="";
				name_product="";
				productBatchType="";
				companyid=0;
				intent.putExtra("currentActivateBean", aBean);
				startActivity(intent);


				finish();
			}
			break;
		case R.id.btn_activate_config_single_activate:

			if (name_pici.equals("") || name_product.equals("")){
				Toast.makeText(ActivateConfigActivity.this,"请填写完整的激活配置信息！",Toast.LENGTH_LONG).show();
			}else {
				//单标激活
				ActivateBll activateBll = new ActivateBll(context);
				Intent intent = new Intent(context, SingleLabelScannerActivity.class);
				intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT);
				organizeActivateBean(Config.SINGLE_OPTION_LABEL_TYPE);
				if ( btn_title_more_option.getVisibility()==View.VISIBLE){
					//修改
					activateBll.updateActivateById(aBean);
				}else {
					//增加
					int activateId = activateBll.insertActivate(aBean);
					aBean.setId(activateId);;
				}
				id_pici=0;
				id_product=0;
				name_pici="";
				name_product="";
				productBatchType="";
				companyid=0;
				intent.putExtra("currentActivateBean", aBean);
				startActivity(intent);
				finish();



			}
			break;
		case R.id.btn_activate_config_with_package_relation_activate:
			//套标激活
			if (presenter != null) {
				presenter.packageRelationActivateClick();
			}
			break;
			case R.id.btn_activate_config_pici_choice:
				if (id_product==0){
					Toast.makeText(ActivateConfigActivity.this,"请先选择产品名称！",Toast.LENGTH_LONG).show();
				}else {
					//选择批次
					Intent intent2 = new Intent(context, PiCiListActivity.class);
					intent2.putExtra("productId",id_product);
					startAcForResult(choiceProductReqCode, intent2);
				}

				break;
		}
	}
	private void organizeActivateBean(int option_mode) {
		if (aBean == null) {
			aBean = new ActivateBean();
		}
		aBean.setProduct_id(id_product+"");
		aBean.setOption_mode(option_mode);
		aBean.setProduct_date(productBatchType);
		aBean.setProduct_batch_number(id_pici+"");
		aBean.setProducer(name_product);
		aBean.setProduct_batch_name(name_pici);
		aBean.setCompany_id(companyid);
	}
	@Override
	public void vibrate(int duration) {
		vibrateReadQRComplete(duration);
		
	}

	@Override
	public void setProductNameVisiable(int visibility) {
		// TODO Auto-generated method stub
		if(tv_activate_config_product_name == null) return;
		tv_activate_config_product_name.setVisibility(visibility);
	}

	@Override
	public void setProductCodeVisiable(int visibility) {
		// TODO Auto-generated method stub
		if(tv_activate_config_product_tiaoma == null) return;
		tv_activate_config_product_tiaoma.setVisibility(visibility);
	}

	@Override
	public void setEmplateChoiceBtnStr(String moreStr) {
		// TODO Auto-generated method stub
		if(btn_activate_config_template_choice == null || moreStr == null) return;
		((TextView) btn_activate_config_template_choice).setText(moreStr);
	}

	@Override
	public void setSetProductIdStr(String Str) {
		if(tv_activate_config_product_id == null || Str == null) return;
		((TextView) tv_activate_config_product_id).setText(Str);
		
	}



}
