package cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.truck.yth;

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.widget.Toast;
import cn.net.caoyao.cpzshandset.bean.StationManagerBean;
import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.StationManagerBll;
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.ListStationDesAdapter;

public class YthChoiceTruckEndStationPresenter extends ChoiceAddressListConstactImpl{
	private final ChoiceAddressListConstact.View cView;
	private Context context;
	private List<StationManagerBean> allStations = new ArrayList<>();
	private List<StationManagerBean> searchStations = new ArrayList<>();
	private StationManagerBll stationBll;
	private ListStationDesAdapter allAdapter;
	private ListStationDesAdapter searchAdapter;
	private TextWatcher t;
	
	public YthChoiceTruckEndStationPresenter(ChoiceAddressListConstact.View cView,Context context,Intent intent) {
		this.cView = cView;
		this.context = context;
		stationBll = new StationManagerBll(context);
		start();
	}
	@Override
	public void start() {
		if(!checkViewAvailable()) return;
		cView.setPageTitleStr("选择到达站");
		searchAdapter = new ListStationDesAdapter(searchStations,false,false);
		cView.setAddressSearchAdapter(searchAdapter);
		allStations = stationBll.queryAllTruckStations();
		allAdapter = new ListStationDesAdapter(allStations,true,false); 
		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.getEtText();
				if (StringUtils.isEmpty(conditionStr)) {
					cView.setAllAddressLvVisible();
				} else {
					searchAddressByCondition(conditionStr);
				}
			}
		};
		cView.addTextChangedListener(t);
	}

	protected void searchAddressByCondition(String conditionStr) {
		if (StringUtils.isEmpty(conditionStr)) {
			cView.showMsgToast("查询条件不能为空^_^", Toast.LENGTH_SHORT);
			return;
		}
		
		searchStations = stationBll.getTruckStationSearch(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_TRUCK_END_STATION_RESULT_CODE, intent);
		}
		
	}

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

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


}
