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.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import cilico.tools.barcode.PhoneScanner;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import cn.net.caoyao.cpzshandset.R;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.OptionHisListStyleConstact;
import cn.net.caoyao.cpzshandset.service.presenter.OptionHisListStyleConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class OptionSubListStyleActivity extends BaseScanActivity
		implements OnClickListener, OptionHisListStyleConstact.View {

	private OptionHisListStyleConstact.Presenter presenter;
	private ListView lv_option_his_list_style;
	private TextView tv_title_common;
	private Button btn_title_more_option,btn_option_his_list_upload;
	private MyToast myToast;
	private Context context;
	private CacheGet cacheG;

	/*@Override
	protected Handler initHandler() {
		return presenter != null ? presenter.getHandler() : null;
	}*/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (presenter != null) {
			presenter.onResume();
		}
	}
	
	@Override
	public int loadView() {
		init();
		return R.layout.activity_option_history_list_style;
	}

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

	@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);
	}

	@Override
	public void initView() {
		lv_option_his_list_style = (ListView) findViewById(R.id.lv_option_his_list_style);
		btn_option_his_list_upload = (Button) findViewById(R.id.btn_option_his_list_upload);
		
	}

	@Override
	public void initPresenter() {
//		int optionMode = getIntent().getIntExtra("optionMode", 0);
		HandsetOperation optionMode = (HandsetOperation) getIntent().getSerializableExtra("optionMode");
		switchCurrentOption(optionMode);
	}
	private void switchCurrentOption(HandsetOperation optionMode) {
		switch (optionMode) {
		case OPERATION_PACK_CONSTANT:
			// 打包
			presenter = createService.createPackSubListPresenter(cacheG, context, this, getIntent());
			break;
		case OPERATION_ACTIVATE_WITH_PACK_CONSTANT:
			//激活并打包
			presenter = createService.createActivateWithPackSubListPresenter(cacheG, context, this, getIntent());
			break;
		case OPERATION_TRUCK_SHIPMENT_CONSTANT:
			presenter = createService.createTruckShipmentSubListPresenter(cacheG,context,this,getIntent());
			break;
		case OPERATION_PACK_BOTTLE_GIFTBOX_CONSTANT:
			//盒瓶关联
			presenter = createService.createPackBottleBoxSubListPresenter(cacheG,context,this,getIntent());
			break;
		case OPERATION_PACK_GIFTBOX_BOX_CONSTANT:
			//盒箱关联
			presenter = createService.createPackGiftBoxSubListPresenter(cacheG,context,this,getIntent());
			break;
		}

	}


	@Override
	public void setListener() {
		btn_option_his_list_upload.setOnClickListener(this);
		lv_option_his_list_style.setOnItemClickListener(new OnItemClickListener() {

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

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

	@Override
	public void initData() {

	}

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

	@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:
			// 返回按钮
			/*if (presenter != null) {
			 //目前不需要实现该方法
				presenter.backBtnClick();
			}*/
			finish();
			break;
		case R.id.btn_title_more_option://新 增 
			if (presenter != null) {
				presenter.moreOptionClick();
			}
			break;
		case R.id.btn_option_his_list_upload://全部上传
			if (presenter != null) {
				presenter.uploadAllHisList();
			}
			break;
		}
	}

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

	}

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

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

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

	@Override
	public void setPageTitle(String pageTitle) {
		if (tv_title_common == null || StringUtils.isEmpty(pageTitle)) return;
		tv_title_common.setText(pageTitle);
	}

	@Override
	public void setMoreOptionText(String btnMoreTextStr) {
		if (btn_title_more_option == null || StringUtils.isEmpty(btnMoreTextStr))
			return;
		btn_title_more_option.setText(btnMoreTextStr);
	}

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

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

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

	@Override
	protected Handler initHandler() {
		return presenter != null ? presenter.getHandler() : null;
	}
	@Override
	public void setMoreOptionVisibility(int visibility) {
		if (btn_title_more_option == null) return;
		btn_title_more_option.setVisibility(visibility);
	}
	@Override
	public void setBottomText(String bottomTextStr) {
		if(btn_option_his_list_upload == null || bottomTextStr == null) return;
		btn_option_his_list_upload.setText(bottomTextStr+"");
	}
	@Override
	public void vibrate(int duration) {
		vibrateReadQRComplete(100);
		
	}

}
