package cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.dealerOut.common;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
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.SaleBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
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.DealerOutBll;
import cn.net.caoyao.cpzshandset.service.bll.SheepDealerBll;
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.BatchLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SearchByInputStyleActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.ChoiceAddressDialog;
import cn.net.cpzslibs.prot.handset.bean.res.DealerBean;

public class CommonDealerOutConfigPresenter extends WarehouseConfigConstactImpl implements OnClickListener {
	private final WarehouseConfigConstact.View wView;
	private Context context;
	private ChoiceAddressDialog choiceAddressDialog;
	// private List<City> provinces = new ArrayList<>();
	private EditText et_warehouse_custom_num;
	private Button btn_out_warehouse_custom_single_label,btn_out_warehouse_custom_tray_label;
//	private CityBll cityBll;
	private SheepDealerBll dealerBll;
	// private int salesArea = 0;
	private DealerOutBll bll;
	private boolean isCreate = true;
	private SaleBean currentSaleBean;
	private int salesId = -1;
	private DealerBean currentDealerBean;
	private HandsetOperation option_type = HandsetOperation.OPERATION_DEALER_OUT_CONSTANT;

	public CommonDealerOutConfigPresenter(WarehouseConfigConstact.View wView, Context context, Intent intent) {
		this.wView = wView;
		this.context = context;
		bll = new DealerOutBll(context);
//		cityBll = new CityBll(context);
		dealerBll = new SheepDealerBll(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;
		wView.setPageTitle("经销商出库配置");
//		wView.setMoreOptionStr("完成");
		wView.setWarehouseLabelStr("经  销  商：");
		wView.setTVWarehousDeclareStr("出库说明：");
		wView.setMoreOptionVisiable(View.GONE);
		addCustomView();
		addBottomCustomView();
		showDataView();
		
	}

	private void addBottomCustomView() {
		View bottomCustomView = LayoutInflater.from(context).inflate(
				R.layout.custom_bottom_btn_style, null);
		wView.addBottomCustomView(bottomCustomView);	
		btn_out_warehouse_custom_single_label = (Button) bottomCustomView
				.findViewById(R.id.btn_out_warehouse_custom_single_label);
		btn_out_warehouse_custom_tray_label = (Button) bottomCustomView
				.findViewById(R.id.btn_out_warehouse_custom_tray_label);
		btn_out_warehouse_custom_single_label.setText("单标出库");
		btn_out_warehouse_custom_tray_label.setText("批量出库");
		btn_out_warehouse_custom_single_label.setOnClickListener(this);
		btn_out_warehouse_custom_tray_label.setOnClickListener(this);
	}

	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);
		View lin_product_info = customView.findViewById(R.id.lin_product_info);
		lin_product_info.setVisibility(View.GONE);
	}

	private void showDataView() {
		if (!isCreate) {
			String describe = (currentSaleBean.getDescribe() == null) ? "" : currentSaleBean.getDescribe();
			wView.setEtWarehousDeclareStr(describe);
//			City mCity = cityBll.queryCityByCode(currentSaleBean.getSales_area());
//			String saleArea = (mCity == null ? "全国" : SaleAddressUtil.getAddressStrByCity(mCity));
//			wView.setSaleArea(saleArea);
			if(currentDealerBean != null){
				wView.setSaleAreaStr(currentDealerBean.getName() +"");	
			}
			et_warehouse_custom_num.setText(currentSaleBean.getPlan_num()+"");
			
			if (currentSaleBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
				btn_out_warehouse_custom_single_label.setVisibility(View.VISIBLE);
				btn_out_warehouse_custom_tray_label.setVisibility(View.GONE);
			} else {
				btn_out_warehouse_custom_single_label.setVisibility(View.GONE);
				btn_out_warehouse_custom_tray_label.setVisibility(View.VISIBLE);
			}
		} 

	}

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


	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (!checkViewAvailable())
			return;
		if (data != null) {
			currentDealerBean = (DealerBean) data.getSerializableExtra("choicedDealer");
			wView.setSaleAreaStr(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) {
			wView.showMsgToast("经销商信息不能为空", Toast.LENGTH_SHORT);
			return;
		}
		String plan_num_str = et_warehouse_custom_num.getText().toString().trim();
		if (StringUtils.isEmpty(plan_num_str)) {
			wView.showMsgToast("计划数量不能为空", Toast.LENGTH_SHORT);
			return;
		}

		int plan_num = Integer.parseInt(plan_num_str);
		if (plan_num <= 0 || plan_num > 2000) {
			wView.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);
		wView.finishPage();

	}

	private SaleBean organizationSaleBean(int plan_num) {
		if (isCreate) {
			currentSaleBean = new SaleBean();
		}
		currentSaleBean.setDealer_id(currentDealerBean.getId());
		currentSaleBean.setSales_area(currentDealerBean.getArea_code());
		currentSaleBean.setDescribe(wView.getEtWarehousDeclareStr());
		currentSaleBean.setPlan_num(plan_num);
		return currentSaleBean;

	}

	@Override
	public void choiceSaleAreaClick() {
		if (!checkViewAvailable())
			return;
		// showChoiceAddressDialog();
		Intent intent = new Intent(context, SearchByInputStyleActivity.class);
		intent.putExtra("optionMode", InternalOptionConstant.ADDRESS_MODE_CHOICE_DEALER);
		intent.putExtra("optionModeInOption", option_type);
		wView.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;
					wView.setSaleAreaStr(SaleAddressUtil.getAddressStrByCity(city));
					// salesArea = city.getCityCode();
				}
			});

		}
		choiceAddressDialog.show();
//		initAddressDialogData();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.btn_out_warehouse_custom_single_label:
			// 出库
			if (!checkViewAvailable())
				return;
			createDealerOutBean(Config.SINGLE_OPTION_LABEL_TYPE);
			if (currentSaleBean == null) {
				return;
			}
			Intent intent = new Intent(context, SingleLabelScannerActivity.class);
			intent.putExtra("optionMode", option_type);
			intent.putExtra("currentSalesBean", currentSaleBean);
			context.startActivity(intent);
			wView.finishPage();
			break;
		case R.id.btn_out_warehouse_custom_tray_label:
			if (!checkViewAvailable())
				return;
			createDealerOutBean(Config.BATCH_OPTION_LABEL_TYPE);
			if (currentSaleBean == null) {
				return;
			}
			Intent batchIntent = new Intent(context, BatchLabelScannerActivity.class);
			batchIntent.putExtra("optionMode", option_type);
			batchIntent.putExtra("currentSalesBean", currentSaleBean);
			context.startActivity(batchIntent);
			wView.finishPage();
			break;
		}		
	}

	private void createDealerOutBean(int option_mode) {
		if (currentDealerBean == null) {
			wView.showMsgToast("经销商信息不能为空", Toast.LENGTH_SHORT);
			return;
		}
		String plan_num_str = et_warehouse_custom_num.getText().toString().trim();
		if (StringUtils.isEmpty(plan_num_str)) {
			wView.showMsgToast("计划数量不能为空", Toast.LENGTH_SHORT);
			return;
		}

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

		currentSaleBean = organizationSaleBean(plan_num);
		currentSaleBean.setOption_mode(option_mode);
		if (isCreate) {
			salesId = bll.insertSalesInfo(currentSaleBean);
			currentSaleBean.setId(salesId);
		} else {
			bll.updateSalesInfo(currentSaleBean);
		}
		currentSaleBean = bll.querySalesById(currentSaleBean.getId());
	}

	
//	private void initAddressDialogData() {
//
//	}

}
