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

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.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.TruckConsigneeBll;
import cn.net.caoyao.cpzshandset.service.presenter.SearchInputStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.base.impl.SearchInputStyleConstactImpl;
import cn.net.caoyao.cpzshandset.ui.adapter.ListSingleTextAdapter;

public class YthLoginInputConsigneePresenter extends SearchInputStyleConstactImpl{
	private final SearchInputStyleConstact.View sView;
	private Context context;
	private TruckConsigneeBll consigneeBll;
	private List<String> allConsignee = new ArrayList<>();
	private List<String> searchConsignee = new ArrayList<>();
	private TextWatcher t;
	private ListSingleTextAdapter allAdapter;
	private ListSingleTextAdapter searchAdapter;
	public YthLoginInputConsigneePresenter(SearchInputStyleConstact.View sView,Context context) {
		this.sView = sView;
		this.context = context;
		consigneeBll = new TruckConsigneeBll(context);
		start();
	}

	@Override
	public void start() {
		if(!checkViewAvailable()) return;
		allConsignee = consigneeBll.queryAllTruckConsigneeName();
//		consigneeBll.queryAllTruckConsignee();
		initEtRealated();
		allAdapter = new ListSingleTextAdapter(allConsignee);
		searchAdapter = new ListSingleTextAdapter(searchConsignee);
		sView.setDataSearchAdapter(searchAdapter);
		sView.setAllDataLvAdapter(allAdapter);
		sView.setAllDataLvVisible();
		
	}

	private void initEtRealated() {
t = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if(!checkViewAvailable()) return;
				String condition = sView.getEtText();
				if (StringUtils.isEmpty(condition)) {
					sView.setAllDataLvVisible();
				} else {
					searchConsignee = consigneeBll.searchConsigneeNameByCondition(condition);
					searchAdapter.refreshData(searchConsignee);
//					LogUtil.e(TConfig.Echo, "consignee:" + searchConsignee.toString());
					sView.setSearchDataLvVisible();
				}
			}
		};
		sView.addTextChangedListener(t);
		sView.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				completeClick();
				return true;
			}
		});		
	}

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

	@Override
	public void onAllLvItemClickListener(int position) {
		if(!checkViewAvailable()) return;
		
		InputMethodManager imm = (InputMethodManager)sView.getEditText().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.hideSoftInputFromWindow(sView.getEditText().getWindowToken(), 0);  
		String consigneeName = allConsignee.get(position);
		Intent intent = new Intent();
		intent.putExtra("consigneeName", consigneeName);
		sView.finishWithResult(InternalOptionConstant.CHOICE_TRUCK_CONSIGNEE_RESULT_CODE, intent);
	}

	@Override
	public void onSearchLvItemClickListener(int position) {
		if(!checkViewAvailable()) return;		
		InputMethodManager imm = (InputMethodManager)sView.getEditText().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.hideSoftInputFromWindow(sView.getEditText().getWindowToken(), 0);  
		String consigneeName = searchConsignee.get(position);
		Intent intent = new Intent();
		intent.putExtra("consigneeName", consigneeName);
		sView.finishWithResult(InternalOptionConstant.CHOICE_TRUCK_CONSIGNEE_RESULT_CODE, intent);
	}


	@Override
	public void completeClick() {
		if(!checkViewAvailable()) return;
		InputMethodManager imm = (InputMethodManager)sView.getEditText().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.hideSoftInputFromWindow(sView.getEditText().getWindowToken(), 0);  
		String consigneeName = sView.getEtText();
		Intent intent = new Intent();
		intent.putExtra("consigneeName", consigneeName);
		sView.finishWithResult(InternalOptionConstant.CHOICE_TRUCK_CONSIGNEE_RESULT_CODE, intent);
	}

	@Override
	public void backClick() {
		if(!checkViewAvailable()) return;
		InputMethodManager imm = (InputMethodManager)sView.getEditText().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);  
        imm.hideSoftInputFromWindow(sView.getEditText().getWindowToken(), 0);  
		sView.finishPage();
	}

}
