<?php

namespace App\Services\Vendor\Ali;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Green\Green;
use App\Services\Service;

class ContentSecure extends Service
{


    public const LABELS = [
        'normal' => '正常',
        'spam' => '含垃圾信息',
        'ad' => '广告',
        'politics' => '涉政',
        'terrorism' => '暴恐',
        'abuse' => '辱骂',
        'porn' => '色情',
        'flood' => '灌水',
        'contraband' => '违禁',
        'meaningless' => '无意义',
        'customized' => '自定义',
    ];


    public function __construct()
    {
        AlibabaCloud::accessKeyClient('LTAIlQc7W3hJyeDW', '2zdSpKMZNRHN6rXDofVh81CktjeENI')
            ->regionId('cn-hangzhou')->asDefaultClient();
    }


    public function text(string $content)
    {
//        $resp = Green::v20180509()->textScan()->jsonBody([
//            'scenes' => [
//                'antispam'
//            ],
//            'tasks' => [
//                [
//                    'dataId' => uniqid(),
//                    'content' => $content
//                ]
//            ]
//        ])->request()->toArray();

        $response = json_decode('{
            "code": 200,
            "data": [{
                "code": 200,
                "content": "あ꧁艹泥马꧂",
                "dataId": "11293950",
                "msg": "OK",
                "results": [{
                    "details": [{
                            "label": "abuse"
                        },
                        {
                            "label": "porn"
                        }
                    ],
                    "label": "porn",
                    "rate": 99.91,
                    "scene": "antispam",
                    "suggestion": "block"
                }],
                "taskId": "txt19HIKHevkVr4aukP0qjxbL-1rcAFR"
            }]
        }', true);
        if (200 == $response['code']) {
            $data = $response['data'][0];
            if (200 == $data['code']) {
                $results = $data['results'];
                $result = $results[0];

                // 审核建议
                $suggestion = $result['suggestion'];
                $rate = $result['rate'];
                $labels = [];
                if (isset($result['details'])) {
                    foreach ($result['details'] as $detail) {
                        array_push($labels, self::LABELS[$detail['label']]);
                    }
                }

                return array(
                    'suggestion' => $suggestion,
                    'rate' => $rate,
                    'labels' => $labels,
                    'machine_result' => $data,
                );
            } else {
                // 审核失败
                return false;
            }
        } else {
            // 请求失败
            return false;
        }
    }

    public function image(string $url)
    {
//        $resp = Green::v20180509()->imageSyncScan()->jsonBody([
//            'scenes' => [
//                'porn', 'terrorism', 'ad'
//            ],
//            'tasks' => [
//                [
//                    'dataId' => uniqid(),
//                    'url' => $url
//                ]
//            ]
//        ])->request()->toArray();

        $resp = json_decode('{
"code": 200,
"data": [
{
  "code": 200,
  "dataId": "5d6c8d5e81f7a",
  "msg": "OK",
  "results": [
    {
      "extras": {
        "newUrl": "http://aligreen-shanghai.oss-cn-shanghai-internal.aliyuncs.com/prod/hammal/02242bf59/12038463_419166f4655fe69dbe9f3502fb767a39.jpg?Expires=1567396966&OSSAccessKeyId=H4sp5QfNbuDghquU&Signature=g1Vt8V2aeoQVd319ZEOzf4tR8zo%3D"
      },
      "label": "normal",
      "rate": 98.75,
      "scene": "porn",
      "suggestion": "pass"
    },
    {
      "label": "log",
      "rate": 99.99,
      "scene": "terrorism",
      "suggestion": "pass"
    },
    {
      "label": "normal",
      "rate": 99.9,
      "scene": "ad",
      "suggestion": "pass"
    }
  ],
  "taskId": "img2Vim0@z9r6g5GbnKC7$Iqz-1rf08e",
  "url": "https://oss.pocketuniversity.cn/headimg/419166f4655fe69dbe9f3502fb767a39.jpg"
}
]
}', true);

        if (200 == $resp['code']) {
            $data = $resp['data'][0];
            if (200 == $data['code']) {
                $results = $data['results'];
                $suggestion = "normal";
                $labels = array();
                foreach ($results as $result) {
                    if ('block' == $result['suggestion']) {
                        $suggestion = "block";
                    }
                    $labels[$result['scene']] = $result['label'];
                }

                return array(
                    'suggestion' => $suggestion,
                    'labels' => $labels,
                    'result' => $resp,
                );
            } else {
                // 审核失败
                return false;
            }
        } else {
            // 请求失败
            return false;
        }

        return $return;
    }

    public function voice($tasks)
    {
        $result = Green::v20180509()->voiceAsyncScan()->jsonBody([
            'scenes' => ['antispam'],
            'tasks' => $tasks,
        ])->request()->toArray();
        $return = array();
        if (200 == $result['code']) {
            $datas = $result['data'];
            foreach ($datas as $data) {
                if (200 == $data['code']) {
                    array_push($return, [
                        'dataId' => $data['dataId'],
                        'url' => "任务id",
                        'label' => $data['taskId'],
                    ]);
                }
            }
        }

        return $return;
    }

    public function getVoice()
    {
        $result = Green::v20180509()->voiceAsyncScanResults()->jsonBody([
            'vc_f_6kK6yjzH4PF4bmHnnHGges-1rbRbb',
            'vc_f_6o0s2g5pA$S6LyQJq7X6tv-1rbRbb',
        ])->request()->toArray();
        dump($result);
    }
}
