package xn.hxp.ui.login.fragment;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.CameraSelector;

import com.blankj.utilcode.util.LogUtils;
import com.google.mlkit.vision.face.Face;
import com.king.camera.scan.AnalyzeResult;
import com.king.camera.scan.CameraScan;
import com.king.camera.scan.config.CameraConfig;
import com.king.camera.scan.config.CameraConfigFactory;
import com.king.mlkit.vision.face.FaceCameraScanFragment;

import java.util.List;

import xn.hxp.R;
import xn.hxp.room.bean.DeviceInfo;
import xn.hxp.room.tool.DeviceInfoTool;
import xn.hxp.ui.login.callback.OnFaceScanResultCallback;

public class LoginFaceFragment extends FaceCameraScanFragment {
    private OnFaceScanResultCallback onFaceScanResultCallback;

    public LoginFaceFragment(OnFaceScanResultCallback onFaceScanResultCallback) {
        this.onFaceScanResultCallback = onFaceScanResultCallback;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void initCameraScan(@NonNull CameraScan<List<Face>> cameraScan) {
        super.initCameraScan(cameraScan);
        // 相机配置
        DeviceInfo deviceInfo = DeviceInfoTool.INSTANCE.getDeviceInfo();
        LogUtils.json(deviceInfo);
        cameraScan.setPlayBeep(true) // 设置是否播放音效，默认为false
                .setVibrate(true) // 设置是否震动，默认为false
                .setCameraConfig(CameraConfigFactory.createDefaultCameraConfig(requireContext(),
                        deviceInfo.isFrontCamera() ? CameraSelector.LENS_FACING_FRONT : CameraSelector.LENS_FACING_BACK)) // 设置相机配置信息
                .setNeedTouchZoom(false) // 支持多指触摸捏合缩放，默认为true
                .setAnalyzeImage(true); // 设置是否分析图片，默认为true。如果设置为false，相当于关闭了扫描识别功能
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_login_face;
    }

    @Override
    public void onScanResultCallback(@NonNull AnalyzeResult<List<Face>> result) {
        getCameraScan().setAnalyzeImage(false);
        onFaceScanResultCallback.onScanResultCallback(result);
    }

    @Override
    public void onScanResultFailure() {
        super.onScanResultFailure();
        onFaceScanResultCallback.onScanResultFailure();
    }

    public void setAnalyzeImage(boolean isAnalyze) {
        getCameraScan().setAnalyzeImage(isAnalyze);
    }
}
