package com.ym.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.ym.dao.HereWhiteDao;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.util.http.HttpUtil;
import com.ym.pojo.HereWhite;
import com.ym.service.HereWhiteService;
import com.ym.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class HereWhiteServiceImpl implements HereWhiteService {

    @Value("${cn.rongcloud.hereWhite.url}")
    private String hereWhiteUrl;
    @Value("${cn.rongcloud.hereWhite.token}")
    private String hereWhiteToken;

    @Autowired
    private HereWhiteDao hereWhiteDao;
    @Autowired
    private RoomService roomService;

    @Override
    @Transactional(rollbackFor = Exception.class,isolation = Isolation.READ_COMMITTED)
    public HereWhite create(String name, Integer userNum,Integer courseScheduleId) throws Exception {
        courseScheduleId = roomService.getCurrentCourseId(courseScheduleId.toString());
        JSONObject json = new JSONObject();
        json.put("name",name);
        json.put("limit",userNum);
        json.put("mode","persistent");
        String url = "/room?token=" + hereWhiteToken;
        JSONObject jsonObject = JSONObject.parseObject(requestParam(json, url));
        if(jsonObject.getString("code").equals("200")){
            HereWhite hereWhite = hereWhiteDao.findByCourseScheduleId(courseScheduleId);
            if(hereWhite == null){
                JSONObject room = jsonObject.getJSONObject("msg").getJSONObject("room");
                hereWhite = new HereWhite();
                Date date = new Date();
                hereWhite.setCourseScheduleId(courseScheduleId);
                hereWhite.setName(room.getString("name"));
                hereWhite.setLimit(room.getInteger("limit"));
                hereWhite.setTeamId(room.getInteger("teamId"));
                hereWhite.setAdminId(room.getInteger("adminId"));
                hereWhite.setMode(room.getString("mode"));
                hereWhite.setTemplate(room.getString("template"));
                hereWhite.setRegion(room.getString("region"));
                hereWhite.setUuid(room.getString("uuid"));
                hereWhite.setRoomToken(jsonObject.getJSONObject("msg").getString("roomToken"));
                hereWhite.setUpdatedAt(date);
                hereWhite.setCreatedAt(date);
                hereWhiteDao.save(hereWhite);
            }
            return hereWhite;
        }else {
            throw new BizException(jsonObject.getString("msg"));
        }
    }

    @Override
    public HereWhite getByClassId(Integer courseScheduleId) {
        return hereWhiteDao.findByCourseScheduleId(courseScheduleId);
    }

    private String requestParam(JSONObject json,String url) throws Exception {
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Content-Type","application/json");
        return HttpUtil.postForHttps(hereWhiteUrl + url, json.toJSONString(), headers);
    }
}
