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.Button;
import android.widget.EditText;
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.common.util.StringUtils;
import cn.net.caoyao.cpzshandset.service.presenter.CarSaleConfigConstact;
import cn.net.caoyao.cpzshandset.service.presenter.CarSaleConfigConstact.Presenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.sale.yunCar.YunCarSaleOnlyPresenter;
import cn.net.caoyao.cpzshandset.service.presenter.impl.warehouse.out.yuncar.YunCarOutWarehouseConfigPresenter;
import cn.net.caoyao.cpzshandset.ui.activity.base.BaseScanActivity;
import cn.net.caoyao.cpzshandset.ui.weight.MyToast;

public class CarSaleConfigActivity extends BaseScanActivity implements CarSaleConfigConstact.View, OnClickListener{

	private CarSaleConfigConstact.Presenter presenter;
	private Context context;
	private MyToast myToast;
	private TextView tv_title_common,btn_title_more_option,tv_car_no_liscense,tv_bill_count,tv_engine_number;
	private View lin_choice_car_no_liscense,btn_car_no_verify,btn_package_label_scanner,btn_ll_picture,lin_vin_number,lin_client_tel,lin_package_label;
	private EditText et_truck_create_truck_no,et_engine_number,et_vin_number,et_client_tel,et_package_label;
	private ListView list_view ;
	@Override
	protected Handler initHandler() {
		return presenter == null ? null : presenter.getHandler();
	}

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

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

	@Override
	public void initView() {
		lin_choice_car_no_liscense = findViewById(R.id.lin_choice_car_no_liscense);
		tv_car_no_liscense = (TextView) findViewById(R.id.tv_car_no_liscense);
		et_truck_create_truck_no = (EditText) findViewById(R.id.et_truck_create_truck_no);
		btn_car_no_verify = findViewById(R.id.btn_car_no_verify);
		et_engine_number = (EditText) findViewById(R.id.et_engine_number);
		et_vin_number = (EditText) findViewById(R.id.et_vin_number);
		et_client_tel = (EditText) findViewById(R.id.et_client_tel);
		et_package_label = (EditText) findViewById(R.id.et_package_label);
		btn_package_label_scanner = findViewById(R.id.btn_package_label_scanner);
		tv_bill_count = (TextView) findViewById(R.id.tv_bill_count);
		btn_ll_picture = findViewById(R.id.btn_ll_picture);
		list_view = (ListView) findViewById(R.id.tv_upload_status);
		
		lin_vin_number = findViewById(R.id.lin_vin_number);//车架号
		lin_client_tel = findViewById(R.id.lin_client_tel);//客户号码
		lin_package_label = findViewById(R.id.lin_package_label);//包标签
		tv_engine_number = (TextView) findViewById(R.id.tv_engine_number);//发动机号 
		
		
		
	}
	
	@Override
	public void initPresenter() {
		HandsetOperation option_type = (HandsetOperation) getIntent().getSerializableExtra("option_type");
		switch (option_type) {
		case OPERATION_SALE_CONSTANT:
			presenter = new YunCarSaleOnlyPresenter(this, context,getIntent());
//			presenter 
			break;
		case OPERATION_OUT_WAREHOUSE_CONSTANT:
			presenter = new YunCarOutWarehouseConfigPresenter(this, context,getIntent());
			break;	

		default:
			break;
		}
		
		
	}

	@Override
	public void setListener() {
		lin_choice_car_no_liscense.setOnClickListener(this);
		btn_car_no_verify.setOnClickListener(this);
		btn_package_label_scanner.setOnClickListener(this);
		btn_ll_picture.setOnClickListener(this);
		btn_title_more_option.setOnClickListener(this);
		et_engine_number.setOnClickListener(this);
		
		list_view.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (presenter == null) return;
				presenter.listItemClick();
				
			}
		});
		list_view.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (presenter != null){
					presenter.listLongItemClick();
				}
				return false;
			}
			
		});
	}

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

	@Override
	public void setPresenter(Presenter presenter) {
		// TODO Auto-generated method stub
		
	}

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

	@Override
	public void showMsgToast(String msg, int duration) {
		if(myToast == null || context == null || StringUtils.isEmpty(msg)) 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 finishPage() {
		finish();
	}

	@Override
	public void addCustomView(View view) {
		// TODO Auto-generated method stub
		
	}

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

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

	@Override
	public View getCarNoLiscenseView() {
		return lin_choice_car_no_liscense;
	}

	@Override
	public TextView getCarNoLiscenseTextView() {
		return tv_car_no_liscense;
	}

	@Override
	public void setCarNoLiscense(String carLiscense) {
		if(tv_car_no_liscense == null || carLiscense == null) return;
		tv_car_no_liscense.setText(carLiscense);
		
	}

	@Override
	public String getCarNoLiscense() {
		return tv_car_no_liscense==null ? "" :tv_car_no_liscense.getText().toString().trim();
	}

	@Override
	public void setCarNo(String carNo) {
		if(et_truck_create_truck_no == null || carNo == null) return;
		et_truck_create_truck_no.setText(carNo);
	}

	@Override
	public String getCarNo() {
		return et_truck_create_truck_no == null ? "":et_truck_create_truck_no.getText().toString().trim();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image_title_back_common:
			if (presenter != null) {
				presenter.backClick();
			}
			break;

		case R.id.lin_choice_car_no_liscense:
			if (presenter != null) {
				presenter.choiceCarNoLiscense();
			}
			break;
		case R.id.btn_car_no_verify:
			if (presenter != null) {
				presenter.carNoVerifyClick();
			}
			break;
		case R.id.btn_package_label_scanner:
			if (presenter != null) {
				presenter.scannerClick();
			}
			break;
		case R.id.btn_ll_picture:
			if (presenter != null) {
				presenter.choicePictureClick();
			}
			break;
		case R.id.btn_title_more_option:
			if (presenter != null) {
				presenter.moreOptionClick();
			}
			break;	
			
		case R.id.et_engine_number:
			if (presenter != null) {
				presenter.dataClick();
			}
			break;	
		}
		
	}

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

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

	@Override
	public void setEngineNumber(String engineNumberStr) {
		if(et_engine_number == null || engineNumberStr == null) return;
		et_engine_number.setText(engineNumberStr+"");
		
	}

	@Override
	public String getEngineNumber() {
		return et_engine_number == null ? "": et_engine_number.getText().toString().trim();
	}

	@Override
	public void setVinNumber(String vinNumberStr) {
		if(et_vin_number == null || vinNumberStr == null) return;
		et_vin_number.setText(vinNumberStr);
		
	}

	@Override
	public String getVinNumber() {
		return et_vin_number == null ? "":et_vin_number.getText().toString().trim();
	}

	@Override
	public void setClientTel(String clientTel) {
		if(et_client_tel == null || clientTel == null) return;
		et_client_tel.setText(clientTel);
		
	}

	@Override
	public String getClientTel() {
		return et_client_tel == null ? "" :et_client_tel.getText().toString().trim();
	}

	@Override
	public void setPackageLabel(String packageLabel) {
		if(et_package_label == null || packageLabel == null) return;
		et_package_label.setText(packageLabel);
	}

	@Override
	public String getPackageLabel() {
		return et_package_label == null ? "" :et_package_label.getText().toString().trim();
	}

	@Override
	public void setBillImageCount(int count) {
		if(tv_bill_count == null) return;
		tv_bill_count.setText(" X  " + count);
		
	}
	
	public void setSaleAdapter(BaseAdapter adapter) {
		if (list_view == null || adapter == null) return;
		list_view.setAdapter(adapter);
	}

	public void setPictureVisible (boolean b){
		if(btn_ll_picture == null) return;
		btn_ll_picture.setEnabled(b);
	}

	@Override
	public void setVinNumberVisiable(int visibility) {
		// lin_vin_number,lin_client_tel,lin_package_label
		if(lin_vin_number == null) return;
		lin_vin_number.setVisibility(visibility);
	}

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

	@Override
	public void setPackageVisiable(int visibility) {
		// TODO Auto-generated method stub
		if(lin_package_label == null) return;
		lin_package_label.setVisibility(visibility);
		
	}

	@Override
	public void setEngineNumberHint(String str) {
		if(et_engine_number == null) return;
		et_engine_number.setHint(str);
		
	}

	@Override
	public void setEngineNumberText(String str) {
		// TODO Auto-generated method stub
		if(tv_engine_number == null) return;
		tv_engine_number.setText(str);
	}

	@Override
	public void setCarNoButton(int visibility) {
		// TODO Auto-generated method stub
		if(btn_car_no_verify == null) return;
		btn_car_no_verify.setVisibility(visibility);
	}

	@Override
	public void setEngineNumberEnabled(boolean b) {
		if(et_engine_number == null) return;
		et_engine_number.setFocusable(b);
		
	}

	@Override
	public EditText getDateTextView() {
		// TODO Auto-generated method stub
		return et_engine_number;
	}

	
	@Override
	public void setCarNoEnabled(boolean b) {
		// TODO Auto-generated method stub
		if(et_truck_create_truck_no == null) return;
		et_truck_create_truck_no.setFocusable(b);
	}

	@Override
	public void setBtnCarNoVerifyEnabled(boolean b) {
		// TODO Auto-generated method stub
		if(btn_car_no_verify == null) return;
		btn_car_no_verify.setEnabled(b);
	}
	
	/*@Override
	public void setUploadStatusVisiable(int visibility) {
		if(tv_upload_status == null) return;
		tv_upload_status.setVisibility(visibility);
	}

	@Override
	public void setUploadStatusStr(String uploadStr) {
		if(tv_upload_status == null || uploadStr == null) return;
		tv_upload_status.setText(uploadStr+"");
		
	}*/

}
