package thyyxxk.webserver.controller.socketmessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.auth.PassToken;
import thyyxxk.webserver.constants.Message;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.socketmessage.ApiMessageBody;
import thyyxxk.webserver.entity.socketmessage.MessageIndex;
import thyyxxk.webserver.entity.socketmessage.SocketMessage;
import thyyxxk.webserver.entity.socketmessage.SystemMessage;
import thyyxxk.webserver.service.externalhttp.WebSocketService;
import thyyxxk.webserver.service.socketmessage.SocketMessageService;
import thyyxxk.webserver.utils.SocketMsg;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/socketMessage")
public class SocketMessageController {
    private final SocketMessageService service;
    private final WebSocketService socketService;

    @Autowired
    public SocketMessageController(SocketMessageService service, WebSocketService socketService) {
        this.service = service;
        this.socketService = socketService;
    }

    @GetMapping("/selectSystemMessages")
    public ResultVo<List<SystemMessage>> selectSystemMessages(@RequestParam("date") String date) {
        return service.selectSystemMessages(date);
    }

    @PassToken
    @PostMapping("/onPageRefresh")
    public ResultVo<Map<String, Object>> onPageRefresh(@RequestBody List<String> exclude) {
        return service.onPageRefresh(exclude);
    }

    @GetMapping("/fetchAllSenders")
    public ResultVo<List<MessageIndex>> fetchAllSenders() {
        return service.fetchAllSenders();
    }

    @GetMapping("/fetchAllMessages")
    public ResultVo<List<SocketMessage>> fetchAllMessages(@RequestParam("sender") String sender) {
        return service.fetchAllMessages(sender);
    }

    @PassToken
    @PostMapping("/sendById")
    public String sendMessageById(@RequestBody CodeName msg) {
        String message = SocketMsg.socketVo(Message.MED_INS_FEE_UPLOAD_PROGRESS, msg.getName());
        ApiMessageBody messageBody = new ApiMessageBody(message);
        messageBody.setSid(msg.getCode());
        socketService.sendMessageBySid(messageBody);
        return "done";
    }
}
