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.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
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.util.VibratorUtil;
import cn.net.caoyao.cpzshandset.service.presenter.ShipmentSingleScannerConstact;
import cn.net.caoyao.cpzshandset.service.presenter.ShipmentSingleScannerConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class ShipmentSingleScannerActivity extends BaseScanActivity implements ShipmentSingleScannerConstact.View, OnClickListener{

	private ShipmentSingleScannerConstact.Presenter presenter;
	private View btn_single_label_scanner;
	private Button btn_title_more_option;
	private TextView tv_train_bag_count,tv_train_weight_count,tv_title_common,tv_real_bag_count,tv_real_weight_count;
	private ViewGroup lin_single_label_scanner_custom_display;
	private ListView lv_single_label;
	private MyToast myToast;
	private Context context;
	
	@Override
	protected Handler initHandler() {
		return presenter == null ? null : presenter.getHandler();
	}

	@Override
	public int loadView() {
		context = this;
		myToast = new MyToast(context);
		return R.layout.activity_shipment_single_scanner_template;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (myToast != null) {
			myToast.cancel();
		}
	}

	@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.setOnClickListener(this);
	}

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

	@Override
	public void initPresenter() {
		switchPresenter();
		
	}

	private void switchPresenter() {
		Intent intent = getIntent();
		presenter = createService.createTruckShipmentSingleScannerPresenter(cacheG, context, this, intent);
		
	}

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

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

	@Override
	public void startAcForResult(int reqCode, Intent intent) {
		// TODO Auto-generated method stub
		
	}

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



	@Override
	public void setPackageCount(String packageCountStr) {
		if (tv_train_bag_count != null && packageCountStr != null) {
			tv_train_bag_count.setText(packageCountStr +"");
		}
		
	}

	@Override
	public void setWeightCount(String weightCountStr) {
		if (tv_train_weight_count != null && weightCountStr != null) {
			tv_train_weight_count.setText(weightCountStr);
		}
		
	}

	@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) {
			lv_single_label.setAdapter(adapter);
		}
		
	}

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

	@Override
	public void addHeadView(View customView) {
		if (lv_single_label != null && customView != null) {
			lv_single_label.addHeaderView(customView);
		}
		
	}

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

		case R.id.btn_single_label_scanner:
			if (presenter != null) {
				presenter.btnScannerClick();
			}
			break;
		case R.id.btn_title_more_option:
			if (presenter != null) {
				presenter.moreOptionClick();
			}
			break;
		}
		
	}

	@Override
	public void setScannerBtnEnabled(boolean enabled) {
		if (btn_single_label_scanner != null) {
			btn_single_label_scanner.setEnabled(enabled);
		}
		
	}

	@Override
	public void vibrate(int duration) {
		VibratorUtil.Vibrate(this, duration);   //震动100ms 		
		
	}

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

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

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

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

	@Override
	public void setRealBagCountVisibility(int visibility) {
		if(tv_real_bag_count!=null)tv_real_bag_count.setVisibility(visibility);		
		if(tv_real_weight_count != null) tv_real_weight_count.setVisibility(visibility);
	}

	@Override
	public void setRealPackageCount(String packageCountStr) {
		if(tv_real_bag_count == null || packageCountStr == null) return;
		tv_real_bag_count.setText(packageCountStr+"");
	}

	@Override
	public void setRealWeightCount(String weightCountStr) {
		if(tv_real_weight_count == null || weightCountStr == null) return;
		tv_real_weight_count.setText(weightCountStr+"");
	}

	@Override
	public void setScannerPackageCountColor(int id) {
		if (tv_train_bag_count == null ) return;
		tv_train_bag_count.setTextColor(getResources().getColor(id));
	}

	@Override
	public void setScannerWeightCountColor(int id) {
		if (tv_train_weight_count == null ) return;
		tv_train_weight_count.setTextColor(getResources().getColor(id));
	}
	

}
