package cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.back.chinatex;
import android.view.View;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import cn.net.caoyao.cpzshandset.bean.WarehouseBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.WarehouseBll;
import cn.net.caoyao.cpzshandset.service.presenter.WarehouseConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.WarehouseConfigConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;

/**
 * 
 * @author 作者 李扬: 
 * @date 创建时间：2017-4-24 下午4:47:24 
 * @version 1.0   
 * @parameter 
 * @since  
 * @return 
 * 
 */
public class ChinatexBackWarehouseConfigPresenter extends
		WarehouseConfigConstactImpl {
	
	private WarehouseConfigConstact.View wView;
	private Context context;
	private HandsetOperation option_type = HandsetOperation.OPERATION_BACK_WAREHOUSE_CONSTANT;
	private WarehouseBean currentWarehouseBean;
	private WarehouseBll bll; // 创 建 数 据 库 的 操 作 信 息   
	private int warehouseId = -1; 
	private boolean isCreate = true; //是 不 是 新 创 建 的 单   
	private int currentChoiceCityCode = 0 ; // 当 前 选 择 城 市 代 码    
	
	
	public ChinatexBackWarehouseConfigPresenter(
			WarehouseConfigConstact.View wView, Context context, Intent intent) {
		this.wView = wView;
		this.context = context;
		bll = new WarehouseBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			warehouseId = intent.getIntExtra("currentHisId", -1);
			currentWarehouseBean = bll.queryWarehouseById(option_type.getOption(), warehouseId);
			LogUtil.i(TConfig.Echo, "currentWarehouseBean:" + currentWarehouseBean.toString());
		}		
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		wView.setSaleAreaVisiable(View.GONE);
		wView.setMoreOptionStr("完成");
		wView.setPageTitle("返库配置");
		wView.setTVWarehousDeclareStr("返库说明：");
		initDataView();
	}
	private void initDataView() {
		if (isCreate)
			return;
		wView.setEtWarehousDeclareStr(currentWarehouseBean.getDescribe());

	}
	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (wView != null && context != null) {
			b = true;
		}
		return b;
	}

	@Override
	public Handler getHandler() {
		
		return null;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		

	}

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

	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
		//如果没有信息写个无字，有就直接拿
		String describe = StringUtils.isEmpty(wView.getEtWarehousDeclareStr()) ? "无" : wView.getEtWarehousDeclareStr();
		if (isCreate) {
			currentWarehouseBean = new WarehouseBean(describe, option_type.getOption());
			currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
			int wId = bll.insertWarehouseInfo(currentWarehouseBean);//是新建的创建本单到数据库 并返回订单id
			currentWarehouseBean.setId(wId);
		} else {
			currentWarehouseBean.setDescribe(describe);
			currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
			bll.updateWarehouseInfo(currentWarehouseBean);
		}

		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("currentWarehouseBean", currentWarehouseBean);
		context.startActivity(intent);
		wView.finishPage();
	}

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

	}

}
