package com.zcloud.common.handler;

import cn.hutool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcloud.common.utils.HttpRequestUtil;
import com.zcloud.common.utils.MD5;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author fangjiakai
 * @date 2023/10/19 10:44
 */
@ConfigurationProperties(prefix = "customer")
@Component
public class PlatVideoHandler {
    private String id;
    private String secret_key;
    private String url;
    private String domain;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSecret_key() {
        return secret_key;
    }

    public void setSecret_key(String secret_key) {
        this.secret_key = secret_key;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public PageUtils syncCurList(Map<String, Object> params) {
        HashMap<String, String> request = new HashMap<>();
        request.put("id", id);
        String key = MD5.md5(secret_key + domain);
        request.put("SECRET_KEY", key);
        if(params.get("name")!=null) request.put("KEYWORDS", params.get("name").toString());
        request.put("showCount", params.get("curPage").toString());
        request.put("currentPage", params.get("limit").toString());
        String _response = HttpRequestUtil.doPost(url + "/curriculum/getCurList", JSONObject.toJSONString(request));
        JSONObject responseResult = JSONObject.parseObject(_response);
        System.out.println(responseResult);
        Page page = new Page();
        page.setRecords(responseResult.getJSONArray("curriculumList"));
        page.setTotal(Long.parseLong(responseResult.getJSONObject("page").getString("totalResult")));
        page.setSize(Long.parseLong(params.get("limit").toString()));
        page.setCurrent(Long.parseLong(responseResult.getJSONObject("page").getString("currentPage")));
        page.setPages(Long.parseLong(responseResult.getJSONObject("page").getString("totalPage")));
        return new PageUtils(page);
    }

    public JSONObject syncCurInfo(Map<String, Object> params) {
        HashMap<String, String> request = new HashMap<>();
        request.put("CURRICULUM_ID", params.get("curriculum_id").toString());
        request.put("id", id);
        String key = MD5.md5(secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/curriculum/getCurInfo", JSONObject.toJSONString(request));
        JSONObject responseResult = JSONObject.parseObject(_response);
        System.out.println(responseResult);
        if(responseResult != null && responseResult.get("code") != null && StringUtils.isNotBlank(responseResult.get("code").toString()) && "0".equals(responseResult.get("code"))){
            return responseResult;
        } else {
            return null;
        }
    }

    public R syncVideoPlayAuth(Map<String, Object> video) {
        HashMap<String, String> request = new HashMap<>();
        request.put("id", this.id);
        request.put("videoId", video.get("videocourseware_id_remote").toString());
        request.put("curriculumId", video.get("curriculum_id_remote").toString());
        request.put("type", "0");
        String key = MD5.md5(secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getPlayInfo", JSONObject.toJSONString(request));
        Map response = JSONObject.parseObject(_response, Map.class);
        return R.ok().put("responseBody",response);
    }

    /**
     * description: 获取视频封面<p/>
     *
     * @throws Exception
     */
    public Map syncVideoInfo(Map video) {
        HashMap<String, String> request = new HashMap<>();
        request.put("id", id);
        request.put("videoId", video.get("videocourseware_id_remote").toString());
        request.put("curriculumId", video.get("curriculum_id_remote").toString());
        request.put("type", "0");
        String key = MD5.md5(secret_key + domain);
        request.put("SECRET_KEY", key);
        String _response = HttpRequestUtil.doPost(url + "/videoCourseware/getPlayUrl", JSONObject.toJSONString(request));
        Map response = JSONObject.parseObject(_response, Map.class);
        return response;
    }
}
