package com.wzj.soopin.content.service.impl;


import com.wzj.soopin.content.domain.base.BaseInfoProperties;
import com.wzj.soopin.content.domain.mo.MessageMO;
import com.wzj.soopin.content.enums.MessageEnum;
import com.wzj.soopin.content.mapper.repository.MessageRepository;
import com.wzj.soopin.content.service.MsgService;
import com.wzj.soopin.member.domain.po.Member;
import com.wzj.soopin.member.mapper.MemberMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class MsgServiceImpl extends BaseInfoProperties implements MsgService {

    @Autowired
    private MessageRepository messageRepository;

    private final MemberMapper memberMapper;

    @Override
    public void createMsg(String fromUserId,
                          String toUserId,
                          Integer type,
                           Map<String, Object> msgContent) {

        Member fromUser = memberMapper.selectById(fromUserId);

        MessageMO messageMO = new MessageMO();

        messageMO.setFromUserId(fromUserId);
        // 兼容会员资料缺失的情况，避免空指针
        String fromNickname = fromUser != null && fromUser.getNickname() != null ? fromUser.getNickname() : "";
        String fromFace = fromUser != null && fromUser.getAvatar() != null ? fromUser.getAvatar() : "";
        messageMO.setFromNickname(fromNickname);
        messageMO.setFromFace(fromFace);

        messageMO.setToUserId(toUserId);

        messageMO.setMsgType(type);
        if (msgContent != null) {
            messageMO.setMsgContent(msgContent);
        }

        messageMO.setCreateTime(new Date());

        messageRepository.save(messageMO);
    }
//
    @Override
    public List<MessageMO> queryList(String toUserId,
                                     Integer page,
                                     Integer pageSize) {

        Pageable pageable = PageRequest.of(page,
                                            pageSize,
                                            Sort.Direction.DESC,
                                            "createTime");

        List<MessageMO> list =  messageRepository
                        .findAllByToUserIdEqualsOrderByCreateTimeDesc(toUserId,
                                                                pageable);
        for (MessageMO msg : list) {
            // 如果类型是关注消息，则需要查询我之前有没有关注过他，用于在前端标记“互粉”“互关”
            if (msg.getMsgType() != null && msg.getMsgType() == MessageEnum.FOLLOW_YOU.type) {
                Map<String, Object> map = (Map<String, Object>) msg.getMsgContent();
                if (map == null) {
                    map = new HashMap<>();
                }

                String relationship = redis.getCacheObject(REDIS_FANS_AND_VLOGGER_RELATIONSHIP + ":" + msg.getToUserId() + ":" + msg.getFromUserId());
                if (StringUtils.isNotBlank(relationship) && relationship.equalsIgnoreCase("1")) {
                    map.put("isFriend", true);
                } else {
                    map.put("isFriend", false);
                }
                msg.setMsgContent(map);
             }
        }
        return list;
    }
}
