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 cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.InternalOptionConstant;
import cn.net.caoyao.cpzshandset.service.presenter.ChoiceAddressListConstact;
import cn.net.caoyao.cpzshandset.service.presenter.ChoiceAddressListConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.train.YthChoiceEndStationPresenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.train.YthChoiceTrainConsigeePresenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.shipment.truck.yth.YthChoiceTruckEndStationPresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class ChoiceAddressStyleActivity extends BaseActivity
		implements ChoiceAddressListConstact.View, OnClickListener {
	private ChoiceAddressListConstact.Presenter presenter;
	private TextView tv_title_common;
	private EditText et_address_list_search_condition,et_consigee_list_search_condition;
	private Context context;
	private ListView lv_address_list_search,lv_address_list;
	private View btn_address_list_search;
	private MyToast myToast;
	
	private View lin_choice_train_province;
	private TextView tv_train_province_content;

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

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}
	@Override
	public void initTitleView() {
		View image_title_back_common = findViewById(R.id.image_title_back_common);
		image_title_back_common.setVisibility(View.VISIBLE);
		image_title_back_common.setOnClickListener(this);
		tv_title_common = (TextView) findViewById(R.id.tv_title_common);
	}

	@Override
	public void initView() {
		et_address_list_search_condition = (EditText) findViewById(R.id.et_address_list_search_condition);
		lv_address_list_search = (ListView) findViewById(R.id.lv_address_list_search);
		lv_address_list = (ListView) findViewById(R.id.lv_address_list);
		btn_address_list_search = findViewById(R.id.btn_address_list_search);
		lin_choice_train_province = findViewById(R.id.lin_choice_train_province);
		tv_train_province_content = (TextView) findViewById(R.id.tv_train_province_content);
		et_consigee_list_search_condition = (EditText) findViewById(R.id.et_consigee_list_search_condition);
	}

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

	private void switchCurrentOption() {
		Intent intent = getIntent();
		int optionMode = getIntent().getIntExtra("optionMode", 0); 
		switch (optionMode) {
		case InternalOptionConstant.ADDRESS_MODE_TRAIN_STATION_NAME:
			//选择到达站名称
			presenter = new YthChoiceEndStationPresenter(this, context);
			break;
		case InternalOptionConstant.ADDRESS_MODE_TRAIN_END_STATION:
			presenter = new YthChoiceTrainConsigeePresenter(this, context,intent);
			break;
		case InternalOptionConstant.ADDRESS_MODE_TRUCK_STATION:
			presenter = new YthChoiceTruckEndStationPresenter(this, context, intent);
			break;
		}
		
	}

	@Override
	public void setListener() {
		lin_choice_train_province.setOnClickListener(this);
		btn_address_list_search.setOnClickListener(this);
		lv_address_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {
					presenter.onAddressLvItemClickListener(position);
				}
				
			}
		});
		lv_address_list_search.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {
					presenter.onSearchLvItemClickListener(position);
				}
				
			}
		});
	}

	@Override
	public void initData() {

	}

	@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 || duration < 0) return;
		myToast.show(msg, duration);
	}

	@Override
	public void setPageTitleStr(String titleStr) {
		if (tv_title_common != null && titleStr != null) {
			tv_title_common.setText(titleStr);
		}

	}

	@Override
	public void setAllAddressLvAdapter(BaseAdapter adapter) {
		if(lv_address_list == null || adapter == null) return;
		lv_address_list.setAdapter(adapter);

	}

	@Override
	public void setAddressSearchAdapter(BaseAdapter adapter) {
		if(lv_address_list_search == null || adapter == null) return;
		lv_address_list_search.setAdapter(adapter);

	}

	@Override
	public void setEtText(String etStr) {
		if (et_address_list_search_condition!=null && etStr != null) {
			et_address_list_search_condition.setText(etStr +"");
		}

	}

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

	@Override
	public void setBtnRightStr(String moreStr) {
		// TODO Auto-generated method stub

	}

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

	}

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

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

	@Override
	public void setSearchAddressLvVisible() {
		if (lv_address_list_search != null) lv_address_list_search.setVisibility(View.VISIBLE);
		if(lv_address_list != null) lv_address_list.setVisibility(View.GONE);
	}

	@Override
	public void setAllAddressLvVisible() {
		if (lv_address_list_search != null) lv_address_list_search.setVisibility(View.GONE);
		if(lv_address_list != null) lv_address_list.setVisibility(View.VISIBLE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_choice_train_province:
			if (presenter != null) {
				presenter.choiceProviceClick();
			}
			break;
		case R.id.image_title_back_common:
			finish();
			break;
		case R.id.btn_address_list_search:
			if (presenter != null) {
				presenter.btnSearchClick();
			}
			break;

		}

	}

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

	@Override
	public void setProvinceStr(String provinceStr) {
		if (tv_train_province_content != null && provinceStr != null) {
			tv_train_province_content.setText(provinceStr);
		}
	}

	@Override
	public View getProvinceView() {
		return lin_choice_train_province;
	}

	@Override
	public TextView getProvinceTextView() {
		return tv_train_province_content;
	}

	@Override
	public void setProvinceViewVisiable(int visiable) {
		if (lin_choice_train_province != null) {
			lin_choice_train_province.setVisibility(visiable);
		}
	}


	@Override
	public void setConsigeeEtVisiable(int visiable) {
		if (et_consigee_list_search_condition != null) {
			et_consigee_list_search_condition.setVisibility(visiable);
		}
	}

	@Override
	public void setEndStationEtVisiable(int visiable) {
		if (et_address_list_search_condition != null) {
			et_address_list_search_condition.setVisibility(visiable);
		}
	}

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

	@Override
	public void addConsigeeTextChangedListener(TextWatcher t) {
		if (et_consigee_list_search_condition != null && t != null) {
			et_consigee_list_search_condition.addTextChangedListener(t);
		}
	}

	@Override
	public void removeConsigeeTextChangedListener(TextWatcher t) {
		if (et_consigee_list_search_condition != null && t != null) {
			et_consigee_list_search_condition.removeTextChangedListener(t);
		}		
	}

}
