package com.webchat.controller.client;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.helper.SessionHelper;
import com.webchat.config.annotation.ValidateLogin;
import com.webchat.domain.vo.request.mess.ChatMessageRequestVO;
import com.webchat.domain.vo.response.mess.ChatMessageResponseVO;
import com.webchat.domain.vo.response.mess.UserMessListResponseVO;
import com.webchat.service.ChatMessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 程序员七七
 * @webSite https://www.coderutil.com
 * @Date 2022/12/10 22:29
 * @description
 */
@RestController
@RequestMapping("/api/chat/mess")
public class ChatMessController {

    @Autowired
    private ChatMessService chatMessService;

    /***
     * 发送消息
     * @return
     */
    @ValidateLogin
    @PostMapping("/send")
    public APIResponseBean sendMessage(@RequestBody ChatMessageRequestVO message) {
        if (StringUtils.isBlank(message.getSenderId())) {
            String currUserId = SessionHelper.getCurrentUserId();
            message.setSenderId(currUserId);
        }
        return APIResponseBeanUtil.success(chatMessService.sendMess(message));
    }

    /***
     * 获取未读消息人数
     * @return
     */
    @ValidateLogin
    @GetMapping("/unReadUserCount")
    public APIResponseBean<Long> getUnReadUserCount() {
        String currUserId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(chatMessService.getUnreadMessUserCountFromCache(currUserId));
    }

    /***
     * 查询跟用户聊天记录
     * @return
     */
    @ValidateLogin
    @GetMapping("/query/{chatUserId}/{size}")
    public APIResponseBean<List<ChatMessageResponseVO>> query(@PathVariable String chatUserId,
                                                              @PathVariable Integer size,
                                                              @RequestParam(value = "lastId", required = false) Long lastId,
                                                              @RequestParam(value = "fixedMessageId", required = false) Long fixedMessageId) {
        String currUserId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(
                chatMessService.getChatMessListFromCache(currUserId, chatUserId, lastId, fixedMessageId, size));
    }

    /***
     * 查询跟用户聊天记录
     * @return
     */
    @ValidateLogin
    @GetMapping("/users")
    public APIResponseBean<List<UserMessListResponseVO>> userList(
            @RequestParam(value = "selectUser", required = false) String selectUser) {
        String currUserId = SessionHelper.getCurrentUserId();
        return APIResponseBeanUtil.success(chatMessService.getMessUserList(selectUser, currUserId, null, 100));
    }
}
