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

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import cilico.tools.barcode.PhoneScanner;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.service.presenter.UnpackConstact;
import cn.net.caoyao.cpzshandset.service.presenter.UnpackConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class UnpackActivity extends BaseScanActivity implements OnClickListener, UnpackConstact.View {
	private Context context;
	private MyToast myToast;
	private UnpackConstact.Presenter presenter;
	private TextView tv_title_common, btn_title_more_option;
	private ListView lv_unpack_scanner;

	@Override
	protected Handler initHandler() {
		return presenter == null ? null : presenter.getHandler();
	}

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

	@Override
	public int loadView() {
		init();
		return R.layout.activity_unpack;
	}

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

	@Override
	public void initView() {
		View btn_unpack_scanner = findViewById(R.id.btn_unpack_scanner);
		btn_unpack_scanner.setOnClickListener(this);
		lv_unpack_scanner = (ListView) findViewById(R.id.lv_unpack_scanner);
	}

	@Override
	public void initPresenter() {
		presenter = createService.createUnPackInfoPresenter(cacheG, context, this, getIntent());
	}

	@Override
	public void setListener() {
		lv_unpack_scanner.setOnItemClickListener(new OnItemClickListener() {

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

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {
					presenter.onItemLongClickListener(position);
				}
				return false;
			}
		});
	}

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

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_title_back_common:
			// if (presenter == null) return;
			finish();
			break;
		case R.id.btn_title_more_option:
			if (presenter == null)
				return;
			presenter.uploadClick();
			break;
		case R.id.btn_unpack_scanner:
			if (presenter == null)
				return;
			presenter.btnScannerClick();
			break;

		}
	}

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

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		if (intent != null) {
			startActivityForResult(intent, reqCode);
		}
	}

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

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

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

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

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

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

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

}
