package com.ruoyi.hwMeeting.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dahuatech.hutool.json.JSONTokener;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.hwMeeting.util.hwMeetingCacheUtil;
import com.ruoyi.hwMeeting.service.HwMeetingService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * 华为云会议控制器
 */
@Controller
@RequestMapping("/hwMeeting")
public class HwMeetingController {

    private String prefix = "hwMeeting/meeting";

    @Resource
    private HwMeetingService hwMeetingService;

    /**
     * 创建或加入会议
     */
    @GetMapping("/joinConferences/{eventId}/{subject}/{nickName}")
    public String hasConferences(@PathVariable(name = "eventId") String eventId,
                                     @PathVariable(name = "subject") String subject,
                                     @PathVariable(name = "nickName") String nickName,
                                     ModelMap mmap) {
        if (hwMeetingCacheUtil.has(eventId)) {
            Object ob = hwMeetingCacheUtil.get(eventId);
            //ob转换为JSONObject 取来宾入会信息返回
            JSONObject object = JSONUtil.parseObj(ob);
            //取密码数组 passwordEntry
            JSONArray array = JSONUtil.parseArray(object.get("passwordEntry"));
            for (Object o : array) {
                JSONObject password = JSONUtil.parseObj(o);
                JSONObject generalInfo = new JSONObject();
                if (password.getStr("conferenceRole").equals("general")) {
                    generalInfo.set("conferenceID", object.getStr("conferenceID"));
                    generalInfo.set("password", password.getStr("password"));
                    mmap.put("info",generalInfo);
                }
            }
            mmap.put("nickName",nickName);
            return prefix;
        }else{
            this.createConferences(eventId,subject,mmap);
            mmap.put("nickName",nickName);
            return prefix;
        }
    }

    /**
     * 创建会议
     */
    public boolean createConferences(String eventId,String subject,ModelMap mmap) {
        String result = hwMeetingService.conferences(subject);
        if (isSuccess(result)) {
            /**将会议信息保存到数据库**/
            //将结果转换为json数组，当处理成功时返回的格式为数组
            Object ob = JSONUtil.parseArray(result).get(0);
            //会议信息存入缓存
            hwMeetingCacheUtil.put(eventId,JSONUtil.parseObj(ob));
            //ob转换为JSONObject 取主持人入会信息返回
            JSONObject object = JSONUtil.parseObj(ob);
            //取密码数组 passwordEntry
            JSONArray array = JSONUtil.parseArray(object.get("passwordEntry"));
            for (Object o : array) {
                JSONObject password = JSONUtil.parseObj(o);
                JSONObject chairInfo = new JSONObject();
                if (password.getStr("conferenceRole").equals("chair")) {
                    chairInfo.set("conferenceID", object.getStr("conferenceID"));
                    chairInfo.set("password", password.getStr("password"));
                    mmap.put("info",chairInfo);
                }
            }
            return true;
        }
        return false;
    }

    /**
     * 获取加入会议URI
     */
    @ResponseBody
    @GetMapping("/guestJoinUri/{eventId}")
    public AjaxResult guestJoinUri(@PathVariable(name = "eventId") String eventId) {
        Object ob = hwMeetingCacheUtil.get(eventId);
        if(ob != null){
            JSONObject object = JSONUtil.parseObj(ob);
            return AjaxResult.success(object.get("guestJoinUri"));
        }
        return AjaxResult.error("未查询到数据");
    }

    /**
     * 判断返回数据是JSONArray还是JSONObject，
     * 如果是Array 则表示成功
     * 反之失败
     * */
    private boolean isSuccess(String result) {
        Object object = new JSONTokener(result).nextValue();
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            String error_code = jsonObject.get("error_code").toString();
            System.err.println(error_code);
            return false;
        }
        return true;
    }

}
