package com.ruoyi.hwMeeting.service.impl;

import com.dahuatech.hutool.core.util.RandomUtil;
import com.dahuatech.hutool.http.HttpRequest;
import com.dahuatech.hutool.json.JSONArray;
import com.dahuatech.hutool.json.JSONObject;
import com.dahuatech.hutool.json.JSONUtil;
import com.ruoyi.config.service.IMeetingConfigService;
import com.ruoyi.hwMeeting.service.HwMeetingService;
import com.ruoyi.hwMeeting.util.HmacSHA256;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HwMeetingServiceImpl implements HwMeetingService {

    private static final Logger log = LoggerFactory.getLogger(HwMeetingServiceImpl.class);

    @Autowired
    private IMeetingConfigService meetingConfigService;

    /**
     * appId鉴权方法 获取token
     */
    @Override
    public String appauth() {
        String appId = meetingConfigService.selectMeetingConfigById((long) 2).getVal();
        String userId = meetingConfigService.selectMeetingConfigById((long) 4).getVal();
        String url = meetingConfigService.selectMeetingConfigById((long) 1).getVal();
        String appauth = meetingConfigService.selectMeetingConfigById((long) 5).getVal();
        //64位随机字符串
        String nonce = RandomUtil.randomString(64);
        log.info("nonce:{}", nonce);
        //鉴权信息
        String authorization = getAuthorization(appId, userId, nonce);
        log.info("authorization:{}", authorization);
        //body
        JSONObject json = new JSONObject();
        json.put("appId", appId);
        json.put("expireTime", 0);
        json.put("nonce", nonce);
        json.put("userId", userId);
        json.put("clientType", 72);
        //请求结果
        String result = HttpRequest.post(url + appauth)
                .header("Authorization", authorization)
                .header("Content-Type", "application/json")
                .body(json)
                .execute().body();
        //打印
        log.info("result:{}", result);
        return JSONUtil.parseObj(result).get("accessToken").toString();
    }

    /**
     * 构成Authorization
     */
    @Override
    public String getAuthorization(String appId, String userId, String nonce) {
        String appKey = meetingConfigService.selectMeetingConfigById((long) 3).getVal();
        String data = appId + ":" + userId + ":" + 0 + ":" + nonce;
        String authorization = "HMAC-SHA256 signature=" + HmacSHA256.encode(data, appKey);
        return authorization;
    }

    /**
     * 绑定给当前创会帐号的VMR_ID。通过查询云会议室及个人会议ID接口获取。
     */
    @Override
    public String vmrID(String token) {
        String url = meetingConfigService.selectMeetingConfigById((long) 1).getVal();
        String vmr = meetingConfigService.selectMeetingConfigById((long) 6).getVal();
        //body
        String result = HttpRequest.get(url + vmr)
                .header("X-Access-Token", token)
                .header("Content-Type", "application/json")
                .execute().body();
        log.info("vmr：{}", result);
        JSONArray rooms = JSONUtil.parseArray(JSONUtil.parseObj(result).get("data"));
        log.info("rooms.get(0)：{}", rooms.get(0));
        log.info("JSONUtil.parseObj(rooms.get(0))：{}", JSONUtil.parseObj(rooms.get(0)));
        log.info("vmrID：{}", JSONUtil.parseObj(rooms.get(0)).get("vmrId"));
        return JSONUtil.parseObj(rooms.get(0)).get("id").toString();
    }

    /**
     * 创建会议
     */
    @Override
    public String conferences(String subject) {
        String url = meetingConfigService.selectMeetingConfigById((long) 1).getVal();
        String conferences = meetingConfigService.selectMeetingConfigById((long) 7).getVal();
        String token = appauth();
        String vmrID = vmrID(token);
        //body
        JSONObject json = new JSONObject();
        /**会议的媒体类型。Voice：语音会议,HDVideo：视频会议**/
        json.put("mediaTypes", "HDVideo");
        /**
         * 会议持续时长，单位分钟。默认30分钟。
         * 最大1440分钟（24小时），最小15分钟。
         * */
        json.put("length", 15);
        /**会议主题。最多128个字符。**/
        json.put("subject", subject);
        /**
         * 是否使用云会议室或者个人会议ID召开预约会议。默认0。
         * 0：不使用云会议室或者个人会议ID
         * 1：使用云会议室或者个人会议ID
         * */
        json.put("vmrFlag", 1);
        /**绑定给当前创会帐号的VMR ID。通过查询云会议室及个人会议ID接口获取。**/
        json.put("vmrID", vmrID);
        //请求
        String result = HttpRequest.post(url + conferences)
                .header("X-Access-Token", token)
                .header("Content-Type", "application/json")
                .body(json)
                .execute().body();
        log.info("创建会议：{}", result);
        return result;
    }
}
