package com.zcloud.service.gateway.impl;

import com.alibaba.fastjson.JSONObject;
import com.zcloud.entity.PageData;
import com.zcloud.service.gateway.SynchronizeInformationService;
import com.zcloud.util.HttpRequestUtil;
import com.zcloud.util.MD5;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class SynchronizeInformationServiceImpl implements SynchronizeInformationService {

    @Value("${customer.id}")
    private String customerId;

    @Value("${customer.SECRET_KEY}")
    private String customer_secret_key;

    @Value("${customer.url}")
    private String url;

    @Value("${customer.domain}")
    private String domain;

    @Override
    public PageData syncVideoPlayAuth(PageData video) throws Exception {
//        PageData video = videoCoursewareService.findById(_video);
//        if (video == null || video.size() <= 0) {
//            throw new RuntimeException("未找到对应的视频");
//        }
        HashMap<String, String> request = new HashMap<>();
        request.put("id", this.customerId);
        request.put("videoId", video.getString("VIDEOCOURSEWARE_ID"));
        request.put("curriculumId", video.getString("CURRICULUM_ID"));
        request.put("type", "0");
        String key = MD5.md5(this.customer_secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getPlayInfo", JSONObject.toJSONString(request));
        PageData response = JSONObject.parseObject(_response, PageData.class);
        return response;
    }

    /**
     * description: 获取视频封面<p/>
     *
     * @throws Exception
     */
    @Override
    public PageData syncVideoInfo(PageData video) throws Exception {
//        PageData video = videoCoursewareService.findById(_video);
//        if (video == null || video.size() <= 0) {
//            throw new RuntimeException("未找到对应的视频");
//        }
        HashMap<String, String> request = new HashMap<>();
        request.put("id", this.customerId);
        request.put("videoId", video.getString("VIDEOCOURSEWARE_ID"));
        request.put("curriculumId", video.getString("CURRICULUM_ID"));
        request.put("type", "0");
        String key = MD5.md5(this.customer_secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getPlayUrl", JSONObject.toJSONString(request));
        PageData response = JSONObject.parseObject(_response, PageData.class);
        return response;
    }
}
