package cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.train;

import java.util.ArrayList;
import java.util.List;

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

import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.ConsigneeBll;
import cn.net.caoyao.cpzshandset.service.presenter.ChoiceAddressListConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.ChoiceAddressListConstactImpl;
import cn.net.caoyao.cpzshandset.ui.adapter.StationPresentAdapter;
import cn.net.cpzslibs.prot.handset.bean.ConsigneeBean;

public class YthChoiceTrainConsigeePresenter extends ChoiceAddressListConstactImpl{
	private final ChoiceAddressListConstact.View cView;
	private Context context;
	private List<ConsigneeBean> allStations = new ArrayList<>();
	private List<ConsigneeBean> searchStations = new ArrayList<>();
	private ConsigneeBll consigneeBll;
	private StationPresentAdapter allAdapter;
	private StationPresentAdapter  searchAdapter;
	private TextWatcher t;
	
	public YthChoiceTrainConsigeePresenter(ChoiceAddressListConstact.View cView,Context context,Intent intent) {
		this.cView = cView;
		this.context = context;
		consigneeBll = new ConsigneeBll(context);
		start();
	}
	@Override
	public void start() {
		if(!checkViewAvailable()) return;
		cView.setPageTitleStr("选择收货人");
		cView.setProvinceViewVisiable(View.GONE);
		cView.setEndStationEtVisiable(View.GONE);
		cView.setConsigeeEtVisiable(View.VISIBLE);
		searchAdapter = new StationPresentAdapter(searchStations);
		cView.setAddressSearchAdapter(searchAdapter);
		allStations = consigneeBll.queryAllConsigee();
//		LogUtil.d(TConfig.Echo, "stationName:" + stationName);
		allAdapter = new StationPresentAdapter(allStations); 
		cView.setAllAddressLvAdapter(allAdapter);
		cView.setAllAddressLvVisible();
		t = 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;
				String conditionStr = cView.getConsigeeEtText();
				if (StringUtils.isEmpty(conditionStr)) {
					cView.setAllAddressLvVisible();
				} else {
					searchAddressByCondition(conditionStr);
				}
			}
		};
		cView.addConsigeeTextChangedListener(t);
	}

	protected void searchAddressByCondition(String conditionStr) {
		if (StringUtils.isEmpty(conditionStr)) {
			cView.showMsgToast("查询条件不能为空^_^", Toast.LENGTH_SHORT);
			return;
		}
		
		searchStations = consigneeBll.searchConsigeeByCondition(conditionStr);
		searchAdapter.refreshData(searchStations);
		cView.setSearchAddressLvVisible();
		
	}
	@Override
	public boolean checkViewAvailable() {
		boolean b = false;
		if (cView != null && context != null) {
			b = true;
		}
		return b;
	}


	@Override
	public void onAddressLvItemClickListener(int position) {
		if(!checkViewAvailable()) return;
		if (allStations.size() > 0) {
			Intent intent = new Intent();
			intent.putExtra("choiceAddressData", allStations.get(position));
			cView.finishWithResult(InternalOptionConstant.CHOICE_TRAIN_END_STATION_RESULT_CODE, intent);
		}
		
	}

	@Override
	public void onSearchLvItemClickListener(int position) {
		if(!checkViewAvailable()) return;
		if (searchStations.size() > 0) {
			Intent intent = new Intent();
//			LogUtil.e(TConfig.Echo,position + "search:" + searchStations.toString());
			intent.putExtra("choiceAddressData", searchStations.get(position));
			cView.finishWithResult(InternalOptionConstant.CHOICE_TRAIN_END_STATION_RESULT_CODE, intent);
		}
		
	}

	@Override
	public void btnSearchClick() {
		if(!checkViewAvailable()) return;
		String conditionStr = cView.getConsigeeEtText();
		searchAddressByCondition(conditionStr);
	}

	

}
