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.view.animation.RotateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
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.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.service.presenter.ShipmentScannerConstact;
import cn.net.caoyao.cpzshandset.service.presenter.ShipmentScannerConstact.Presenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class ShipmentScannerActivity extends BaseScanActivity implements ShipmentScannerConstact.View, OnClickListener{

	private ShipmentScannerConstact.Presenter presenter;
	private View lin_title_change_view,btn_single_label_scanner,rel_title_common;
	private TextView tv_title,tv_train_bag_count,tv_train_weight_count;
	private ImageView image_title_click_item,title_btn_option;
	private Button btn_title_more_option;
	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_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.setOnClickListener(this);
		lin_title_change_view = findViewById(R.id.lin_title_change_view);
		lin_title_change_view.setOnClickListener(this);
		tv_title = (TextView) findViewById(R.id.tv_title);
		image_title_click_item = (ImageView) findViewById(R.id.image_title_click_item);
		title_btn_option = (ImageView) findViewById(R.id.title_btn_option);
		title_btn_option.setOnClickListener(this);
		btn_title_more_option = (Button) findViewById(R.id.btn_title_more_option);
		btn_title_more_option.setOnClickListener(this);
		rel_title_common = findViewById(R.id.rel_title_common);
	}

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

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

	private void switchPresenter() {
		Intent intent = getIntent();
		int shipmentMode = intent.getIntExtra("shipmentMode", 1);//1：火车   2：汽车
		switch (shipmentMode) {
		case 1:
			presenter = createService.createTrainShipmentScannerPresenter(cacheG, context, this, getIntent());
			break;

		case 2:
			presenter = createService.createTruckShipmentScannerPresenter(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);
				}
				
			}
		});
		
	}

	@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) {
		// 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 changeAddIcon(int resId) {
		if (title_btn_option != null) {
			title_btn_option.setImageResource(resId);
		}
		
	}

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

	@Override
	public void showAnimation(View mView,boolean opendown) {
		final float centerX = mView.getWidth() / 2.0f;
		final float centerY = mView.getHeight() / 2.0f;

		RotateAnimation rotateAnimation;

		if (opendown) {
			rotateAnimation = new RotateAnimation(0, 180, centerX, centerY);
		} else {
			rotateAnimation = new RotateAnimation(180, 0, centerX, centerY);
		}
		rotateAnimation.setDuration(200);
		rotateAnimation.setFillAfter(true);
		mView.startAnimation(rotateAnimation);
		
	}

	@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 setCurrentShipmentModeStr(String shipmentModeStr) {
		if (tv_title != null && shipmentModeStr != null) {
			tv_title.setText(shipmentModeStr);
		}
		
	}

	@Override
	public View getShipmentModeGroupView() {
		return rel_title_common;
	}

	@Override
	public View getShipmentModeOptionView() {
		return image_title_click_item;
	}

	@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.lin_title_change_view:
			LogUtil.d(TConfig.Echo, "changeShipmentModeClick  activity");
			if (presenter != null) {
				presenter.changeShipmentModeClick();
			}
			break;
		case R.id.title_btn_option:
			if (presenter != null) {
				presenter.addOrSubOptionClick();
			}
			break;
		case R.id.btn_title_more_option:
			if (presenter != null) {
				presenter.lastTrayClick();
			}
			break;
		case R.id.btn_single_label_scanner:
			if (presenter != null) {
				presenter.btnScannerClick();
			}
			break;
		}
		
	}

	@Override
	public TextView getShipmentModeView() {
		return tv_title;
	}

	@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 		
//		PhoneCilicoSoundUtils.play();
		vibrateReadQRComplete(duration);
	}

}
