package com.qhclh.ytzh.work.scan;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.ScanBean;

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

import butterknife.BindView;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by 青花瓷 on 2017/11/29.
 */
///< 扫一扫毛鸡
public class ScanResultActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.lv_infos)
    ListView lv_infos;
    private String scanStr = "";
    private boolean scanf = false;

    private List<ScanBean> scanBeanList;
    private ScanAdapter scanAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.act_scanresult;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "扫码结果", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        scanBeanList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (scanf == false) {
            dongtaiQuanxian();
        }
    }


    private static final int REQUEST_PERMISSION_CAMERA_CODE = 102;

    @AfterPermissionGranted(REQUEST_PERMISSION_CAMERA_CODE)
    private void dongtaiQuanxian() {
        String[] perms = {Manifest.permission.CAMERA};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, getString(R.string.permission_camera),
                    REQUEST_PERMISSION_CAMERA_CODE, perms);
        } else {
            // Already have permission, do the thing
            scan();
            scanf = true;
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    private void scan() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        // 设置要扫描的条码类型，ONE_D_CODE_TYPES：一维码，QR_CODE_TYPES-二维码
        integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
        integrator.setCaptureActivity(ScanActivity.class);
        integrator.setPrompt("请扫描"); //底部的提示文字，设为""可以置空
        integrator.setCameraId(0); //前置或者后置摄像头
        integrator.setBeepEnabled(false); //扫描成功的「哔哔」声，默认开启
        integrator.setBarcodeImageEnabled(true);
        integrator.initiateScan();
    }

    ///< 回调获取扫描得到的条码值
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                showToast("取消扫描");
                finish();
            } else {
                scanStr = result.getContents();
                System.out.println("aaa++++++++"+scanStr);
//                for (int i=0;i<4;i++){
//                    scanBeanList.add(new ScanBean(i,"车牌号"+i,"司机"+i,"联系方式"+i,"批次"+i,"厂址"+i,
//                            "时间"+i,"数量"+i,"免疫"+i,"均匀度"+i,"起始时间"+i,"到达时间"+i));
//                }
//                scanAdapter = new ScanAdapter(this,scanBeanList);
//                lv_infos.setAdapter(scanAdapter);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
