package com.red.circle.other.app.command.family;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.other.app.dto.clientobject.family.FamilyLevelCO;
import com.red.circle.other.infra.database.rds.entity.family.FamilyBaseInfo;
import com.red.circle.other.infra.database.rds.entity.family.FamilyLevelConfig;
import com.red.circle.other.infra.database.rds.entity.family.FamilyMemberInfo;
import com.red.circle.other.infra.database.rds.service.family.FamilyBaseInfoService;
import com.red.circle.other.infra.database.rds.service.family.FamilyLevelConfigService;
import com.red.circle.other.infra.database.rds.service.family.FamilyMemberInfoService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 家族等级列表.
 */
@Component
@RequiredArgsConstructor
public class FamilyLevelListExe {

  private final FamilyLevelConfigService familyLevelConfigService;
  private final FamilyMemberInfoService familyMemberInfoService;
  private final FamilyBaseInfoService familyBaseInfoService;

  public List<FamilyLevelCO> execute(AppExtCommand cmd) {
    List<FamilyLevelConfig> levelConfigs = familyLevelConfigService
        .listLevelConfig(cmd.getReqSysOrigin().getOrigin());

    ResponseAssert.isFalse(CommonErrorCode.NOT_FOUND_RECORD_INFO,
        CollectionUtils.isEmpty(levelConfigs));

    // 获取用户当前家族等级
    Long currentFamilyLevelId = getCurrentFamilyLevelId(cmd.getReqUserId());

    return levelConfigs.stream()
        .map(config -> convertToLevelCO(config, currentFamilyLevelId))
        .sorted(Comparator.comparing(FamilyLevelCO::getSort))
        .collect(Collectors.toList());
  }

  /**
   * 获取用户当前家族等级id
   */
  private Long getCurrentFamilyLevelId(Long userId) {
    FamilyMemberInfo familyMemberInfo = familyMemberInfoService.getFamilyMemberByUserId(userId);
    if (familyMemberInfo == null) {
      return null;
    }

    FamilyBaseInfo familyBaseInfo = familyBaseInfoService.getBaseInfoById(familyMemberInfo.getFamilyId());
    if (familyBaseInfo == null) {
      return 1L;
    }

    return familyBaseInfo.getFamilyLevelId();
  }

  /**
   * 转换为LevelCO，如果等级大于当前家族等级Id，则levelExp设置为null
   */
  private FamilyLevelCO convertToLevelCO(FamilyLevelConfig config, Long currentFamilyLevelId) {
    // 如果用户有家族，且配置等级大于当前家族等级，则levelExp设置为null
    Long levelExp = config.getLevelExp();
    if (currentFamilyLevelId != null && config.getId() > currentFamilyLevelId) {
      levelExp = null;
    }

    return new FamilyLevelCO()
        .setLevel(config.getLevelKey().substring(6))
        .setLevelExp(levelExp)
        .setMaxMember(config.getMaxMember())
        .setMaxManager(config.getMaxManager())
        .setLevelBackgroundPicture(config.getLevelBackgroundPicture())
        .setSort(config.getSort());
  }
}
