package cn.net.caoyao.cpzshandset.service.presenter.impl.exchange.sheepEx;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import cn.net.caoyao.cpzshandset.bean.ExchangeCargoBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.service.bll.SheepDealerBll;
import cn.net.caoyao.cpzshandset.service.bll.ExchangeCargoBll;
import cn.net.caoyao.cpzshandset.service.presenter.ExchangeCargoConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.ExchangeCargoConfigConstactImpl;
import cn.net.caoyao.cpzshandset.ui.activity.BatchLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.activity.SingleLabelScannerActivity;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog;
import cn.net.caoyao.cpzshandset.ui.dialog.TipsDialog.DSure;
import cn.net.cpzslibs.prot.handset.bean.res.DealerBean;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class SheepExCargoConfigPresenter extends ExchangeCargoConfigConstactImpl{
	private final ExchangeCargoConfigConstact.View configView;
	private Context context;
	private boolean isCreate = true;
	private int cargoId = -1;
	private ExchangeCargoBean cBean;
	private ExchangeCargoBll cargoBll;
	private SheepDealerBll dealerBll;
	private DealerBean fromDealer;
	private DealerBean toDealer;
	private TextWatcher fromT;
	private TextWatcher toT;
	private TipsDialog tipsDialog;
	
	public SheepExCargoConfigPresenter(Context context,ExchangeCargoConfigConstact.View configView,Intent intent) {
		this.context = context;
		this.configView = configView;
		cargoBll = new ExchangeCargoBll(context);
		dealerBll = new SheepDealerBll(context);
		isCreate = intent.getBooleanExtra("isCreate", true);
		if (!isCreate) {
			cargoId = intent.getIntExtra("currentHisId", -1);
			configView.setMoreOptionBtnVisiable(View.VISIBLE);
			configView.setMoreOptionBtnStr("保存");
		}
		start();
	}
	
	@Override
	public void start() {
		if(!checkViewAvailable()) return;
		configView.setPageTitle("调货配置");
		initViewConfig();
		initDataView();
	}
	
	private void initViewConfig() {
		fromT = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(!checkViewAvailable()) return;
				fromDealer = dealerBll.queryDealerByCode(configView.getFromDealerCode());
//				LogUtil.d(TConfig.Echo, "from:" + fromDealer+"  FromDealerCode:" + configView.getFromDealerCode());
				if (fromDealer == null) {
					configView.setFromDealerName("");
				} else {
					configView.setFromDealerName(fromDealer.getName());
				}
			}
		};
		toT = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(!checkViewAvailable()) return;
				toDealer = dealerBll.queryDealerByCode(configView.getToDealerCode());
				if (toDealer == null) {
					configView.setToDealerName("");
				} else {
					configView.setToDealerName(toDealer.getName());
				}
			}
		};
		
		configView.addFromDealerTextChangedListener(fromT);
		configView.addToDealerTextChangedListener(toT);
		
	}

	private void initDataView() {
		
		cBean = cargoBll.queryExchangeCargoById(cargoId);
		if (cBean == null) return;
//		LogUtil.d(TConfig.Echo, "aBean:"+aBean.toString());
		if (cBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
			configView.setBatchBtnVisiable(View.GONE);
		} else if (cBean.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
			configView.setSingleBtnVisiable(View.GONE);
		}
		fromDealer = dealerBll.queryDealerById(cBean.getFrom_dealer_id());
		configView.removeFromDealerTextChangedListener(fromT);
		configView.setFromDealerCode(fromDealer.getCode());
		configView.setFromDealerName(fromDealer.getName());
		configView.addFromDealerTextChangedListener(fromT);
		toDealer = dealerBll.queryDealerById(cBean.getTo_dealer_id());
		configView.removeToDealerTextChangedListener(toT);
		configView.setToDealerCode(toDealer.getCode());
		configView.setToDealerName(toDealer.getName());
		configView.addToDealerTextChangedListener(toT);
		
		configView.settCargoCount(cBean.getExchange_count()+"");
}

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

	@Override
	public Handler getHandler() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}
	
	protected void showTipsDialog(String msg) {
		if (tipsDialog == null) {
			tipsDialog = new TipsDialog(context);
		}
		tipsDialog.show();
		tipsDialog.setTipsView("提示", msg, 0, false, new DSure() {
			
			@Override
			public void sureClick() {
				if(!checkViewAvailable()) return;
				if (cBean.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
					organizeCargoBean(Config.SINGLE_OPTION_LABEL_TYPE);
				} else if (cBean.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
					organizeCargoBean(Config.BATCH_OPTION_LABEL_TYPE);
				}
				cargoBll.updateExchangeCargoById(cBean);
				configView.finishPage();
			}
		});
		
	}

	@Override
	public void moreOptionClick() {
		if(!checkViewAvailable()) return;
		if (!checkValidCargo()) return;
		showTipsDialog("是否保存当前修改的信息？");
	}

	@Override
	public void backClick() {
		if(!checkViewAvailable()) return;
		configView.finishPage();
	}

	@Override
	public void singleClick() {
		if(!checkViewAvailable()) return;
		if (!checkValidCargo()) return;
		
		Intent intent = new Intent(context, SingleLabelScannerActivity.class);
		intent.putExtra("optionMode", HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT);
		organizeCargoBean(Config.SINGLE_OPTION_LABEL_TYPE);
		
		if (isCreate) {
			cargoId = cargoBll.insertExchangeCargo(cBean);
			cBean.setId(cargoId);
		} else {
			cargoBll.updateExchangeCargoById(cBean);
		}
		
		intent.putExtra("currentCargoBean", cBean);
		
		context.startActivity(intent);
		configView.finishPage();		
	}

	private void organizeCargoBean(int option_mode) {
		if (cBean == null) {
			cBean = new ExchangeCargoBean();
		}
		cBean.setOption_mode(option_mode);
		cBean.setFrom_dealer_id(fromDealer.getId());
		cBean.setTo_dealer_id(toDealer.getId());
		cBean.setTo_dealer_sale_area(toDealer.getArea_code());
		cBean.setExchange_count(Integer.parseInt(configView.getCargoCount()));
	}

	private boolean checkValidCargo() {
		if (fromDealer == null) {
			configView.showMsgToast("调货来源信息不能为空！", Toast.LENGTH_SHORT);
			return false;
		}
		if (toDealer == null) {
			configView.showMsgToast("调到商家信息不能为空！", Toast.LENGTH_SHORT);
			return false;
		}
		
		if (StrUtils.isEmpty(configView.getCargoCount())) {
			configView.showMsgToast("调货数量不能为空！", Toast.LENGTH_SHORT);
			return false;
		}
		
		if (fromDealer.getId() == toDealer.getId()) {
			configView.showMsgToast("调货来源和调到商家不能相同！", Toast.LENGTH_SHORT);
			return false;
		}
		if (Integer.parseInt(configView.getCargoCount()) <=0) {
			configView.showMsgToast("调货数量必须为大于0的数字", Toast.LENGTH_SHORT);
			return false;
		}
		if (Integer.parseInt(configView.getCargoCount()) > 2000) {
			configView.showMsgToast("每个调货单的扫码数据不能超过2000", Toast.LENGTH_SHORT);
			return false;
		}
		return true;
	}

	@Override
	public void batchClick() {
		if(!checkViewAvailable()) return;
		if (!checkValidCargo()) return;
		Intent intent = new Intent(context, BatchLabelScannerActivity.class);
//		intent.putExtra("optionMode", HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption());
		intent.putExtra("optionMode", HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT);
		organizeCargoBean(Config.BATCH_OPTION_LABEL_TYPE);
		
		if (isCreate) {
			cargoId = cargoBll.insertExchangeCargo(cBean);
			cBean.setId(cargoId);
		} else {
			cargoBll.updateExchangeCargoById(cBean);
		}
		
		
		intent.putExtra("currentCargoBean", cBean);
		context.startActivity(intent);
		configView.finishPage();	
	}

}
