package cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.in.commonEgg;

import android.content.Context;
import android.content.Intent;
import android.view.View;
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.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;

public class CommonEggInWarehouseConfigPresenter extends WarehouseConfigConstactImpl{

	private final WarehouseConfigConstact.View wView;
	private Context context;
	private HandsetOperation option_type = HandsetOperation.OPERATION_IN_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;

	public CommonEggInWarehouseConfigPresenter(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());
//		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 void moreOptionClick() {
		if (!checkViewAvailable())
			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);
			int wId = bll.insertWarehouseInfo(currentWarehouseBean);
			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() {
		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();
	}
}
