<?php

namespace Modules\Video\Services;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use Modules\Common\Errors\Errors;
use Modules\Common\Exceptions\CommonBusinessException;
use Modules\Manage\Services\WechatMiniprogramService;


class WechatCheckSyncService
{
    /**
     * 拉取上传
     * @param $item 剧集信息
     * @param $accessToken 小程序accessToken
     * @return  string task_id
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function pullupload($item, $accessToken) {
        $url = 'https://api.weixin.qq.com/wxa/sec/vod/pullupload?access_token='.$accessToken;
        $parsedContent = self::postWechat($url, [
            'media_name' => $item->media_name,
            'media_url' => $item->video_url
        ]);
        return $parsedContent['task_id'];
    }

    /**
     * 获取微信短剧剧集的播放链接信息
     * @param $seriesId
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function getMedialinkInfo($seriesId) {
        $syncInfo = DB::table('video_series_wechat_check')
            ->where([
                'series_id'=> $seriesId,
                'sync_status' => 3,
                'is_enabled' => 1,
            ])->first();

        if(!$syncInfo || !$syncInfo->media_id) {
            CommonBusinessException::throwError(Errors::SYNC_WECHAT_NOT_OK);
        }
        $mediaId = $syncInfo->media_id;
        $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);
        $url = 'https://api.weixin.qq.com/wxa/sec/vod/getmedialink?access_token='.$accessToken;

        $parsedContent = self::postWechat($url, [
            'media_id' => $mediaId,
            't' => time() + 7200,
        ]);
        return $parsedContent['media_info'];
    }

    public static function getWechatMediaLinkRedisKey($seriesId) {
        return 'wechat.medialink.'.$seriesId;
    }

    public static function getTask($syncInfo) {
        try {
            $accessToken = WechatMiniprogramService::getDuanjuCheckAccessToken($syncInfo->appid);
            $url = 'https://api.weixin.qq.com/wxa/sec/vod/gettask?access_token='.$accessToken;
            $parsedContent = self::postWechat($url, [
                'task_id' => $syncInfo->sync_task_id
            ]);
            return $parsedContent['task_info'];
        } catch (\Exception $exception) {
            return [];
        }

    }

    /**
     *  post 请求微信上游
     * @param $url
     * @param $data
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function postWechat($url, $data) {
        $client = new Client(['timeout' => 3]);
        $httpResult = $client->post($url, $data);

        $httpStatus = $httpResult->getStatusCode();
        if(200 != $httpStatus) {
            CommonBusinessException::throwError(Errors::REQUEST_HTTP_STATUS_ERROR);
        }
        $httpContent = $httpResult->getBody()->getContents();
        $parsedContent = \json_decode($httpContent, true);
        if(0 != ($parsedContent['errcode'] ?? 0)) {
            myLog('WechatCheckSync')->error('请求微信异常', [
                'url' => $url,
                'data' => $data,
                'errMsg' => $httpContent
            ]);
            CommonBusinessException::throwError(Errors::REQUEST_CODE_STATUS_ERROR);
        }

        return $parsedContent;
    }
}
