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.GridView;
import android.widget.TextView;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.ProductDetailListConstact;
import cn.net.caoyao.cpzshandset.service.presenter.ProductDetailListConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class ProductDetailGvActivity extends BaseScanActivity implements OnClickListener,ProductDetailListConstact.View {

	private ProductDetailListConstact.Presenter presenter;
	private TextView tv_title_common;
	private Button btn_title_more_option;
	private View btn_p_list_scanner,btn_p_list_search;
	private EditText et_p_list_search_condition;
	private GridView gv_p_list,gv_p_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_product_gv_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);
		tv_title_common.setText("选择产品");
		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);
	}

	@Override
	public void initView() {
		btn_p_list_scanner = findViewById(R.id.btn_p_list_scanner);
		et_p_list_search_condition = (EditText) findViewById(R.id.et_p_list_search_condition);
		btn_p_list_search = findViewById(R.id.btn_p_list_search);
		gv_p_list = (GridView) findViewById(R.id.gv_p_list);
		gv_p_list_search = (GridView) findViewById(R.id.gv_p_list_search);
	}

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

	@Override
	public void setListener() {
		btn_p_list_scanner.setOnClickListener(this);
		btn_p_list_search.setOnClickListener(this);
		gv_p_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter == null) return;
				presenter.onPLvItemClickListener(position);
			}
		});
		gv_p_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_p_list_scanner:
			if (presenter == null)return;
			presenter.btnScannerClick();
			break;
		case R.id.btn_p_list_search:
			if (presenter == null)return;
			presenter.btnSearchClick();
			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 (gv_p_list == null || adapter == null) return;
		gv_p_list.setAdapter(adapter);
	}

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

	@Override
	public String getEtText() {
		return et_p_list_search_condition == null ? "":et_p_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_p_list_search_condition == null || t == null) return;
		et_p_list_search_condition.addTextChangedListener(t);
	}

	@Override
	public void removeTextChangedListener(TextWatcher t) {
		if (et_p_list_search_condition == null || t == null) return;
		et_p_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 (gv_p_list_search == null || adapter == null) return;
		gv_p_list_search.setAdapter(adapter);
	}

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

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

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

}
