package cn.reghao.tnb.content.app.chat.service;

import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.content.app.chat.db.mapper.GroupInfoMapper;
import cn.reghao.tnb.content.app.chat.db.mapper.GroupMemberMapper;
import cn.reghao.tnb.content.app.chat.db.mapper.GroupNoticeMapper;
import cn.reghao.tnb.content.app.chat.model.dto.group.*;
import cn.reghao.tnb.content.app.chat.model.po.GroupInfo;
import cn.reghao.tnb.content.app.chat.model.po.GroupMember;
import cn.reghao.tnb.user.api.dto.ContactDetail;
import org.springframework.stereotype.Service;

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

/**
 * @author reghao
 * @date 2022-04-29 11:18:27
 */
@Service
public class GroupInfoService {
    private final GroupInfoMapper groupInfoMapper;
    private final GroupMemberMapper groupMemberMapper;
    private final GroupNoticeMapper groupNoticeMapper;

    public GroupInfoService(GroupInfoMapper groupInfoMapper, GroupMemberMapper groupMemberMapper,
                            GroupNoticeMapper groupNoticeMapper) {
        this.groupInfoMapper = groupInfoMapper;
        this.groupMemberMapper = groupMemberMapper;
        this.groupNoticeMapper = groupNoticeMapper;
    }

    public CreateGroupRet createGroup(CreateGroup createGroup) {
        long loginUser = UserContext.getUserId();
        GroupInfo groupInfo = new GroupInfo(createGroup, loginUser);
        groupInfoMapper.save(groupInfo);

        long groupId = groupInfo.getId();
        List<GroupMember> list = Arrays.stream(createGroup.getIds().split(","))
                .map(memberId -> new GroupMember(groupId, Long.valueOf(memberId)))
                .collect(Collectors.toList());
        list.add(new GroupMember(groupId, loginUser));
        groupMemberMapper.saveAll(list);
        return new CreateGroupRet(groupId);
    }

    public GroupDetailRet getGroupDetail(long groupId) {
        long loginUser = UserContext.getUserId();
        GroupMember groupMember = groupMemberMapper.findByGroupAndUserId(groupId, loginUser);
        if (groupMember == null) {
            // TODO 只有群组成员才可获取群信息
            return null;
        }

        GroupDetailRet groupDetailRet = groupInfoMapper.findDetailByGroupId(groupId);
        long ownerId = groupDetailRet.getOwnerId();
        ContactDetail contactDetail = new ContactDetail();
        String screenName = contactDetail.getScreenName();
        groupDetailRet.setVisitCard(screenName);
        if (ownerId == loginUser) {
            groupDetailRet.setManager(true);
        }

        NoticeRet noticeRet = groupNoticeMapper.findByLatest(groupId);
        long userId = noticeRet.getUserId();
        ContactDetail contactDetail1 = new ContactDetail();
        noticeRet.setNickname(contactDetail1.getScreenName());
        noticeRet.setAvatar(contactDetail1.getAvatarUrl());

        groupDetailRet.setNotice(noticeRet);
        return groupDetailRet;
    }

    public void editGroupDetail(GroupSetting groupSetting) {
        long loginUser = UserContext.getUserId();
        int groupId = groupSetting.getGroupId();
        GroupInfo groupInfo = groupInfoMapper.findByGroupId(groupId);
        long ownerId = groupInfo.getOwnerId();
        if (loginUser != ownerId) {
            // TODO 只有群主才可以修改群信息
            return;
        }

        groupInfo.setName(groupSetting.getGroupName());
        groupInfo.setProfile(groupSetting.getProfile());
        groupInfo.setAvatar(groupSetting.getAvatar());
        groupInfoMapper.updateGroupInfo(groupInfo);
    }

    public void deleteGroup(long groupId) {
    }
}
