<?php

namespace App\Listeners;

use App\Events\AuditMediaDeal;
use App\Jobs\XunFeiVoiceJob;
use App\Models\Media\MediaSecure;
use App\Services\Vendor\BaiduAi\ImageCensor;
use App\Services\Vendor\Miniprogram\Security;
use App\Services\Vendor\Xunfei\Voice2Text;
use Carbon\Carbon;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\File;
use Ixudra\Curl\Facades\Curl;

class AuditMedia implements ShouldQueue
{

    /**
     * Create the event listener.
     */
    public function __construct()
    {
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\AuditMedia  $event
     * @return void
     */
    public function handle(\App\Events\AuditMedia $event)
    {
        $media = $event->storage->updateOrCreate([
            'result' => 0,
            'uid' => $event->uid,
            'field' => $event->field,
            'field_primary' => $event->field_primary
        ], [
            'type' => $event->type,
            'origin' => $event->origin,
            'audit_suggestion' => 0,
        ]);

        switch ($media->type) {
            case 'text':
                $security = new Security();
                $result = $security->msgSecCheck($media->origin);
                switch ($result['errcode']) {
                    case 0:
                        $media->audit_suggestion = 1;
                        $media->result = 1;
                        $media->save();
                        break;
                    case 87014:
                        $media->machine_result = $result;
                        $media->audit_suggestion = -1;
                        $media->result = -1;
                        $media->save();
                        event(new AuditMediaDeal($media));
                        break;
                    default:
                        $media->audit_suggestion = 0;
                        $media->result = 0;
                        $media->save();
                        break;
                }
                break;
            case 'image':
                $contentSecure = new ImageCensor();
                $result = $contentSecure->image($media->origin);
                $media->machine_result = $result['machine_result'];
                $media->audit_suggestion = $result['suggestion'];
                $media->labels = $result['labels'];
                $media->save();
                switch ($result['suggestion']) {
                    case MediaSecure::MACHINE_SUGGESTION_REVIEW:
                        break;
                    case MediaSecure::MACHINE_SUGGESTION_PASS:
                        if ($media->field == 'headimgurl') {
                            $media->result = MediaSecure::MANUAL_SUGGESTION_PASS;
                            $media->save();
                        }
                        break;
                    case MediaSecure::MACHINE_SUGGESTION_BLOCK:
                        $media->result = MediaSecure::MANUAL_SUGGESTION_BLOCK;
                        $media->channel = MediaSecure::CHANNEL_MACHINE;
                        $media->save();
                        event(new AuditMediaDeal($media));
                        break;
                }
                break;
            case "voice":
                $url = $media->origin;

                // 文件下载
                $parseUrl = parse_url($url);
                $fileBasename = pathinfo($parseUrl['path'], PATHINFO_EXTENSION);
                $fileName = storage_path('app/' . uniqid() . '.' . $fileBasename);
                File::put($fileName, Curl::to($url)->get());
                $fileSize = filesize($fileName);
                $fileContent = fopen($fileName, 'r');
                File::delete($fileName);

                // 预处理
                $extData = [
                    'file_len' => $fileSize,
                    'file_name' => $fileName,
                    'slice_num' => 1,
                ];
                $XunFeiService = new Voice2Text();
                $data = $XunFeiService->prepare($extData);
                $taskId = $data['data'];
                $extData = [
                    'task_id' => $taskId,
                    'slice_id' => 'aaaaaaaaaa',
                    'content' => $fileContent
                ];
                // 上传
                $XunFeiService->upload($extData);
                // 合并
                $XunFeiService->merge($taskId);
                $media->machine_result = null;
                $media->translate = "【队列中】";
                $media->task_id = $taskId;
                $media->save();

                XunFeiVoiceJob::dispatch($media)
                    ->onQueue('{xunfei:voice}')
                    ->delay(Carbon::now()->addMinutes(1));
                break;
        }
    }
}
