package cn.edu.whu.boot.message.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import org.springframework.web.util.HtmlUtils;
import cn.edu.whu.boot.message.bean.RequestMessage;
import cn.edu.whu.boot.message.bean.ResponseMessage;


import java.util.ArrayList;
import java.util.List;


/**
 * @author Li Haoming
 */
@Controller
public class GreetingController {

    private final List<String> rooms = new ArrayList<>();

    private List<String> messages = new ArrayList<>();

    @Autowired
    private SimpMessagingTemplate template;

//    @MessageMapping("/hello/{roomId}")
//    @SendTo("/topic/greetings/{roomId}")
//    public ResponseMessage greeting(RequestMessage message, @DestinationVariable String roomId) throws Exception{
//        System.out.println(roomId);
//        rooms.add(roomId);
//        System.out.println("Hello " + message.getName());
//        return new ResponseMessage("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
//    }

    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public ResponseMessage greeting(RequestMessage message) throws Exception{
        System.out.println("Hello " + message.getName());
        messages.add("Hello " + message.getName());
        return new ResponseMessage("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }


}
