package ink.wgink.module.instantmessage.controller.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.instantmessage.pojo.bos.NoticeBO;
import ink.wgink.module.instantmessage.pojo.bos.NoticeTargetBO;
import ink.wgink.module.instantmessage.pojo.vos.MessageVO;
import ink.wgink.module.instantmessage.pojo.vos.NoticeSendVO;
import ink.wgink.module.instantmessage.service.IMessageService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MessageController
 * @Description: 消息
 * @Author: wanggeng
 * @Date: 2021/1/14 3:25 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "消息接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/websocket/message")
public class WebSocketMessageController extends DefaultBaseController {

    @Autowired
    private IMessageService messageService;

    @ApiOperation(value = "通知消息", notes = "通知消息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("notice")
    @CheckRequestBodyAnnotation
    public SuccessResult notice(@RequestBody NoticeSendVO noticeSendVO) throws Exception {
        messageService.notice(noticeSendVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "通知消息", notes = "通知消息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("notice/{clientName}")
    @CheckRequestBodyAnnotation
    public SuccessResult notice(@PathVariable("clientName") String clientName, @RequestBody MessageVO messageVO) {
        NoticeBO noticeBO = new NoticeBO();
        noticeBO.setTitle(messageVO.getTitle());
        noticeBO.setContent(messageVO.getContent());
        noticeBO.setTo(messageVO.getTo());
        messageService.notice(clientName, noticeBO);
        return new SuccessResult();
    }

    @ApiOperation(value = "通知消息", notes = "通知消息接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("notice-target/{clientName}")
    @CheckRequestBodyAnnotation
    public SuccessResult saveNoticeWithTargetAndSave(@PathVariable("clientName") String clientName, @RequestBody MessageVO messageVO) {
        NoticeTargetBO noticeTargetBO = new NoticeTargetBO();
        noticeTargetBO.setTitle(messageVO.getTitle());
        noticeTargetBO.setContent(messageVO.getContent());
        noticeTargetBO.setTo(messageVO.getTo());
        noticeTargetBO.setTarget(messageVO.getTarget());
        messageService.notice(clientName, noticeTargetBO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除通知", notes = "删除通知接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceIds", value = "业务ID", paramType = "path"),
    })
    @DeleteMapping("delete/{serviceIds}")
    public SuccessResult delete(@PathVariable("serviceIds") String serviceIds) {
        messageService.delete(Arrays.asList(serviceIds.split("\\_")));
        return new SuccessResult();
    }

}
