package com.fxy.mine;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.widget.RecyclerView;


import com.elvishew.xlog.XLog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.fxy.baselibrary.base.BaseActivity;
import com.fxy.R;
import com.fxy.common.PictureSelectorUpload;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.qrcode.core.BarcodeType;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.BarcodeFormat;
import cn.bingoogolapple.qrcode.zbar.ZBarView;



public class PictureSelectorActivity extends BaseActivity implements QRCodeView.Delegate {
    private Context mContext=this;
    private List<LocalMedia> selectList = new ArrayList<>();
    private RecyclerView mRecyclerView;

    private static final String TAG = PictureSelectorActivity.class.getSimpleName();
    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;

    private ZBarView mZBarView;

    private PictureSelectorUpload pictureSelectorUpload;


    @Override
    public boolean showToolBar() {
        return false;
    }

    @Override
    public int getContentViewResId() {
        return R.layout.activity_picture_selector;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected void initView() {
        mRecyclerView = findViewById(R.id.recycler);

        pictureSelectorUpload = new PictureSelectorUpload(PictureSelectorActivity.this,mRecyclerView,"tally");

        mZBarView = findViewById(R.id.zbarview);
        mZBarView.setDelegate(PictureSelectorActivity.this);

        mZBarView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZBarView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别

//        mZBarView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    public boolean openEventBus() {
        return false;
    }

    @Override
    public boolean isPlayMusic() {
        return true;
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调
                //pictureSelectorUpload.getSelectImg(data);
                try {
                    mZBarView.startSpot();
                    List<LocalMedia>  images = PictureSelector.obtainMultipleResult(data);
                    final String picturePath = images.get(0).getPath();
                    mZBarView.decodeQRCode(picturePath);
                } catch (Exception e) {
                    XLog.e(e.getMessage());
                }
            }
        }
    }



    @Override
    public void onScanQRCodeSuccess(String result) {
        XLog.i(TAG, "result:" + result);
        XLog.e("扫描结果为：" + result);


    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {

    }

    @Override
    public void onScanQRCodeOpenCameraError() {

    }
}
