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

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
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;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class LiqueurOutWarehouseConfigPresenter extends WarehouseConfigConstactImpl {

	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 et_warehouse_custom_num;
	private City currentCity;

	public LiqueurOutWarehouseConfigPresenter(WarehouseConfigConstact.View wView, Context context, Intent intent) {
		this.wView = wView;
		this.context = context;
		bll = new WarehouseBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		addCustomView();
		if (!isCreate) {
			warehouseId = intent.getIntExtra("currentHisId", -1);
			currentWarehouseBean = bll.queryWarehouseById(option_type.getOption(), warehouseId);
			LogUtil.i(TConfig.Echo, "currentWarehouseBean:" + currentWarehouseBean.toString());
			et_warehouse_custom_num.setText(currentWarehouseBean.getPlan_num()+"");
		}
		start();
	}

	private void addCustomView() {
		View customView = LayoutInflater.from(context).inflate(R.layout.custom_warehouse_middle_style, null);
		wView.addMiddleCustomView(customView);
		et_warehouse_custom_num = (EditText) customView.findViewById(R.id.et_warehouse_custom_num);
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;

		wView.setMoreOptionStr("完成");
		wView.setPageTitle("地域出库配置");
		wView.setTVWarehousDeclareStr("出库说明：");
		initDataView();
	}

	private void initDataView() {
		if (isCreate)
			return;
		wView.setEtWarehousDeclareStr(currentWarehouseBean.getDescribe());
		et_warehouse_custom_num.setText(currentWarehouseBean.getPlan_num()+"");
		if (cityBll == null) cityBll = new CityBll(context);
		currentCity = cityBll.queryCityByCode(currentWarehouseBean.getSale_area_code());
		if (currentCity != null) {
			wView.setSaleAreaStr(SaleAddressUtil.getAddressStrByCity(currentCity));
		} else {
			currentCity = new City("全国", 0);
			wView.setSaleAreaStr("全国");
		}
		currentChoiceCityCode = currentWarehouseBean.getSale_area_code();
	}

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


	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
		
		String plan_no = et_warehouse_custom_num.getText().toString().trim();
		if (currentCity == null) {
			wView.showMsgToast("销售区域不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		if (StrUtils.isEmpty(plan_no)) {
			wView.showMsgToast("计划数量不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		
		if (Integer.parseInt(plan_no) <= 0) {
			wView.showMsgToast("计划数量必须大于0！", Toast.LENGTH_SHORT);
			return;
		}
		
		String describe = StringUtils.isEmpty(wView.getEtWarehousDeclareStr()) ? "" : wView.getEtWarehousDeclareStr();
//		String describe = wView.getEtWarehousDeclareStr();
		if (isCreate) {
			currentWarehouseBean = new WarehouseBean(describe, option_type.getOption());
			currentWarehouseBean.setSale_area_code(currentChoiceCityCode);
			currentWarehouseBean.setPlan_num(Integer.parseInt(plan_no));
			int wId = bll.insertWarehouseInfo(currentWarehouseBean);
			currentWarehouseBean.setId(wId);
		} else {
			currentWarehouseBean.setDescribe(describe);
			currentWarehouseBean.setPlan_num(Integer.parseInt(plan_no));
			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() {
		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;
					currentCity = city;
					currentChoiceCityCode = currentCity.getCityCode();
					wView.setSaleAreaStr(SaleAddressUtil.getAddressStrByCity(currentCity));
				}
			});

		}
		choiceAddressDialog.show();
	}
}
