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

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.CarNoDetailListConstact;
import cn.net.caoyao.cpzshandset.service.presenter.CarNoDetailListConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class CarNoDetailListActivity extends BaseScanActivity implements OnClickListener,CarNoDetailListConstact.View {

	private CarNoDetailListConstact.Presenter presenter;
	private TextView tv_title_common,tv_serial;
	private Button btn_title_more_option;
	private View btn_car_no_list_search,btn_car_no_bottom;
	private EditText et_car_no_list_search_condition;
	private ListView lv_car_no_list,lv_car_no_list_search;
	private MyToast myToast;
	private Context context;

	@Override
	protected Handler initHandler() {
		return presenter != null ? presenter.getHandler() : null;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		PhoneScanner.isDeviceId = true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		PhoneScanner.isDeviceId = false;
		if (myToast != null) myToast.cancel();
	}
	@Override
	public int loadView() {
		init();
		return R.layout.activity_car_no_list;
	}

	private void init() {
		context = this;
		myToast = new MyToast(context);
	}

	@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);
		btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
		btn_title_more_option.setVisibility(View.VISIBLE);
		btn_title_more_option.setOnClickListener(this);
		tv_serial = (TextView) findViewById(R.id.tv_serial);
	}

	@Override
	public void initView() {
		et_car_no_list_search_condition = (EditText) findViewById(R.id.et_car_no_list_search_condition);
		btn_car_no_list_search = findViewById(R.id.btn_car_no_list_search);
		lv_car_no_list = (ListView) findViewById(R.id.lv_car_no_list);
		lv_car_no_list_search = (ListView) findViewById(R.id.lv_car_no_list_search);
		btn_car_no_bottom = findViewById(R.id.btn_car_no_bottom);
	}

	@Override
	public void initPresenter() {
		HandsetOperation option_type = (HandsetOperation) getIntent().getSerializableExtra("option_type");
		presenter = createService.createCarNoListPresenter(cacheG, context, this, getIntent());
//		LogUtil.d(TConfig.Echo, "initPresenter:" + presenter);
	}

	@Override
	public void setListener() {
		btn_car_no_bottom.setOnClickListener(this);
		btn_car_no_list_search.setOnClickListener(this);
		lv_car_no_list.setOnItemClickListener(new OnItemClickListener() {

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

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

	@Override
	public void initData() {

	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (presenter != null) {
			presenter.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_title_back_common:
			//返回按钮
			finish();
			break;

		case R.id.btn_title_more_option:
			//更新产品列表操作
//			LogUtil.d(TConfig.Echo, "更新产品列表");
			if (presenter == null)return;
				presenter.updatePListClick();
			break;
		case R.id.btn_car_no_list_search:
			if (presenter == null)return;
			presenter.btnSearchClick();
			break;
		case R.id.btn_car_no_bottom:
			if(presenter == null) return;
			presenter.btnBottomClick();
			break;
		}
	}

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

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		if (reqCode < 0 || intent == null) return;
		startActivityForResult(intent, reqCode);
	}

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

	@Override
	public void readQRCode() {
		PhoneScanner.Read(createService, cacheG, context);
	}

	@Override
	public void setPageTitleStr(String titleStr) {
		if (tv_title_common == null || titleStr == null) return;
		tv_title_common.setText(titleStr+"");		
	}

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

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

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

	@Override
	public void setBtnRightStr(String moreStr) {
		if (btn_title_more_option == null || moreStr == null) return;
		btn_title_more_option.setText(moreStr +"");
	}

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

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

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

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

	@Override
	public void setSearchPLvVisible() {
		if (lv_car_no_list_search != null && lv_car_no_list_search.getVisibility() != View.VISIBLE) lv_car_no_list_search.setVisibility(View.VISIBLE);
		if (lv_car_no_list != null && lv_car_no_list.getVisibility() != View.GONE) lv_car_no_list.setVisibility(View.GONE);
	}

	@Override
	public void setAllPLvVisible() {
		if (lv_car_no_list_search != null && lv_car_no_list_search.getVisibility() != View.GONE) lv_car_no_list_search.setVisibility(View.GONE);
		if (lv_car_no_list != null && lv_car_no_list.getVisibility() != View.VISIBLE) lv_car_no_list.setVisibility(View.VISIBLE);
	}

	@Override
	public void vibrate(int duration) {
		vibrateReadQRComplete(duration);
	}

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

	@Override
	public void setTitleSerialText(String serialText) {
		if(tv_serial == null || serialText == null) return;
		tv_serial.setText(serialText+"");
		
	}

	@Override
	public void finishCarNoDetailListActivity() {
		finish();
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		presenter.onResumeCarNoDetailListActivity();
	}

	@Override
	public void setButtonCarNew(int visibility) {
		// TODO Auto-generated method stub
		if(btn_car_no_bottom == null) return;
		btn_car_no_bottom.setVisibility(visibility);
	}
}
