<?php
namespace app\kernel\Lebolebo;

use GuzzleHttp\Client;
use think\facade\Cache;

class Curriculum
{
    protected $client;
    protected $headers = [
        'campusid'=>'1863',
        'logincode'=>'13880615958',
        'platform'=>'OMO_WEB',
        'referer'=>'http://manage.shengtongedu.cn/',
        'token'=>'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2ODE4MDAxMTcsInVzZXJDb2RlIjoiMTMwODgzOTM5MjcifQ.Wy61_ITLuag4JwYLhAOosEBz2Fsw-hCgGrrF-rgoQZM',
        'user-agent'=>'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Core/1.94.196.400 QQBrowser/11.7.5286.400',
        'content-type'=>'application/json; charset=utf-8',
        'origin'=>'http://manage.shengtongedu.cn',
    ];
    protected $config = [
        'cache_time'=>86400
    ];
    public function setHeader(string $key,string $value)
    {
        $this->headers[$key] = $value;
    }
    public function __construct()
    {
        $this->client = new Client([
            'timeout'  => 30,
        ]);
    }
    
    public function list(int $page = 0)
    {
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/coursePageListV3';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'pageNum'=>$page,
                'pageSize'=>200,
                'platform'=>"OMO_WEB",
                'total'=>101,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        return json_decode($result->getBody()->getContents(),true);
    }
    public function getVideoUrl($videoId)
    {
        $key = 'lebolebo_curriculum_getVideoUrl_' . $videoId;
        // if(($data = Cache::get($key,null)) !== null ){
        //     return $data;
        // }
        $url = 'https://api-manage2.shengtongedu.cn/st-msg-server/msg/bjy/getVideoUrl';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'companyId'=>'139',
                'videoId'=>$videoId,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
    }
    public function courseUnitList(string $courseCode)
    {
        $key = 'lebolebo_curriculum_courseUnitList_' . $courseCode;
        if(($data = Cache::get($key,null)) !== null ){
            return $data;
        }
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/unit/courseUnitList';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'courseCode'=>$courseCode,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
        
    }
    public function selectByCourseSessionCode(string $courseSessionCode)
    {
        $key = 'lebolebo_curriculum_selectByCourseSessionCode_' . $courseSessionCode;
        if(($data = Cache::get($key,null)) !== null ){
            return $data;
        }
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/resource/selectByCourseSessionCode';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'courseSessionCode'=>$courseSessionCode,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
    }
    public function selectByCourseUnits(string $courseCode,string $courseUnitCode)
    {
        $key = 'lebolebo_curriculum_selectByCourseUnits_' . $courseCode . $courseUnitCode;
        if(($data = Cache::get($key,null)) !== null ){
            return $data;
        }
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/sesson/selectByCourseUnits';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'courseCode'=>$courseCode,
                'courseUnitCode'=>$courseUnitCode
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
    }
    public function detail(int $id)
    {
        $key = 'lebolebo_curriculum_detail_' . $id;
        if(($data = Cache::get($key,null)) !== null ){
            return $data;
        }
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/detailV2';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'id'=>$id,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
    }
    public function course(string $code)
    {
        $key = 'lebolebo_curriculum_course_'.$code;
        if(($data = Cache::get($key,null)) !== null ){
            return $data;
        }
        $url = 'https://api-manage2.shengtongedu.cn/st-course-server/manage/course/unit/courseUnitList';
        $options = [
            'body'=>json_encode([
                'campusIdList'=>["1863"],
                'platform'=>"OMO_WEB",
                'courseCode'=>$code,
            ]),
            'headers'=>$this->headers
        ];
        $result = $this->client->post($url,$options);
        $d = json_decode($result->getBody()->getContents(),true);
        Cache::set($key,$d,$this->config['cache_time']);
        return $d;
    }
}