package cn.net.caoyao.cpzshandset.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
import cn.net.caoyao.cpzshandset.service.presenter.SearchInputStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.SearchInputStyleConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.truck.ythImage.YthImageInputAddressDetailPresenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.truck.ythImage.YthImageInputConsigneePresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class SearchByInputStyleActivity extends BaseActivity implements SearchInputStyleConstact.View, OnClickListener {
	private SearchInputStyleConstact.Presenter presenter;
	private Context context;
	private MyToast myToast;
	private View image_search_del;
	private EditText et_search_condition;
	private TextView btn_p_list_search;
	private ListView lv_search, lv_all;

	@Override
	public int loadView() {
		context = this;
		myToast = new MyToast(context);
		return R.layout.activity_search_list;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}

	@Override
	public void initTitleView() {
	}

	@Override
	public void initView() {
		View btn_search_back = findViewById(R.id.btn_search_back);
		btn_search_back.setOnClickListener(this);
		btn_p_list_search = (TextView) findViewById(R.id.btn_p_list_search);
		btn_p_list_search.setOnClickListener(this);
		image_search_del = findViewById(R.id.image_search_del);
		et_search_condition = (EditText) findViewById(R.id.et_search_condition);
		lv_search = (ListView) findViewById(R.id.lv_search);
		lv_all = (ListView) findViewById(R.id.lv_all);
		// et_search_condition.setHint("请输入经销商名称");
		// et_search_condition.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
	}

	@Override
	public void initPresenter() {
		switchCurrentOption();
	}

	private void switchCurrentOption() {
		Intent intent = getIntent();
		int optionMode = getIntent().getIntExtra("optionMode", 0);

		switch (optionMode) {
		case InternalOptionConstant.ADDRESS_MODE_TRUCK_DETAIL_STATION:
			// 选择到达站名称
			presenter = new YthImageInputAddressDetailPresenter(this, context, intent);
			break;
		case InternalOptionConstant.ADDRESS_MODE_TRUCK_CONSIGNEE:
			// 输入汽车到达站的客户信息
			presenter = new YthImageInputConsigneePresenter(this, context);
			break;
		case InternalOptionConstant.ADDRESS_MODE_CHOICE_DEALER:
			// 选择经销商
			HandsetOperation optionModeInOption = (HandsetOperation) getIntent()
					.getSerializableExtra("optionModeInOption");
			switchCurrentOptionInOption(optionModeInOption);
			break;
		}

	}

	@SuppressWarnings("incomplete-switch")
	private void switchCurrentOptionInOption(HandsetOperation optionModeInOption) {

		switch (optionModeInOption) {
		case OPERATION_ACTIVATE_CONSTANT:
			// 激活
			break;
		case OPERATION_PACK_CONSTANT:
			// 打包
			break;
		case OPERATION_OUT_WAREHOUSE_CONSTANT:
			// 出库
			presenter = createService.createWarehouseDealerChoicePresenter(cacheG, context, this, getIntent());
			break;
		case OPERATION_IN_WAREHOUSE_CONSTANT:
			// 入库
			break;
		case OPERATION_BACK_WAREHOUSE_CONSTANT:
			// 返库
			break;
		case OPERATION_SALE_CONSTANT:
			// 销售
			presenter = createService.createDealerChoicePresenter(cacheG, context, this, getIntent());
			break;
		case OPERATION_DEALER_OUT_CONSTANT:
			presenter = createService.createDealerOutChoicePresenter(cacheG, context, this, getIntent());
			break;
		case OPERATION_FARM_CONSTANT:
			// 养殖
			break;
		case OPERATION_PLANT_CONSTANT:
			// 种植
			break;
		case OPERATION_TRAIN_SHIPMENT_CONSTANT:
			// 火车发货
			break;
		case OPERATION_TRUCK_SHIPMENT_CONSTANT:
			// 汽车发货
			break;
		case OPERATION_EXCHANGE_CARGO_CONSTANT:
			// 调货
			break;
		}
	}

	@Override
	public void setListener() {
		image_search_del.setOnClickListener(this);
		lv_search.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (presenter != null) {
					presenter.onSearchLvItemClickListener(position);
				}
			}
		});
		lv_all.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (presenter != null) {
					presenter.onAllLvItemClickListener(position);
				}
			}
		});
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setPresenter(Presenter presenter) {
		if (presenter != null) {
			this.presenter = presenter;
		}
	}

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		// TODO Auto-generated method stub

	}

	@Override
	public void showMsgToast(String msg, int duration) {
		if (myToast == null || msg == null)
			return;
		myToast.show(msg, duration);
	}

	@Override
	public void readQRCode() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setAllDataLvAdapter(BaseAdapter adapter) {
		if (lv_all == null || adapter == null)
			return;
		lv_all.setAdapter(adapter);
	}

	@Override
	public void setDataSearchAdapter(BaseAdapter adapter) {
		if (lv_search == null || adapter == null)
			return;
		lv_search.setAdapter(adapter);
	}

	@Override
	public void setEtText(String etStr) {
		if (et_search_condition == null || etStr == null)
			return;
		et_search_condition.setText(etStr);
	}

	@Override
	public String getEtText() {
		return et_search_condition == null ? "" : et_search_condition.getText().toString().trim();
	}

	@Override
	public void addTextChangedListener(TextWatcher t) {
		if (et_search_condition == null || t == null)
			return;
		et_search_condition.addTextChangedListener(t);
	}

	@Override
	public void removeTextChangedListener(TextWatcher t) {
		if (et_search_condition == null || t == null)
			return;
		et_search_condition.removeTextChangedListener(t);
	}

	@Override
	public void finishWithResult(int resultCode, Intent intent) {
		if (intent == null)
			return;
		setResult(resultCode, intent);
		finish();
	}

	@Override
	public void setSearchDataLvVisible() {
		if (lv_search != null)
			lv_search.setVisibility(View.VISIBLE);
		if (lv_all != null)
			lv_all.setVisibility(View.GONE);
	}

	@Override
	public void setAllDataLvVisible() {
		if (lv_search != null)
			lv_search.setVisibility(View.GONE);
		if (lv_all != null)
			lv_all.setVisibility(View.VISIBLE);
	}

	@Override
	public void setOnEditorActionListener(OnEditorActionListener actionListener) {
		if (et_search_condition == null || actionListener == null)
			return;
		et_search_condition.setOnEditorActionListener(actionListener);
	}

	@Override
	public void finishPage() {
		finish();
	}

	@Override
	public void delIconVisibility(int visibility) {
		if (image_search_del == null)
			return;
		image_search_del.setVisibility(visibility);
	}

	@Override
	public EditText getEditText() {
		return et_search_condition;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search_back:
//			LogUtil.d(TConfig.Echo, "btn_search_back backClick");
			if (presenter != null) {
				presenter.backClick();
			}
			break;
		case R.id.btn_p_list_search:
			if (presenter != null) {
				presenter.completeClick();
			}
			break;
		case R.id.image_search_del:
			if (presenter != null) {
				presenter.delTextClick();
			}
			break;
		}
	}

	@Override
	public void setImeOptions(int imeOptions) {
		if (et_search_condition == null)
			return;
		et_search_condition.setImeOptions(imeOptions);

	}

	@Override
	public void setEditTextHintStr(String hintStr) {
		if (et_search_condition == null || hintStr == null)
			return;
		et_search_condition.setHint(hintStr + "");
	}

	@Override
	public void setRightOptionStr(String rightOptionStr) {
		if (btn_p_list_search == null || rightOptionStr == null)
			return;
		btn_p_list_search.setText(rightOptionStr + "");

	}

}
