package cn.net.caoyao.cpzshandset.service.presenter.impl.sale.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.DealerInfoBean;
import cn.net.caoyao.cpzshandset.bean.SaleBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
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.DealerBll;
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.SearchByInputStyleActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.ChoiceAddressDialog;

public class LiqueurSaleConfigPresenter extends SaleConfigConstactImpl {
	private final SaleConfigConstact.View sView;
	private Context context;
	private ChoiceAddressDialog choiceAddressDialog;
	// private List<City> provinces = new ArrayList<>();
	private EditText et_warehouse_custom_num;
//	private CityBll cityBll;
	private DealerBll dealerBll;
	// private int salesArea = 0;
	private SalesBll bll;
	private boolean isCreate = true;
	private SaleBean currentSaleBean;
	private int salesId = -1;
	private DealerInfoBean currentDealerBean;
	private HandsetOperation option_type = HandsetOperation.OPERATION_SALE_CONSTANT;

	public LiqueurSaleConfigPresenter(SaleConfigConstact.View sView, Context context, Intent intent) {
		this.sView = sView;
		this.context = context;
		bll = new SalesBll(context);
//		cityBll = new CityBll(context);
		dealerBll = new DealerBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			salesId = intent.getIntExtra("currentHisId", -1);
			currentSaleBean = bll.querySalesById(salesId);
			currentDealerBean = dealerBll.queryDealerById(currentSaleBean.getDealer_id());
			LogUtil.i(TConfig.Echo, "currentSaleBean:" + currentSaleBean.toString());
		}
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		sView.setPageTitle("经销商出库配置");
		sView.setMoreOptionStr("完成");
		sView.setSaleLabelStr("经  销  商：");
		addCustomView();
		showDataView();

	}

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

	private void showDataView() {
		if (!isCreate) {
			String describe = (currentSaleBean.getDescribe() == null) ? "" : currentSaleBean.getDescribe();
			sView.setEtSaleDeclareStr(describe);
//			City mCity = cityBll.queryCityByCode(currentSaleBean.getSales_area());
//			String saleArea = (mCity == null ? "全国" : SaleAddressUtil.getAddressStrByCity(mCity));
//			sView.setSaleArea(saleArea);
			if(currentDealerBean != null){
				sView.setSaleArea(currentDealerBean.getName() +"");	
			}
			et_warehouse_custom_num.setText(currentSaleBean.getPlan_num()+"");
		}

	}

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


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (!checkViewAvailable())
			return;
		if (data != null) {
			currentDealerBean = (DealerInfoBean) data.getSerializableExtra("choicedDealer");
			sView.setSaleArea(currentDealerBean.getName() + "");
		}
		// String detailAddress = data.getStringExtra("addressDetail");
		// if (!StringUtils.isEmpty(detailAddress)) {
		// configView.setEndStationDetail(detailAddress);
		// addressBll.insertNativeTruckAddress(getAddressDetailBean(detailAddress));
		// }
	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
		if (currentDealerBean == null) {
			sView.showMsgToast("经销商信息不能为空", Toast.LENGTH_SHORT);
			return;
		}
		String plan_num_str = et_warehouse_custom_num.getText().toString().trim();
		if (StringUtils.isEmpty(plan_num_str)) {
			sView.showMsgToast("计划数量不能为空", Toast.LENGTH_SHORT);
			return;
		}

		int plan_num = Integer.parseInt(plan_num_str);
		if (plan_num <= 0 || plan_num > 2000) {
			sView.showMsgToast("计划数量必须大于0，并且小于2000", Toast.LENGTH_SHORT);
			return;
		}

		currentSaleBean = organizationSaleBean(plan_num);
		if (isCreate) {
			salesId = bll.insertSalesInfo(currentSaleBean);
			currentSaleBean.setId(salesId);
		} else {
			bll.updateSalesInfo(currentSaleBean);
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("currentSalesBean", currentSaleBean);
		context.startActivity(intent);
		sView.finishPage();

	}

	private SaleBean organizationSaleBean(int plan_num) {
		if (isCreate) {
			currentSaleBean = new SaleBean();
		}
		currentSaleBean.setDealer_id(currentDealerBean.getId());
		currentDealerBean.setSales_area(currentDealerBean.getSales_area());
		currentSaleBean.setDescribe(sView.getEtSaleDeclareStr());
		currentSaleBean.setPlan_num(plan_num);
		return currentSaleBean;

	}

	@Override
	public void choiceSaleAddressClick() {
		if (!checkViewAvailable())
			return;
		// showChoiceAddressDialog();
		Intent intent = new Intent(context, SearchByInputStyleActivity.class);
		intent.putExtra("optionMode", InternalOptionConstant.ADDRESS_MODE_CHOICE_DEALER);
		intent.putExtra("optionModeInOption", option_type);
		sView.startAcForResult(10220, intent);
	}

	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() {
//
//	}

}
