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

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.material.GiftInnerConvertor;
import com.red.circle.other.app.inner.service.material.gift.GiftConfigClientService;
import com.red.circle.other.infra.database.cache.service.other.GameLuckyGiftCacheService;
import com.red.circle.other.infra.database.cache.service.other.GiftCacheService;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.infra.database.rds.service.gift.GiftWallService;
import com.red.circle.other.inner.model.cmd.sys.GiftQryCmd;
import com.red.circle.other.inner.model.dto.gift.UserGiftWallDTO;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author pengliang on 2023/5/24
 */
@Service
@RequiredArgsConstructor
public class GiftConfigClientServiceImpl implements GiftConfigClientService {

  private final GiftWallService giftWallService;
  private final GiftCacheService giftCacheService;
  private final GiftConfigService giftConfigService;
  private final GiftInnerConvertor giftInnerConvertor;
  private final GameLuckyGiftCacheService gameLuckyGiftCacheService;

  @Override
  public GiftConfigDTO getById(Long id) {
    return giftCacheService.getById(id);
  }

  @Override
  public Map<Long, GiftConfigDTO> mapByIds(Set<Long> ids) {
    return giftInnerConvertor.toMapGiftConfigDTO(giftConfigService.mapByIds(ids));
  }

  @Override
  public List<GiftConfigDTO> listByIds(Set<Long> ids) {
    return giftInnerConvertor.toListGiftConfigDTO(giftConfigService.listByIds(ids));
  }

  @Override
  public GiftConfigDTO getPairCpGive(SysOriginPlatformEnum sysOrigin) {
    return giftCacheService.getPairCpGive(sysOrigin);
  }

  @Override
  public List<GiftConfigDTO> listBySysOrigin(String sysOrigin) {
    return giftInnerConvertor.toListGiftConfigDTO(giftConfigService.listBySysOrigin(sysOrigin));
  }

  @Override
  public PageResult<GiftConfigDTO> pageGiftInfo(GiftQryCmd giftQryCmd) {
    PageResult<GiftConfig> pageResult = giftConfigService.pageGiftInfo(giftQryCmd);
    return pageResult.convert(giftInnerConvertor::toGiftConfigDTO);
  }

  @Override
  public Boolean updateGift(GiftConfigDTO giftConfigDTO) {
    return giftConfigService.updateGift(giftInnerConvertor.toGiftConfig(giftConfigDTO));
  }

  @Override
  public void removeGiftCache() {
    giftCacheService.remove();
  }

  @Override
  public void removeRemainingInventory(Long id, Long standardId) {
    gameLuckyGiftCacheService.removeRemainingInventory(id, standardId);
  }

  @Override
  public Boolean saveGift(GiftConfigDTO giftConfig) {
    return giftConfigService.save(giftInnerConvertor.toGiftConfig(giftConfig));
  }

  @Override
  public Boolean switchDelStatus(Long id, Boolean status) {
    return giftConfigService.switchDelStatus(id, status);
  }

  @Override
  public List<GiftConfigDTO> listByTab(String sysOrigin, String giftTab) {
    return giftInnerConvertor.toListGiftConfigDTO(giftConfigService.listByTab(sysOrigin, giftTab));
  }

  @Override
  public List<GiftConfigDTO> listByTabV2(GiftQryCmd giftQryCmd) {
    return giftInnerConvertor.toListGiftConfigDTO(giftConfigService.listByTabV2(giftQryCmd));
  }

  @Override
  public String getPairCpGiveGiftId(String sysOrigin) {
    return Optional.ofNullable(
        giftCacheService.getPairCpGiveGiftId(SysOriginPlatformEnum.valueOf(sysOrigin))).map(
        Objects::toString).orElse("");
  }

  @Override
  public void pushPairCpGiveGiftId(String sysOrigin, Long giftId) {
    giftCacheService.pushPairCpGiveGiftId(SysOriginPlatformEnum.valueOf(sysOrigin), giftId);
  }


  @Override
  public List<UserGiftWallDTO> listGiftWallByUser(Long userId) {
    return giftInnerConvertor.toListUserGiftWallDTO(giftWallService.listGiftWallByUser(userId));
  }

  @Override
  public List<GiftConfigDTO> listByExpiredTime(Timestamp now) {
    return giftInnerConvertor.toListGiftConfigDTO(giftConfigService.listByExpiredTime(now));
  }
}
