package com.red.circle.other.app.command.activity.query;

import com.google.common.collect.Lists;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.activity.ActivityGlobalizationDescriptionCO;
import com.red.circle.other.app.dto.clientobject.activity.ActivityHallFameCO;
import com.red.circle.other.app.dto.clientobject.activity.ActivityUserCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.rds.entity.activity.ActivityGlobalizationDescription;
import com.red.circle.other.infra.database.rds.entity.activity.ActivityHallFame;
import com.red.circle.other.infra.database.rds.service.activity.ActivityGlobalizationDescriptionService;
import com.red.circle.other.infra.database.rds.service.activity.ActivityHallFameService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.text.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 活动名人堂.
 *
 * @author pengshigang on 2022/08/11
 */
@Service
@RequiredArgsConstructor
public class ActivityHallFameQueryExe {

  private final UserProfileGateway userProfileGateway;
  private final UserProfileAppConvertor userProfileAppConvertor;
  private final ActivityHallFameService activityHallFameService;
  private final ActivityGlobalizationDescriptionService activityGlobalizationDescriptionService;

  public List<ActivityHallFameCO> execute(AppExtCommand cmd) {

    List<ActivityHallFame> activityHallFameList = getActivityHallFameList(cmd);
    if (CollectionUtils.isEmpty(activityHallFameList)) {
      return Lists.newArrayList();
    }

    Map<Long, List<ActivityGlobalizationDescription>> descriptionMap = getDescriptionMap(
        activityHallFameList);

    if (CollectionUtils.isEmpty(descriptionMap)) {
      return Lists.newArrayList();
    }

    return activityHallFameList.stream()
        .map(hallFame -> {
          List<ActivityGlobalizationDescription> descriptions = descriptionMap
              .get(hallFame.getId());
          if (CollectionUtils.isEmpty(descriptions) || StringUtils
              .isBlank(hallFame.getAccounts())) {
            return null;
          }
          List<ActivityUserCO> users = Lists.newArrayList();
          for (String account : hallFame.getAccounts().split(",")) {
            UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO(
                userProfileGateway.getByUserId(Long.valueOf(account)));
            if (Objects.isNull(userProfile)) {
              return null;
            }
            users.add(new ActivityUserCO()
                .setUserId(userProfile.getId())
                .setUserNickname(userProfile.getUserNickname())
                .setAccount(userProfile.actualAccount())
                .setUserAvatar(userProfile.getUserAvatar())
                .setCountryCode(userProfile.getCountryCode())
                .setCountryName(userProfile.getCountryName())
            );
          }
          if (CollectionUtils.isEmpty(users)) {
            return null;
          }
          return new ActivityHallFameCO()
              .setUsers(users)
              .setDescriptions(
                  descriptions.stream()
                      .map(description -> new ActivityGlobalizationDescriptionCO()
                          .setLanguage(description.getLanguage())
                          .setDescription(description.getDescription())
                      ).toList()
              );
        }).filter(Objects::nonNull).collect(Collectors.toList());

  }

  private Map<Long, List<ActivityGlobalizationDescription>> getDescriptionMap(
      List<ActivityHallFame> activityHallFameList) {
    return activityGlobalizationDescriptionService.mapByRelationIds(
        activityHallFameList.stream().map(ActivityHallFame::getId).collect(Collectors.toSet()));
  }

  private List<ActivityHallFame> getActivityHallFameList(AppExtCommand cmd) {
    return activityHallFameService.listBySysOrigin(cmd.getReqSysOrigin().getOrigin());
  }


}
