<?php

namespace App\Jobs;

use App\Models\User\UserModel;
use App\Services\Partner\NoticeService;
use App\Services\User\PartnerService;
use App\Services\Vendor\BaiduAi\FaceService;
use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class FaceVerifyJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    private $uid;
    private $liveness_image;
    private $check_image;
    private $cnt = 0;

    /**
     * Create a new job instance.
     *
     * @param  int  $uid
     * @param  string  $liveness_image
     * @param  string  $check_image
     * @param  int  $cnt
     */
    public function __construct(int $uid, string $liveness_image, string $check_image, int $cnt)
    {
        $this->uid = $uid;
        $this->liveness_image = $liveness_image;
        $this->check_image = $check_image;
        $this->cnt = $cnt;
    }

    /**
     * Execute the job.
     *
     */
    public function handle()
    {
        sleep(5);
        $checks = array(
            'photo_src' => 'check_photo',
            'photo_1' => 'photo_1_check',
            'photo_2' => 'photo_2_check',
            'photo_3' => 'photo_3_check',
            'photo_4' => 'photo_4_check'
        );

        if ($this->cnt > 5) {
            return;
        }
        $photo_field = null;
        try {
            $user = UserModel::findOrFail($this->uid);
            if (!empty($user->identity_auth)) {
                return;
            }
            if ($user->photo_src == $this->check_image) {
                $photo_field = "photo_src";
            }
            if ($user->photo_1 == $this->check_image) {
                $photo_field = "photo_1";
            }
            if ($user->photo_2 == $this->check_image) {
                $photo_field = "photo_2";
            }
            if ($user->photo_3 == $this->check_image) {
                $photo_field = "photo_3";
            }
            if ($user->photo_4 == $this->check_image) {
                $photo_field = "photo_4";
            }
            if (empty($photo_field)) {
                return false;
            }
        } catch (\Exception $exception) {
            return;
        }

        $fs = new FaceService();
        $images = array(
            [
                'image' => $this->liveness_image,
                'image_type' => 'URL',
                'face_type' => 'LIVE',
                'quality_control' => 'NONE',
                'liveness_control' => 'LOW'
            ],
            [
                'image' => "https://oss.pocketuniversity.cn" . $this->check_image,
                'image_type' => 'URL',
                'face_type' => 'LIVE',
                'quality_control' => 'NONE',
                'liveness_control' => 'NONE'
            ]
        );
        $data = $fs->match($images);

        $msg = $data;
        $msg['uid'] = $this->uid;
        $msg['liveness_image'] = $this->liveness_image;
        $msg['check_image'] = $this->check_image;
        $msg['field'] = $photo_field;
        switch ($data['error_code']) {
            case 0:
                if ($data['result']['score'] > 69) {
                    $user->identity_auth = $photo_field;
                    $user->save();
                    if ($user->partner_id > 0) {
                        $ps = new PartnerService();
                        $ps->check($user->partner_id, $checks[$photo_field], 1);
                    }
                    if ($user->wx_auth == 1) {
                        $ns = new NoticeService();
                        $ns->authenticationSuccess($user->uid);
                    }
                    return;
                } else {
                    return;
                }
                break;
                FaceVerifyJob::dispatch(
                    $this->uid,
                    $this->liveness_image,
                    $this->check_image,
                    $this->cnt + 1
                )->delay(Carbon::now()->addMinutes(10));
                break;
            case 222204:
            default:
                break;
        }
    }
}
