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

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
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.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.ProductBll;
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.ProductDetailListActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.ChoiceAddressDialog;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class LiqueurSFYSaleConfigPresenter extends SaleConfigConstactImpl {
	private final SaleConfigConstact.View sView;
	private Context context;
	private ChoiceAddressDialog choiceAddressDialog;
	// private List<City> provinces = new ArrayList<>();
	private EditText et_warehouse_custom_purchaser;//et_warehouse_custom_num
	private TextView tv_product_name;
	private CityBll cityBll;
	private ProductBll productBll;
//	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;
	private int choiceProductReqCode = 10001;
	private Product currentProduct;

	public LiqueurSFYSaleConfigPresenter(SaleConfigConstact.View sView, Context context, Intent intent) {
		this.sView = sView;
		this.context = context;
		bll = new SalesBll(context);
		cityBll = new CityBll(context);
		productBll = new ProductBll(context);
//		dealerBll = new DealerBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			salesId = intent.getIntExtra("currentHisId", -1);
			currentSaleBean = bll.querySalesById(salesId);
			LogUtil.e(TConfig.Echo, "currentSaleBean:" + currentSaleBean +" ");
//			currentDealerBean = dealerBll.queryDealerById(currentSaleBean.getDealer_id());
			salesArea = currentSaleBean.getSales_area();
			currentProduct = productBll.queryProductById(currentSaleBean.getProduct_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);
		View lin_custom_warehouse_plan_num = customView.findViewById(R.id.lin_custom_warehouse_plan_num);
		lin_custom_warehouse_plan_num.setVisibility(View.GONE);
//		et_warehouse_custom_num = (EditText) customView.findViewById(R.id.et_warehouse_custom_num);
		View lin_custom_warehouse_purchaser = customView.findViewById(R.id.lin_custom_warehouse_purchaser);
		lin_custom_warehouse_purchaser.setVisibility(View.VISIBLE);
//		TextView tv_warehouse_custom_num_label = (TextView) customView.findViewById(R.id.tv_warehouse_custom_num_label);
//		tv_warehouse_custom_num_label.setText("计划数量： ");
		et_warehouse_custom_purchaser = (EditText) customView.findViewById(R.id.et_warehouse_custom_purchaser);
		View lin_product_info = customView.findViewById(R.id.lin_product_info);
		lin_product_info.setVisibility(View.VISIBLE);
		tv_product_name = (TextView) customView.findViewById(R.id.tv_product_name);
		View btn_product_name_choice = customView.findViewById(R.id.btn_product_name_choice);
		btn_product_name_choice.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(!checkViewAvailable()) return;
				Intent intent = new Intent(context, ProductDetailListActivity.class);
				sView.startAcForResult(choiceProductReqCode, intent);
			}
		});
		
	}

	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);
//			if(currentDealerBean != null){
//				sView.setSaleArea(currentDealerBean.getName() +"");	
//			}
			salesArea = currentSaleBean.getSales_area();
//			et_warehouse_custom_num.setText(currentSaleBean.getPlan_num()+"");
			String purchaser = (currentSaleBean.getPurchaser() == null) ? "" : currentSaleBean.getPurchaser();
			et_warehouse_custom_purchaser.setText(purchaser);
			tv_product_name.setText(currentProduct.getName()+"");
		}

	}

	@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 (requestCode == choiceProductReqCode && resultCode == 1001) {
			if (data != null) {
				currentProduct = (Product) data.getSerializableExtra("choicedProduct");
				tv_product_name.setText(currentProduct.getName()+"");
			}
			
		}
	}

	@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;
//		}
		if (currentProduct == null) {
			sView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		String purchaser = et_warehouse_custom_purchaser.getText().toString().trim();
		if (StringUtils.isEmpty(purchaser)) {
			sView.showMsgToast("采购方信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		currentSaleBean = organizationSaleBean(purchaser);
		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(String purchaser) {
		if (isCreate) {
			currentSaleBean = new SaleBean();
		}
//		currentSaleBean.setDealer_id(currentDealerBean.getId());
//		currentDealerBean.setSales_area(currentDealerBean.getSales_area());
		currentSaleBean.setPurchaser(purchaser);
		currentSaleBean.setSales_area(salesArea);
		currentSaleBean.setDescribe(sView.getEtSaleDeclareStr());
		currentSaleBean.setProduct_id(currentProduct.getId());
//		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() {
//
//	}

}
