<?php

namespace App\Http\Controllers\Api;

use App\Members;
use App\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class GarbageController extends Controller
{
    public function index(Request $request)
    {
        $client = new Client();
        $access_token = Cache::get('access_token01', function () use ($client) {
            $apikey = '8gflcNCZDnH6juetHLsa37HZ';
            $secret_key = 'bstrmGqZMiB6FFzmgkC0mwf8P45frGkv';
            $response = $client->get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$apikey}&client_secret={$secret_key}", [
                'verify' => false,
            ]);

            $result = json_decode($response->getBody()->getContents(), true);

            Cache::put('access_token01', $result['access_token'], 2591000);

            return $result['access_token'];
        });

        $response = $client->post('https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=' . $access_token, [
            'verify' => false,
            'json' => [
                'scenes' => [
                    'advanced_general'
                ],
                'image' => $request->input('image')
            ]
        ]);

        $result = json_decode($response->getBody()->getContents(), true);
        /**
         * "score" => 0.45869
         * "root" => "商品-香烟"
         * "keyword" => "玉溪香烟"
         */
        //$result['result']['advanced_general']['result'];
        $data = [];
        if (isset($result['result'])) {
            $results = $result['result']['advanced_general']['result'];
            $data = [];
            foreach ($results as $res) {
                $response1 = $client->get('https://trash.lhsr.cn/sites/feiguan/trashTypes_2/Handler/Handler.ashx?a=GET_KEYWORDS&kw='.$res['keyword'], [
                    'verify' => false
                ]);

                $resi = json_decode($response1->getBody()->getContents(), true);
                if ($resi['query_result'] == -1 && !empty($resi['kw_arr'])) {
                    $item = [
                        'data' => $resi['kw_arr'][0]['TypeKey'],
                        'name' => $resi['kw_arr'][0]['Name']
                    ];
                    $data[] = $item;
                }
            }
        }

        if (empty($data)) {
            return $this->fail(500, '接口异常');
        }

        return $this->ok($data);
    }
    public function index01(Request $request)
    {
        $client = new Client();
        $access_token = Cache::get('access_token01', function () use ($client) {
            $apikey = '8gflcNCZDnH6juetHLsa37HZ';
            $secret_key = 'bstrmGqZMiB6FFzmgkC0mwf8P45frGkv';
            $response = $client->get("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={$apikey}&client_secret={$secret_key}", [
                'verify' => false,
            ]);

            $result = json_decode($response->getBody()->getContents(), true);

            Cache::put('access_token01', $result['access_token'], 2591000);

            return $result['access_token'];
        });

        $response = $client->post('https://aip.baidubce.com/api/v1/solution/direct/imagerecognition/combination?access_token=' . $access_token, [
            'verify' => false,
            'json' => [
                'scenes' => [
                    'advanced_general'
                ],
                'image' => $request->input('image')
            ]
        ]);

        $result = json_decode($response->getBody()->getContents(), true);
        /**
         * "score" => 0.45869
         * "root" => "商品-香烟"
         * "keyword" => "玉溪香烟"
         */
        //$result['result']['advanced_general']['result'];
        $data = [];
        if (isset($result['result'])) {
            $results = $result['result']['advanced_general']['result'];
            $data = [];
            foreach ($results as $res) {
                $response1 = $client->get('https://trash.lhsr.cn/sites/feiguan/trashTypes_2/Handler/Handler.ashx?a=GET_KEYWORDS&kw='.$res['keyword'], [
                    'verify' => false
                ]);

                $resi = json_decode($response1->getBody()->getContents(), true);
                if ($resi['query_result'] == -1 && !empty($resi['kw_arr'])) {
                    $item = [
                        'data' => '物品名字',
                        'name' => $resi['kw_arr'][0]['Name']
                    ];
                    $data[] = $item;
                }
            }
        }

        if (empty($data)) {
            return $this->fail(500, '接口异常');
        }

        return $this->ok($data);
    }

    public function text(Request $request)
    {
        $client = new Client();
        $response = $client->get('https://trash.lhsr.cn/sites/feiguan/trashTypes_2/Handler/Handler.ashx?a=GET_KEYWORDS&kw='.$request->input('keyword'), [
            'verify' => false
        ]);
        $data = [];
        $res = json_decode($response->getBody()->getContents(), true);
        if ($res['query_result'] == -1) {
            $item = [
                'data' => $res['kw_arr'][0]['TypeKey'],
                'name' => $res['kw_arr'][0]['Name']
            ];
            $data[] = $item;
        }
        //这里对用户进行积分
        $user = Members::query()->find($request->input('user_id'))->toArray();
        Members::query()->find($request->input('user_id'))->update(['points' => $user['points']+5]);
        return $this->ok($data);
    }

}
