package cn.net.caoyao.cpzshandset.service.presenter.impl.sale.commonfarmers;

import android.content.Context;
import android.content.Intent;
import cn.net.caoyao.cpzshandset.bean.City;
import cn.net.caoyao.cpzshandset.bean.SaleBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.bll.CityBll;
import cn.net.caoyao.cpzshandset.service.bll.SalesBll;
import cn.net.caoyao.cpzshandset.service.presenter.SaleConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.SaleConfigConstactImpl;
import cn.net.caoyao.cpzshandset.service.tools.SaleAddressUtil;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.ChoiceAddressDialog;

public class CommonFarmersSaleConfigPresenter extends SaleConfigConstactImpl{
	private final SaleConfigConstact.View sView;
	private Context context;
	private ChoiceAddressDialog choiceAddressDialog;
//	private List<City> provinces = new ArrayList<>();
	private CityBll cityBll;
	private int salesArea = 0;
	private SalesBll bll;
	private boolean isCreate = true;
	private SaleBean currentSaleBean;
	private int salesId = -1;
	private HandsetOperation option_type = HandsetOperation.OPERATION_SALE_CONSTANT;
	public CommonFarmersSaleConfigPresenter(SaleConfigConstact.View sView, Context context,Intent intent) {
		this.sView = sView;
		this.context = context;
		bll = new SalesBll(context);
		cityBll = new CityBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			salesId = intent.getIntExtra("currentHisId", -1);
			currentSaleBean = bll.querySalesById(salesId);
//			LogUtil.i(TConfig.Echo, "currentSaleBean:" + currentSaleBean.toString());
		}
		start();
	}
	@Override
	public void start() {
		if (!checkViewAvailable()) return;
		sView.setPageTitle("销售配置");
		sView.setMoreOptionStr("完成");
		showDataView();
		
	}
	private void showDataView() {
		if (!isCreate) {
			String describe = (currentSaleBean.getDescribe() == null) ? "" : currentSaleBean.getDescribe();
			sView.setEtSaleDeclareStr(describe);
			City mCity = cityBll.queryCityByCode(currentSaleBean.getSales_area());
			String saleAreaStr = (mCity == null ? "全国" : SaleAddressUtil.getAddressStrByCity(mCity));
			sView.setSaleArea(saleAreaStr);
			salesArea = currentSaleBean.getSales_area();
		}
		
	}
	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (sView != null && context != null) {
			b = true;
		}
		return b;
	}
	
	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable()) return;
		if (isCreate) {
			currentSaleBean = new SaleBean(salesArea,sView.getEtSaleDeclareStr());
			salesId = bll.insertSalesInfo(currentSaleBean);
			currentSaleBean.setId(salesId);
		} else {
			currentSaleBean.setSales_area(salesArea);
			currentSaleBean.setDescribe(sView.getEtSaleDeclareStr());
			bll.updateSalesInfo(currentSaleBean);
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("currentSalesBean", currentSaleBean);
		context.startActivity(intent);
		sView.finishPage();
		
	}
	@Override
	public void choiceSaleAddressClick() {
		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;
					sView.setSaleArea(SaleAddressUtil.getAddressStrByCity(city));
					salesArea = city.getCityCode();
				}
			});
			
		}
		choiceAddressDialog.show();
//		initAddressDialogData();
	}
//	private void initAddressDialogData() {
//		
//	}

}

