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.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.TrainStationBll;
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.ListTextAdapter;
import cn.net.caoyao.cpzshandset.ui.adapter.SpinnerTextPopWindow;
import cn.net.caoyao.cpzshandset.ui.adapter.StationPresentAdapter;
import cn.net.cpzslibs.prot.handset.bean.res.TrainStationBean;
import cn.net.caoyao.cpzshandset.ui.adapter.ListTextAdapter.IOnItemSelectListener;

public class YthChoiceEndStationPresenter extends ChoiceAddressListConstactImpl{
	private final ChoiceAddressListConstact.View cView;
	private Context context;
	private List<TrainStationBean> allStations = new ArrayList<>();
	private List<TrainStationBean> searchStations = new ArrayList<>();
	private TrainStationBll stationBll;
	private StationPresentAdapter allAdapter;
	private StationPresentAdapter searchAdapter;
	private TextWatcher t;
	private List<String> provinceBriefs = new ArrayList<>();
	private SpinnerTextPopWindow mProvinceBriefPopWindow;
	private String currentProvinceCode;
	
	public YthChoiceEndStationPresenter(ChoiceAddressListConstact.View cView,Context context) {
		this.cView = cView;
		this.context = context;
		stationBll = new TrainStationBll(context);
		start();
	}
	@Override
	public void start() {
		if(!checkViewAvailable()) return;
		cView.setPageTitleStr("选择到达站");
		searchAdapter = new StationPresentAdapter(searchStations);
		cView.setAddressSearchAdapter(searchAdapter);
		allStations = stationBll.queryAllStationInfo();
		allAdapter = new StationPresentAdapter(allStations); 
		cView.setAllAddressLvAdapter(allAdapter);
		cView.setAllAddressLvVisible();
		initProvinceBriefSpinnerView();
		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) {
//		String currentProvinceCode = cView.getProvinceStr();
		if (StringUtils.isEmpty(conditionStr)) {
			cView.showMsgToast("查询条件不能为空^_^", Toast.LENGTH_SHORT);
			return;
		}
		
		searchStations = stationBll.searchTrainStationByCondition(currentProvinceCode, 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_STATION_NAME_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_TRAIN_STATION_NAME_RESULT_CODE, intent);
		}
		
	}

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

	
	
	/**
	 * 显示选择车皮号前字母的popWindow
	 */
	private void showProvinceStrSpinnerWindow() {
		mProvinceBriefPopWindow.setWidth(cView.getProvinceView().getWidth());
		mProvinceBriefPopWindow.showAsDropDown(cView.getProvinceTextView());
	}
	
	/**
	 * 初始化车皮号前面字母的Spinner
	 */
	private void initProvinceBriefSpinnerView() {
		provinceBriefs = stationBll.queryAllProvinceCode();
		cView.setProvinceStr(provinceBriefs.get(0)+"");
		currentProvinceCode = provinceBriefs.get(0);
		ListTextAdapter mAdapter = new ListTextAdapter(provinceBriefs);
		mAdapter.refreshData(provinceBriefs, 0);
		// 初始化PopWindow
		mProvinceBriefPopWindow = new SpinnerTextPopWindow(context);
		mProvinceBriefPopWindow.setAdatper(mAdapter);
		mProvinceBriefPopWindow.setItemListener(new IOnItemSelectListener() {
			@Override
			public void onItemClick(int pos) {
				if (!checkViewAvailable())
					return;
				currentProvinceCode = provinceBriefs.get(pos);
				cView.setProvinceStr(currentProvinceCode + "");
				allStations = stationBll.queryStationByProvinceCode(currentProvinceCode);
				allAdapter.refreshData(allStations);
				cView.setAllAddressLvVisible();
			}
		});

	}
	@Override
	public void choiceProviceClick() {
		showProvinceStrSpinnerWindow();		
	}

}
