package cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.out.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.TextView;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.bean.WarehouseBean;
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.service.bll.ProductBll;
import cn.net.caoyao.cpzshandset.service.bll.UserLoginBll;
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.ui.activity.ProductDetailListActivity;
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.Product;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class LiqueurSFYOutWarehouseConfigPresenter extends WarehouseConfigConstactImpl {
	private final WarehouseConfigConstact.View wView;
	private Context context;
	private ChoiceAddressDialog choiceAddressDialog;
	// private List<City> provinces = new ArrayList<>();
//	private EditText et_warehouse_custom_num;
//	private CityBll cityBll;
	private TextView tv_product_name;
//	private DealerBll dealerBll;
	// private int salesArea = 0;
	private UserLoginBll loginBll;
	private WarehouseBll bll;
	private ProductBll productBll;
	private boolean isCreate = true;
	private WarehouseBean currentWarehouseBean;
	private int salesId = -1;
	private ResponsibleUserInfoBean currentSaleUserBean;
	private int choiceProductReqCode = 10001;
	private Product currentProduct;
	private HandsetOperation option_type = HandsetOperation.OPERATION_OUT_WAREHOUSE_CONSTANT;

	public LiqueurSFYOutWarehouseConfigPresenter(WarehouseConfigConstact.View wView, Context context, Intent intent) {
		this.wView = wView;
		this.context = context;
		bll = new WarehouseBll(context);
//		cityBll = new CityBll(context);
//		dealerBll = new DealerBll(context);
		loginBll = new UserLoginBll(context);
		productBll = new ProductBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			salesId = intent.getIntExtra("currentHisId", -1);
//			currentWarehouseBean = bll.querySalesById(salesId);
			currentWarehouseBean = bll.queryWarehouseById(option_type.getOption(), salesId);
//			currentSaleUserBean = dealerBll.queryDealerById(currentWarehouseBean.getDealer_id());
			currentSaleUserBean = loginBll.queryUserInfoByAccount(currentWarehouseBean.getTo_sale_user_account());
			currentProduct = productBll.queryProductById(currentWarehouseBean.getProduct_id());
			LogUtil.i(TConfig.Echo, "currentWarehouseBean:" + currentWarehouseBean.toString());
		}
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		wView.setPageTitle("出库配置");
		wView.setMoreOptionStr("完成");
		wView.setWarehouseLabelStr("销售人员：");
		wView.setTVWarehousDeclareStr("出库说明：");
		addCustomView();
		showDataView();

	}

	private void addCustomView() {
		View customView = LayoutInflater.from(context).inflate(R.layout.custom_warehouse_middle_style, null);
		wView.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_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);
				wView.startAcForResult(choiceProductReqCode, intent);
			}
		});
	}

	private void showDataView() {
		if (!isCreate) {
			String describe = (currentWarehouseBean.getDescribe() == null) ? "" : currentWarehouseBean.getDescribe();
			wView.setEtWarehousDeclareStr(describe);
//			City mCity = cityBll.queryCityByCode(currentWarehouseBean.getSales_area());
//			String saleArea = (mCity == null ? "全国" : SaleAddressUtil.getAddressStrByCity(mCity));
//			wView.setSaleArea(saleArea);
			if(currentSaleUserBean != null){
				wView.setSaleAreaStr(currentSaleUserBean.getUsername() +"");	
			}
//			et_warehouse_custom_num.setText(currentWarehouseBean.getPlan_num()+"");
			tv_product_name.setText(currentProduct.getName()+"");
		}

	}

	@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 (requestCode == choiceProductReqCode && resultCode == 1001) {
			if (data != null) {
				currentProduct = (Product) data.getSerializableExtra("choicedProduct");
				tv_product_name.setText(currentProduct.getName()+"");
			}
			
		} else if (requestCode == 10220) {
			if (data != null) {
				currentSaleUserBean = (ResponsibleUserInfoBean) data.getSerializableExtra("choicedDealer");
				wView.setSaleAreaStr(currentSaleUserBean.getUsername() + "");
			}
		}
		// String detailAddress = data.getStringExtra("addressDetail");
		// if (!StringUtils.isEmpty(detailAddress)) {
		// configView.setEndStationDetail(detailAddress);
		// addressBll.insertNativeTruckAddress(getAddressDetailBean(detailAddress));
		// }
	}

	@Override
	public void moreOptionClick() {
		if (!checkViewAvailable())
			return;
		if (currentSaleUserBean == 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;
//		}
		if (currentProduct == null) {
			wView.showMsgToast("产品信息不能为空！", Toast.LENGTH_SHORT);
			return;
		}
		currentWarehouseBean = organizationSaleBean();
		if (isCreate) {
//			salesId = bll.insertSalesInfo(currentWarehouseBean);
			salesId = bll.insertWarehouseInfo(currentWarehouseBean);
			currentWarehouseBean.setId(salesId);
		} else {
//			bll.updateSalesInfo(currentWarehouseBean);
			bll.updateWarehouseInfo(currentWarehouseBean);
		}
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", option_type);
		intent.putExtra("currentSalesBean", currentWarehouseBean);
		context.startActivity(intent);
		wView.finishPage();

	}

	private WarehouseBean organizationSaleBean() {
		if (isCreate) {
			currentWarehouseBean = new WarehouseBean();
		}
//		currentWarehouseBean.setDealer_id(currentSaleUserBean.getId());
//		currentSaleUserBean.setSales_area(currentSaleUserBean.getSales_area());
		currentWarehouseBean.setTo_sale_user_account(currentSaleUserBean.getAccount());
		currentWarehouseBean.setDescribe(wView.getEtWarehousDeclareStr());
		currentWarehouseBean.setOption_type(option_type.getOption());
		currentWarehouseBean.setProduct_id(currentProduct.getId());
//		currentWarehouseBean.setPlan_num(plan_num);
		return currentWarehouseBean;

	}


	@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);
		
	}



}
