package cn.net.caoyao.cpzshandset.service.presenter.impl.sale.liqueur;

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.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import cn.net.caoyao.cpzshandset.bean.DealerInfoBean;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.bll.DealerBll;
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.DealerAdapter;

public class LiqueurChoiceDealerPresenter extends SearchInputStyleConstactImpl {
	private final SearchInputStyleConstact.View sView;
	private Context context;
//	private int cityCode = 0;
	private DealerBll dealerBll;
	private List<DealerInfoBean> allDealer = new ArrayList<>();
	private List<DealerInfoBean> searchDealer = new ArrayList<>();
	private TextWatcher t;
	private DealerAdapter allAdapter;
	private DealerAdapter searchAdapter;

	public LiqueurChoiceDealerPresenter(SearchInputStyleConstact.View sView, Context context, Intent intent) {
		this.sView = sView;
		this.context = context;
		dealerBll = new DealerBll(context);
		start();
	}

	@Override
	public void start() {
		if (!checkViewAvailable())
			return;
		sView.setEditTextHintStr("输入经销商名称或首字母");
		sView.setRightOptionStr("搜索");
		sView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		allDealer = dealerBll.queryAllDealer();
		initEtRealated();
		allAdapter = new DealerAdapter(allDealer);
		searchAdapter = new DealerAdapter(searchDealer);
		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;
				searchDealer();
			}
		};
		sView.addTextChangedListener(t);
		sView.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				completeClick();
				return true;
			}
		});
	}

	protected void searchDealer() {
		String condition = sView.getEtText();
		if (StringUtils.isEmpty(condition)) {
			sView.showMsgToast("查询条件不能为空！", Toast.LENGTH_SHORT);
//			sView.setAllDataLvVisible();
		} else {
			searchDealer = dealerBll.searchDealerByCondition(condition);
			searchAdapter.refreshData(searchDealer);
			if (searchDealer.size() > 0) {
				sView.setSearchDataLvVisible();
			} else {
				sView.showMsgToast("未搜索到信息", Toast.LENGTH_SHORT);
			}
			
		}		
	}

	@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);
		DealerInfoBean b = allDealer.get(position);
		Intent intent = new Intent();
		intent.putExtra("choicedDealer", b);
		sView.finishWithResult(10020, 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 addressDetail = searchDealer.get(position);
//		Intent intent = new Intent();
//		intent.putExtra("addressDetail", addressDetail);
//		sView.finishWithResult(InternalOptionConstant.CHOICE_TRUCK_END_STATION_DETAIL_RESULT_CODE, intent);
	
		DealerInfoBean b = searchDealer.get(position);
		Intent intent = new Intent();
		intent.putExtra("choicedDealer", b);
		sView.finishWithResult(10020, 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);
		searchDealer();
//		String addressDetail = sView.getEtText();
//		Intent intent = new Intent();
//		intent.putExtra("addressDetail", addressDetail);
//		sView.finishWithResult(InternalOptionConstant.CHOICE_TRUCK_END_STATION_DETAIL_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();
	}


}
