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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
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.service.presenter.SingleLabelScannerConstact;
import cn.net.caoyao.cpzshandset.service.presenter.SingleLabelScannerConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.activate.kia.KIAActivateSingleLSPresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class SingleLabelScannerActivity extends BaseScanActivity implements SingleLabelScannerConstact.View, OnClickListener{
	private SingleLabelScannerConstact.Presenter presenter;
	private Context context;
	private MyToast myToast;
	private TextView tv_title_common;
	private Button btn_title_more_option;
	private ViewGroup lin_single_label_scanner_custom_display;
	private ListView lv_single_label;
	private View btn_single_label_scanner;
	
	@Override
	protected Handler initHandler() {
        Log.d("getHandler","mHandler=="+presenter.getHandler());
		return presenter != null? presenter.getHandler(): null;
	}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

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

	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_title_common.setOnLongClickListener(new OnLongClickListener() {

			@Override
			public boolean onLongClick(View v) {
				inputQRCode();
				return true;
			}
		});
	}

	@Override
	public void initView() {
		lin_single_label_scanner_custom_display = (ViewGroup) findViewById(R.id.lin_single_label_scanner_custom_display);
		lv_single_label = (ListView) findViewById(R.id.lv_single_label);
		btn_single_label_scanner = findViewById(R.id.btn_single_label_scanner);
	}

	@Override
	public void initPresenter() {
//		presenter = new WangSearchCountPresenter(this, context, null);
		switchCurrentOption(getIntent());
	}
	
	

	private void switchCurrentOption(Intent intent) {
//		int optionMode = getIntent().getIntExtra("optionMode", 0);
		HandsetOperation optionMode = (HandsetOperation) getIntent().getSerializableExtra("optionMode");
		switch (optionMode) {
		case OPERATION_ACTIVATE_CONSTANT:
			// 激活
//			createActivePresenter();
			presenter = createService.createActivateSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_PACK_CONSTANT:
			// 打包
			presenter = createService.createPackSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_OUT_WAREHOUSE_CONSTANT:
			// 出库
			presenter = createService.createOutWarehouseSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_IN_WAREHOUSE_CONSTANT:
			// 入库
			presenter = createService.createInWarehouseSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_BACK_WAREHOUSE_CONSTANT:
			// 返库
			presenter = createService.createBackWarehouseSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_SALE_CONSTANT:
			// 销售
			presenter = createService.createSaleSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_DEALER_OUT_CONSTANT:
			presenter = createService.createDealerOutSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_FARM_CONSTANT:
			// 养殖
			// TODO 通用版暂时无该功能
			break;
		case OPERATION_PLANT_CONSTANT:
			// 种植
			// TODO 通用版暂时无该功能
			break;
		case OPERATION_TRAIN_SHIPMENT_CONSTANT:
			// 火车发货
			// TODO 通用版暂时无该功能
			break;
		case OPERATION_TRUCK_SHIPMENT_CONSTANT:
			// 汽车发货
			// TODO 通用版暂时无该功能
			break;
		case OPERATION_EXCHANGE_CARGO_CONSTANT:
			presenter = createService.createExchangeCargoSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_ACTIVATE_WITH_PACK_CONSTANT:
			//激活并打包
			presenter = createService.createActivateWitPackSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_ACTIVATE_WITH_RELATION_CONSTANT:
			//套标激活
			presenter = createService.createActivateWithRelationSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_COUNTER_SALE_CONSTANT:
			//专柜销售
			presenter = createService.createCounterSaleSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_RETURNED_GOODS_CONSTANT:
			//退货
			presenter = createService.createReturnsSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_REPLENISH_BING_CONSTANT:
			//补垛
			presenter = createService.createReplenidhBingSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_PACK_BOTTLE_GIFTBOX_CONSTANT:
			//瓶盒关联
			presenter = createService.createPackBottleBoxSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_PACK_GIFTBOX_BOX_CONSTANT:
			//盒箱关联
			presenter = createService.createPackGiftBoxSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_DAN_ACTIVATE_CONSTANT:
			//单标激活
			presenter = new KIAActivateSingleLSPresenter(this, context,intent);
			//presenter = createService.createPackGiftBoxSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;
		case OPERATION_PI_ACTIVATE_CONSTANT:
			//批量激活
		//	presenter = new KIAActivateSingleLSPresenter(this, context,intent);
			//presenter = createService.createPackGiftBoxSingleLabelScannerPresenter(cacheG, context, this, intent);
			break;	
		}

	}

	@Override
	public void setListener() {
		btn_single_label_scanner.setOnClickListener(this);
		lv_single_label.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (presenter != null) {
					presenter.onItemClickLinstener(position);
				}
			}
		});
		lv_single_label.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 setPresenter(Presenter presenter) {
		if (presenter != null) {
			this.presenter = presenter;
		}
	}

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

	@Override
	public void showMsgToast(String msg, int duration) {
		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_single_label == null || adapter == null) return;
		lv_single_label.setAdapter(adapter);
	}

	@Override
	public void addCustomDisplayView(View customView) {
		if (lin_single_label_scanner_custom_display == null || customView == null) return;
		lin_single_label_scanner_custom_display.removeAllViews();
		lin_single_label_scanner_custom_display.addView(customView);
	}

	@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:
			if (presenter != null) {
				presenter.uploadClick();
			}
			break;
		case R.id.btn_single_label_scanner:
			if (presenter != null) {
				presenter.btnScannerClick();
			}
		
			break;
		}
	}

	@Override
	public void addHeadView(View customView) {
		if (lv_single_label == null || customView == null) return;
		lv_single_label.addHeaderView(customView);
	}
	@Override
	public void setMoreOptionBtnVisiable(int visibility) {
		if (btn_title_more_option != null) {
			btn_title_more_option.setVisibility(visibility);
		}
	}
	@Override
	public void vibrate(int duration) {
		vibrateReadQRComplete(duration);
		
	}

}
