package com.m2pool.chat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.m2pool.chat.dto.ChatMessageDto;
import com.m2pool.chat.entity.ChatMessage;
import com.m2pool.chat.entity.ChatRoom;
import com.m2pool.chat.mapper.ChatMessageHistoryMapper;
import com.m2pool.chat.mapper.ChatMessageMapper;
import com.m2pool.chat.mapper.ChatRoomMapper;
import com.m2pool.chat.service.ChatMessageService;
import com.m2pool.common.core.Result.R;
import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.common.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.m2pool.chat.constant.UserType.*;

@Service
public class ChatMessageServiceImpl implements ChatMessageService {

    @Resource
    private ChatMessageMapper chatMessageMapper;

    @Resource
    private ChatMessageHistoryMapper chatMessageHistoryMapper;

    @Resource
    private ChatRoomMapper chatRoomMapper;

    @Override
    public R<List<ChatMessageDto>>
    findHistoryMessage(Long id,Integer pageNum,Long roomId) {
        String email = SecurityUtils.getUsername();
        List<ChatMessageDto> messages;
        messages = chatMessageMapper.findRecentlyMessage(email,id, pageNum, roomId);
        return R.success(messages);
    }

    @Override
    public R<List<ChatMessageDto>> findRecentlyMessage(String email,Long id,Integer pageNum,Long roomId) {
        ChatMessage chatMessage;
        if(StringUtils.isEmpty(email)){
            return R.fail("查询失败,用户标识或邮箱不能为空");
        }
        if(roomId == null){
            return R.fail("查询聊天消息失败,聊天室ID不能为空");
        }
        //判断当前消息是否是七天内消息
        if(id != null && id != 0){
            chatMessage = chatMessageMapper.selectById(id);
        }else{
            chatMessage = chatMessageMapper.selectOne(new LambdaQueryWrapper<ChatMessage>()
                    .eq(ChatMessage::getRoomId, roomId).orderByDesc(ChatMessage::getId).last("LIMIT 1"));
        }
        List<ChatMessageDto> recentlyMessage;
        if(chatMessage != null){
            recentlyMessage  = chatMessageMapper.findRecentlyMessage(email,id, pageNum, roomId);
            if(recentlyMessage.size() != pageNum){
                recentlyMessage.addAll(chatMessageHistoryMapper.findHistoryMessage(email, id, pageNum - recentlyMessage.size(), roomId));
            }
        }else{
            recentlyMessage = chatMessageHistoryMapper.findHistoryMessage(email, id, pageNum, roomId);
        }

        return R.success(recentlyMessage);
    }

    @Override
    public R<String> deleteMessage(Long id,Integer type) {
        if(chatMessageMapper.deleteById(id) > 0){
            //TODO 对象图片存储删除
            return R.success("删除成功");
        }
        return R.fail("删除失败");
    }


    @Override
    @Transactional
    public R<String> readMessage(Long roomId, Integer type) {
        ChatRoom chatRoom = chatRoomMapper.selectById(roomId);
        ChatRoom build = ChatRoom.builder()
                .id(roomId)
                .build();
        if (chatRoom != null){
            if (Objects.equals(type, LOGIN_USER) || Objects.equals(type, TOURIST) ) {
                build.setClientReadNum(0);
            }
            if(Objects.equals(type, CUSTOMER)){
                build.setServiceReadNum(0);
            }
            int i = chatRoomMapper.updateById(build);
            return i >= 0 ?  R.success("已读") : R.fail("消息读取失败");
        }
        return R.fail("聊天室不存在");
    }
}
