package com.red.circle.other.app.inner.service.material.props.impl;

import com.red.circle.common.business.enums.SendPropsOrigin;
import com.red.circle.other.app.inner.convertor.material.RoomThemeUserBackpackInnerConvertor;
import com.red.circle.other.app.inner.service.material.props.RoomThemeBackpackClientService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.rds.entity.props.RunningWaterUserProps;
import com.red.circle.other.infra.database.rds.service.props.RoomThemeUserBackpackService;
import com.red.circle.other.infra.database.rds.service.props.RunningWaterUserPropsService;
import com.red.circle.other.inner.model.dto.material.RoomThemeUserBackpackDTO;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户房间主题背包 服务类.
 *
 * @author pengliang on 2023/6/6
 */
@Service
@RequiredArgsConstructor
public class RoomThemeBackpackClientServiceImpl implements RoomThemeBackpackClientService {

  private final UserProfileGateway userProfileGateway;
  private final RoomThemeUserBackpackService roomThemeUserBackpackService;
  private final RunningWaterUserPropsService runningWaterUserPropsService;
  private final RoomThemeUserBackpackInnerConvertor roomThemeUserBackpackInnerConvertor;

  @Override
  public List<RoomThemeUserBackpackDTO> listByUserIdOrderByUseThemeDesc(Long userId) {
    return roomThemeUserBackpackInnerConvertor.toListRoomThemeUserBackpackDTO(
        roomThemeUserBackpackService.listByUserIdOrderByUseThemeDesc(userId)
    );
  }

  @Override
  public boolean switchUseTheme(Long userId, Long useThemeId, Boolean isFree) {
    return roomThemeUserBackpackService.switchUseTheme(userId, useThemeId, isFree);
  }

  @Override
  public boolean cleanUseTheme(Long userId) {
    return roomThemeUserBackpackService.cleanUseTheme(userId);
  }

  @Override
  public RoomThemeUserBackpackDTO getNotExpireTimeByUserAndThemeId(Long userId, Long themeId) {
    return roomThemeUserBackpackInnerConvertor.toRoomThemeUserBackpackDTO(
        roomThemeUserBackpackService.getNotExpireTimeByUserAndThemeId(userId, themeId)
    );
  }

  @Override
  public void sendRoomTheme(Long userId, Long themeId, Integer days, SendPropsOrigin origin) {

    roomThemeUserBackpackService.addTheme(userId, themeId, days);

    runningWaterUserPropsService.save(new RunningWaterUserProps()
        .setUserId(userId)
        .setPropsId(themeId)
        .setPropsCandy(BigDecimal.ZERO)
        .setOrigin(Objects.toString(origin))
        .setDays(days)
        .setOriginDesc(origin.getDesc())
        .setType(Objects.toString(origin))
    );

    userProfileGateway.removeCacheAll(userId);
  }

  @Override
  public RoomThemeUserBackpackDTO getByUserUseTheme(Long userId) {
    return roomThemeUserBackpackInnerConvertor.toRoomThemeUserBackpackDTO(
        roomThemeUserBackpackService.getByUserUseTheme(userId)
    );
  }

  @Override
  public List<RoomThemeUserBackpackDTO> listByUserUseTheme(Collection<Long> userIds) {
    return roomThemeUserBackpackInnerConvertor.toListRoomThemeUserBackpackDTO(
        roomThemeUserBackpackService.listByUserUseTheme(userIds)
    );
  }
}
