<?php

namespace App\Http\Controllers\Common;

use App\Http\Controllers\Controller;
use App\Services\Vendor\BaiduAi\FaceService;
use Illuminate\Http\Request;

class FaceController extends Controller
{
    /**
     * @param  Request  $request
     * @return array
     * @deprecated Common/FaceDetect
     */
    public function detect(Request $request)
    {
        $this->validate($request, [
            'image' => 'required'
        ]);
        $images = $request->post('image');
        $fs = new FaceService();
        $data = $fs->detect($images, 'URL', array(
            'face_field' => "quality",
            'max_face_num' => 1,
            'face_type' => 'LIVE'
        ));
        if (
            $data['error_code'] == 0
            && $data['result']['face_num'] > 0
            && $data['result']['face_list'][0]['quality']['occlusion']['left_eye'] < 0.6
            && $data['result']['face_list'][0]['quality']['occlusion']['right_eye'] < 0.6
            && $data['result']['face_list'][0]['quality']['occlusion']['nose'] < 0.7
            && $data['result']['face_list'][0]['quality']['occlusion']['mouth'] < 0.7
            && $data['result']['face_list'][0]['quality']['occlusion']['left_cheek'] < 0.8
            && $data['result']['face_list'][0]['quality']['occlusion']['right_cheek'] < 0.8
            && $data['result']['face_list'][0]['quality']['occlusion']['chin_contour'] < 0.8
            && $data['result']['face_list'][0]['quality']['blur'] < 0.7
            && $data['result']['face_list'][0]['quality']['illumination'] > 40
        ) {
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => [
                    'detect' => true
                ]
            );
        } else {
            return array(
                'code' => 200,
                'message' => 'success',
                'data' => [
                    'detect' => false
                ]
            );
        }
    }
}
