package org.kurento.room.demo;
import java.util.HashMap;
import java.util.Map;

import org.kurento.commons.PropertiesManager;
import org.kurento.room.NotificationRoomManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

/**
 * Rest controller for the room demo app.
 *
 * @author Radu Tom Vlad (rvlad@naevatec.com)
 * @since 6.0.0
 */
@RestController
public class DemoController {

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

	private final static boolean DEMO_LOOPBACK_REMOTE = PropertiesManager.getProperty("demo.loopback.remote", false);
	private final static boolean DEMO_LOOPBACK_AND_LOCAL = PropertiesManager.getProperty("demo.loopback.andLocal",
			false);

	private static ClientConfig config;

	static {
		config = new ClientConfig();
		config.setLoopbackRemote(DEMO_LOOPBACK_REMOTE);
		config.setLoopbackAndLocal(DEMO_LOOPBACK_AND_LOCAL);
		log.info("Set client config: {}", config);
	}

	@Autowired
	private NotificationRoomManager roomManager;

	@ResponseStatus(value = HttpStatus.NOT_FOUND)
	public class ResourceNotFoundException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		public ResourceNotFoundException(String msg) {
			super(msg);
		}
	}

	@RequestMapping(value = "/room/close", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
	public @ResponseBody Map<String, Object> closeRoom(@RequestParam Map<String, Object> data) {
		Map<String, Object> map = new HashMap<String, Object>();
		String roomId = data.get("roomName").toString();
//		String uid = data.get("uid").toString();

		log.warn("Trying to close the room '{}'", roomId);
		if (!roomManager.getRooms().contains(roomId)) {
			map.put("code", "200");
			return map;
		}
		//TODO 此处应该验证是不是创建者
		roomManager.closeRoom(roomId);

		map.put("code", "200");
		return map;
	}
	
	@RequestMapping("/getClientConfig")
	public ClientConfig clientConfig() {
		log.debug("Sending client config {}", config);
		return config;
	}
}
