package cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.out.chinatex;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.City;
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.CityBll;
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.service.tools.SaleAddressUtil;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.ChoiceAddressDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;

/**
 * @author 作者 李扬:
 * @date 创建时间：2017-5-2 下午4:05:25
 * @version 1.0
 * @parameter
 * @since
 * @return
 */
public class ChinatexOutWarehouseConfigPresenter extends
		WarehouseConfigConstactImpl implements OnClickListener {
	private final WarehouseConfigConstact.View wView;
	private Context context;
	private HandsetOperation option_type = HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT;
	private WarehouseBll bll;
	private WarehouseBean currentWarehouseBean;
	private boolean isCreate = true;
	private int warehouseId = -1;
	private TipsDialog tipsDialog;
	private ChoiceAddressDialog choiceAddressDialog;
	private int currentChoiceCityCode = 0;
	private CityBll cityBll;
	private EditText custom_middle_edit;

	public ChinatexOutWarehouseConfigPresenter(
			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());
		}
		View bottomCustomView = LayoutInflater.from(context).inflate(
				R.layout.custom_bottom_btn_style, null);
		initBottomCustomView(bottomCustomView);
		wView.addBottomCustomView(bottomCustomView);

		View middleCustomView = LayoutInflater.from(context).inflate(
				R.layout.custom_middle_btn_style, null);
		initMiddleCustomView(middleCustomView);
		wView.addMiddleCustomView(middleCustomView);

		start();
	}

	private void initBottomCustomView(View bottomCustomView) {
		View btn_out_warehouse_custom_single_label = bottomCustomView
				.findViewById(R.id.btn_out_warehouse_custom_single_label);
		View btn_out_warehouse_custom_tray_label = bottomCustomView
				.findViewById(R.id.btn_out_warehouse_custom_tray_label);
		btn_out_warehouse_custom_single_label.setOnClickListener(this);
		btn_out_warehouse_custom_tray_label.setOnClickListener(this);
	}

	private void initMiddleCustomView(View middleCustomView) {
		custom_middle_edit = (EditText) middleCustomView
				.findViewById(R.id.custom_middle_edit);
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		// wView.setMoreOptionStr("完成");
		wView.setMoreOptionVisiable(View.GONE);
		wView.setPageTitle("出库配置");
		wView.setTVWarehousDeclareStr("出库说明：");
		initDataView();

	}

	private void initDataView() {
		if (isCreate)
			return;
		wView.setEtWarehousDeclareStr(currentWarehouseBean.getDescribe());
		if (cityBll == null)
			cityBll = new CityBll(context);
		City mCity = cityBll.queryCityByCode(currentWarehouseBean
				.getSale_area_code());
		if (mCity != null) {
			wView.setSaleAreaStr(SaleAddressUtil.getAddressStrByCity(mCity));
		} else {
			wView.setSaleAreaStr("全国");
		}

	}

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

	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub
		return null;
	}

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

	}

	@Override
	public void onResume() {

	}

	@Override
	public void moreOptionClick() {

	}

	@Override
	public void choiceSaleAreaClick() {
		if (!checkViewAvailable())
			return;
		showChoiceAddressDialog();
	}

	private void showChoiceAddressDialog() {
		if (choiceAddressDialog == null) {
			choiceAddressDialog = new ChoiceAddressDialog(context,
					new ChoiceAddressDialog.ChoiceCity() {
						@Override
						public void setCity(City city) {
							if (!checkViewAvailable())
								return;
							currentChoiceCityCode = city.getCityCode();
							wView.setSaleAreaStr(SaleAddressUtil
									.getAddressStrByCity(city));
						}
					});

		}
		choiceAddressDialog.show();
	}

	@Override
	public void onClick(View v) {

		// 如果没有信息写个无字，有就直接拿
		String describe = StringUtils.isEmpty(wView.getEtWarehousDeclareStr()) ? "无"
				: wView.getEtWarehousDeclareStr();
		String strPlanNum = custom_middle_edit.getText().toString().trim();
		if("".equals(strPlanNum)){
			wView.showMsgToast("请填写计划箱数", 1);
			return;
		}
		if(Integer.parseInt(strPlanNum)<=0){
			wView.showMsgToast("请输入大于0的整数", 1);
			return;
		}
		switch (v.getId()) {

		case R.id.btn_out_warehouse_custom_single_label:
			// 出库
			if (!checkViewAvailable())
				return;
			if (isCreate) {
				currentWarehouseBean = new WarehouseBean(describe,
						option_type.getOption());
				currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
				currentWarehouseBean.setPlanNumber(strPlanNum);
				currentWarehouseBean.setOutWarehouseType(2);// 
				int wId = bll.insertWarehouseInfo(currentWarehouseBean);// 是新建的创建本单到数据库
				currentWarehouseBean.setId(wId);
			} else {
				currentWarehouseBean.setDescribe(describe);
				currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
				bll.updateWarehouseInfo(currentWarehouseBean);// 修改出库信息
			}
			break;
		case R.id.btn_out_warehouse_custom_tray_label:
			if (!checkViewAvailable())
				return;
			// 托盘出库
			if (isCreate) {
				currentWarehouseBean = new WarehouseBean(describe,
						option_type.getOption());
				currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
				currentWarehouseBean.setPlanNumber(strPlanNum);
				currentWarehouseBean.setOutWarehouseType(1);
				int wId = bll.insertWarehouseInfo(currentWarehouseBean);// 是新建的创建本单到数据库
				currentWarehouseBean.setId(wId);
			} else {
				currentWarehouseBean.setDescribe(describe);
				currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
				bll.updateWarehouseInfo(currentWarehouseBean);// 修改出库信息
			}
			break;
		}
		
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("currentWarehouseBean", currentWarehouseBean);
		context.startActivity(intent);
		wView.finishPage();
	}

}
