package org.jeecg.modules.ai.service.impl;

import java.io.StringReader;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.annotation.Resource;

import org.jeecg.modules.ai.entity.Room;
import org.jeecg.modules.ai.entity.RoomHideElement;
import org.jeecg.modules.ai.mapper.RoomMapper;
import org.jeecg.modules.ai.service.IRoomHideElementService;
import org.jeecg.modules.ai.service.IRoomService;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.system.entity.SysAnnouncement;
import org.jeecg.modules.system.entity.SysAnnouncementSend;
import org.jeecg.modules.system.service.ISysAnnouncementSendService;
import org.jeecg.modules.system.service.ISysAnnouncementService;
import org.jeecg.modules.system.util.WebServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.http.webservice.SoapClient;
import cn.hutool.http.webservice.SoapUtil;

/**
 * @Description: 房间表
 * @Author: jeecg-boot
 * @Date:   2023-11-30
 * @Version: V1.0
 */
@Service
@DS("multi-datasource1")
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
	
	@Resource
	private RoomMapper roomMapper;
	@Value(value = "${openmeetings_url}")
	private String OPENMEETINGS_URL;
	@Autowired
	private IRoomHideElementService iRoomHideElementService;
	
	/**
	 * 创建房间并返回房间地址
	 * @return
	 */
	public JSONObject saveRoom(String userId) {
		Room room = new Room();
		room.setName("远程协助"+userId);
		room.setDeleted(0);
		room.setInserted(new Date());
		room.setAllowRecording(1);
		room.setAllowUserQuestions(0);
		room.setAppointment(0);
		room.setAudioOnly(1);
		room.setCapacity(2L);
		room.setChatOpened(0);
		room.setClosed(0);
		room.setDemoRoom(0);
		room.setFilesOpened(0);
		room.setIspublic(1);
		room.setModerated(0);
		room.setSipEnabled(0);
		room.setType("CONFERENCE");
		room.setWaitForRecording(1);
		room.setWaitModerator(0);
		room.setChatModerated(0);
		roomMapper.insert(room);
		
		RoomHideElement roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("ACTION_MENU");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("FILES");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("WHITEBOARD");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("POLL_MENU");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("USER_COUNT");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("CHAT");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		roomHideElement = new RoomHideElement();
		roomHideElement.setHideElement("ACTIVITIES");
		roomHideElement.setRoomId(room.getId());
		iRoomHideElementService.save(roomHideElement);
		//生成房间地址
		String result = WebServiceUtil.send("请求人", room.getId(), OPENMEETINGS_URL);
		String roomUrl = OPENMEETINGS_URL + "/openmeetings/hash?secure=" + JSONObject.parseObject(result).getString("message");
		JSONObject json = new JSONObject();
		json.put("roomUrl", roomUrl);
		json.put("roomId", room.getId());
		return json;
	}
}
