package com.yunzhupaas.message.service.impl;

import com.yunzhupaas.base.service.SuperServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunzhupaas.message.entity.ImContentEntity;
import com.yunzhupaas.message.entity.ImReplyEntity;
import com.yunzhupaas.message.mapper.ImReplyMapper;
import com.yunzhupaas.message.model.ImReplyListModel;
import com.yunzhupaas.message.service.ImReplyService;
import com.yunzhupaas.permission.entity.UserEntity;
import com.yunzhupaas.permission.service.UserService;
import com.yunzhupaas.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司（http://www.szlecheng.cn）
 * @date 2024-05-29
 */
@Service
public class ImReplyServiceImpl extends SuperServiceImpl<ImReplyMapper, ImReplyEntity> implements ImReplyService {

    @Autowired
    private UserService userService;

    @Override
    public List<ImReplyEntity> getList() {
        QueryWrapper<ImReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ImReplyEntity::getUserId, UserProvider.getUser().getUserId()).or()
                .eq(ImReplyEntity::getReceiveUserId, UserProvider.getUser().getUserId())
                .orderByDesc(ImReplyEntity::getUserId);
        return this.list();
    }

    @Override
    public boolean savaImReply(ImReplyEntity entity) {
        QueryWrapper<ImReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ImReplyEntity::getUserId, entity.getUserId()).eq(ImReplyEntity::getReceiveUserId, entity.getReceiveUserId());
        //判断数据是否存在
        ImReplyEntity imReplyEntity = this.getOne(queryWrapper);
        if (imReplyEntity != null) {
            entity.setId(imReplyEntity.getId());
            this.updateById(entity);
            return true;
        }
        this.save(entity);
        return true;

    }

    @Override
    public List<ImReplyListModel> getImReplyList() {
        List<ImReplyListModel> imReplyList = this.baseMapper.getImReplyList();
        List<ImReplyListModel> imReplyLists = new ArrayList<>(imReplyList);
        // 过滤掉用户id和接收id相同的
        imReplyLists = imReplyList.stream().filter(t ->{
            if(t.getImreplySendDeleteMark() == null){
                return true;
            }
            return false;
        }).collect(Collectors.toList());
//        // 过滤掉用户id和接收id相同的
//        List<ImReplyListModel> imReplyListModels = new ArrayList<>(imReplyList);
//        for (int i = 0; i < imReplyList.size(); i++) {
//            ImReplyListModel imReplyListModel = imReplyList.get(i);
//            // 不和自己比
//            imReplyListModels.remove(imReplyList.get(i));
//            List<ImReplyListModel> irs = new ArrayList<>(imReplyListModels);
//            ImReplyListModel model = irs.stream().filter(t -> t.getUserId().equals(imReplyListModel.getUserId()) && t.getId().equals(imReplyListModel.getId())).findFirst().orElse(null);
//            if (model != null) {
//                imReplyLists.remove(model);
//            }
//        }
        //我发给别人
        List<ImReplyListModel> collect = imReplyLists.stream().filter(t -> t.getUserId().equals(UserProvider.getUser().getUserId())).collect(Collectors.toList());
        //头像替换成对方的
        for (ImReplyListModel imReplyListModel : collect) {
            UserEntity entity = userService.getInfo(imReplyListModel.getId());
            imReplyListModel.setHeadIcon(entity != null ? entity.getHeadIcon() : "");
//            imReplyListModel.setSendDeleteMark(imReplyListModel.getSendDeleteMark());
//            imReplyListModel.setImreplySendDeleteMark(imReplyListModel.getImreplySendDeleteMark());
//            imReplyListModel.setDeleteMark(imReplyListModel.getDeleteMark());
        }
        //别人发给我
        List<ImReplyListModel> list = imReplyLists.stream().filter(t -> t.getId().equals(UserProvider.getUser().getUserId())).collect(Collectors.toList());
        for (ImReplyListModel model : list) {
            //移除掉互发的
            List<ImReplyListModel> collect1 = collect.stream().filter(t -> t.getId().equals(model.getUserId())).collect(Collectors.toList());
            if (collect1.size() > 0) {
                //判断我发给别人的时间和接收的时间大小
                //接收的大于发送的
                if (model.getLatestDate().getTime() > collect1.get(0).getLatestDate().getTime()) {
                    collect.remove(collect1.get(0));
                } else { //发送的大于接收的则跳过
                    continue;
                }
            }
            ImReplyListModel imReplyListModel = new ImReplyListModel();
            UserEntity entity = userService.getInfo(model.getUserId());
            if(entity != null) {
                imReplyListModel.setHeadIcon(entity.getHeadIcon());
                imReplyListModel.setUserId(UserProvider.getUser().getUserId());
                imReplyListModel.setId(entity.getId());
                imReplyListModel.setLatestDate(model.getLatestDate());
                imReplyListModel.setLatestMessage(model.getLatestMessage());
                imReplyListModel.setMessageType(model.getMessageType());
                if (model.getImreplySendDeleteMark() != null && !model.getImreplySendDeleteMark().equals(UserProvider.getUser().getUserId())) {
                    imReplyListModel.setSendDeleteMark(model.getSendDeleteMark());
                    imReplyListModel.setImreplySendDeleteMark(model.getImreplySendDeleteMark());
                    imReplyListModel.setDeleteMark(model.getDeleteMark());
                }
                imReplyListModel.setDeleteUserId(model.getDeleteUserId());

                collect.add(imReplyListModel);
            }
        }
        return collect;
    }

    @Override
    public boolean relocation(String sendUserId, String receiveUserId) {
        QueryWrapper<ImReplyEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().and(t-> {
            t.eq(ImReplyEntity::getUserId, receiveUserId)
                    .eq(ImReplyEntity::getReceiveUserId, sendUserId).or();
            t.eq(ImReplyEntity::getReceiveUserId, receiveUserId)
                    .eq(ImReplyEntity::getUserId, sendUserId);
        });
        List<ImReplyEntity> list = this.list(queryWrapper);
        for (ImReplyEntity entity : list) {
            if(entity.getDeleteUserId()!=null){
                if(!entity.getDeleteUserId().equals(sendUserId)) {
                    entity.setDeleteMark(1);
                    this.updateById(entity);
                }
            }
            entity.setDeleteUserId(sendUserId);
            this.updateById(entity);
        }
        QueryWrapper<ImReplyEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ImReplyEntity::getDeleteMark,1);
        this.remove(wrapper);
        return false;
    }

}

