package com.red.circle.other.app.service.user.user;

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.UserIdCommonCmd;
import com.red.circle.other.app.convertor.user.CpBlessRecordConvert;
import com.red.circle.other.app.convertor.user.UserCpCabinConvertor;
import com.red.circle.other.app.dto.clientobject.user.user.CpBlessRecordCO;
import com.red.circle.other.app.dto.clientobject.user.user.UserCpCabinUnreadCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.model.user.UserProfile;
import com.red.circle.other.infra.database.rds.entity.user.user.CpBlessRecord;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
import com.red.circle.other.infra.database.rds.service.user.user.CpBlessRecordService;
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * CP最近两百条祝福记录.
 *
 * @author zongpubin on 2023/11/20
 */

@Component
@AllArgsConstructor
public class CpBlessRecordAndUpReadExe {

  private final UserCpCabinConvertor userCpCabinConvertor;
  private final CpBlessRecordService cpBlessRecordService;
  private final UserProfileGateway userProfileGateway;
  private final CpRelationshipService cpRelationshipService;
  private final CpBlessRecordConvert cpBlessRecordConvert;

  public UserCpCabinUnreadCO execute(UserIdCommonCmd cmd) {

    if (!Objects.equals(cmd.getReqUserId(), cmd.getUserId())) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(Lists.newArrayList())
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }

    CpRelationship relationship = cpRelationshipService.getByUserId(cmd.getReqUserId(), cmd.getUserId());
    if (Objects.isNull(relationship)) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(Lists.newArrayList())
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }

    List<CpBlessRecord> cpBlessRecords = cpBlessRecordService.listByUserId(
        relationship.getUserId());
    if (CollectionUtils.isEmpty(cpBlessRecords)) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(Lists.newArrayList())
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }

    Map<Long, UserProfile> userProfileMap =  userProfileGateway.mapByUserIds(
        cpBlessRecords.stream().map(CpBlessRecord::getSendUserId).collect(Collectors.toSet()));
    if (CollectionUtils.isEmpty(userProfileMap)) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(Lists.newArrayList())
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }


    // 将记录标记为已读
    cpBlessRecordService.updateByUserId(relationship.getUserId());

    List<CpBlessRecordCO> cpBlessRecordList = cpBlessRecords.stream().map(cpBlessRecord -> {
      CpBlessRecordCO co = cpBlessRecordConvert.toCpBlessRecordCO(cpBlessRecord);
      UserProfile userProfile = userProfileMap.get(cpBlessRecord.getSendUserId());
      if (Objects.isNull(userProfile)) {
        return null;
      }
      co.setSendUserAvatar(userProfile.getUserAvatar());
      co.setSendUserNickname(userProfile.getUserNickname());
      return co;
    }).filter(Objects::nonNull).collect(Collectors.toList());
    if (CollectionUtils.isEmpty(cpBlessRecordList)) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(Lists.newArrayList())
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }


    if (cpBlessRecordList.size() <= 2) {
      return UserCpCabinUnreadCO.builder()
          .topClassRecords(cpBlessRecordList)
          .bottomClassRecords(Lists.newArrayList())
          .build();
    }
    List<CpBlessRecordCO> topClassRecords = Lists.newArrayList();
    List<CpBlessRecordCO> bottomClassRecords = Lists.newArrayList();
    for (int i = 0; i < cpBlessRecordList.size(); i++) {
      if (i % 2 == 0) {
        bottomClassRecords.add(cpBlessRecordList.get(i));
        continue;
      }
      topClassRecords.add(cpBlessRecordList.get(i));
    }

    return UserCpCabinUnreadCO.builder()
        .topClassRecords(topClassRecords)
        .bottomClassRecords(bottomClassRecords)
        .build();

  }


}
