package com.red.circle.other.app.convertor.material;

import com.google.common.collect.Lists;
import com.red.circle.framework.core.convertor.ConvertorModel;
import com.red.circle.mq.business.model.event.gift.GiveAwayGiftBatchEvent;
import com.red.circle.mq.business.model.event.gift.GiveAwayGiftRoomAccepts;
import com.red.circle.mq.business.model.event.gift.GiveGiftConfig;
import com.red.circle.other.app.dto.clientobject.gift.GiftBackpackLimitCO;
import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO;
import com.red.circle.other.app.dto.cmd.gift.GiveAwayGiftBackpackCmd;
import com.red.circle.other.app.dto.cmd.gift.GiveAwayGiftBatchCmd;
import com.red.circle.other.infra.database.rds.entity.gift.GiftBackpackLimit;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;

/**
 * @author pengliang on 2023/5/23
 */
@Mapper(componentModel = ConvertorModel.SPRING)
public interface GiftAppConvertor {

  GiftConfigDTO toGiftConfigDTO(GiftConfig giftConfig);

  List<GiftConfigDTO> toListGiftConfigDTO(List<GiftConfig> giftConfigs);

  List<GiftConfigCO> toListGiftConfigCO(List<GiftConfigDTO> giftConfigCaches);

  GiftBackpackLimitCO toGiftBackpackLimitCO(GiftBackpackLimit giftBackpackLimit);

  GiftConfigCO toGiftConfigCO(GiftConfig giftConfig);

  List<GiftConfigCO> toGiftConfigCO(List<GiftConfig> giftConfig);

  default GiveAwayGiftBatchEvent toGiveAwayGiftBatchEvent(GiveAwayGiftBatchCmd cmd,
      GiftConfigDTO giftConfig, Long trackId) {
    return new GiveAwayGiftBatchEvent()
        .setGameId(cmd.getGameId())
        .setSysOrigin(cmd.requireReqSysOriginEnum())
        .setRequestPlatform(Objects.toString(cmd.getReqClient()))
        .setSendUserId(cmd.requiredReqUserId())
        .setAccepts(getAccepts(cmd))
        .setQuantity(cmd.getQuantity())
        .setGiftConfig(toGiveGiftConfig(giftConfig))
        .setRoomId(cmd.getRoomId())
        .setTrackId(trackId)
        .setSongId(cmd.getSongId())
        .setBagGift(Boolean.FALSE)
        .setCreateTime(
            Objects.isNull(cmd.getReqTime()) ? TimestampUtils.now()
                : new Timestamp(cmd.getReqTime().getTime()));
  }

  private List<GiveAwayGiftRoomAccepts> getAccepts(GiveAwayGiftBatchCmd cmd) {
    if (CollectionUtils.isNotEmpty(cmd.getAccepts())) {

      return cmd.acceptsDistinct().stream().map(accept -> new GiveAwayGiftRoomAccepts()
          .setSeatIndex(accept.getSeatIndex())
          .setAcceptUserId(accept.getAcceptUserId())
      ).collect(Collectors.toList());
    }

    if (CollectionUtils.isNotEmpty(cmd.getAcceptUserIds())) {
      return cmd.getAcceptUserIds().stream().distinct().map(acceptUserId ->
          new GiveAwayGiftRoomAccepts()
              .setSeatIndex(null)
              .setAcceptUserId(acceptUserId)
      ).collect(Collectors.toList());
    }
    return Lists.newArrayList();
  }

  default GiveGiftConfig toGiveGiftConfig(GiftConfigDTO giftConfig) {
    return new GiveGiftConfig()
        .setId(giftConfig.getId())
        .setGiftCandy(giftConfig.getGiftCandy())
        .setGiftIntegral(giftConfig.getGiftIntegral())
        .setSpecial(giftConfig.getSpecial())
        .setType(giftConfig.getType())
        .setGiftTab(giftConfig.getGiftTab());
  }

  default GiveAwayGiftBatchEvent toBackpackGift(GiveAwayGiftBackpackCmd cmd,
      GiftConfigDTO giftConfig, Long trackId) {
    return new GiveAwayGiftBatchEvent()
        .setSysOrigin(cmd.requireReqSysOriginEnum())
        .setSongId(cmd.getSongId())
        .setRequestPlatform(Objects.toString(cmd.getReqClient()))
        .setSendUserId(cmd.requiredReqUserId())
        .setAccepts(List.of(
                new GiveAwayGiftRoomAccepts()
                    .setSeatIndex(cmd.getSeatIndex())
                    .setAcceptUserId(cmd.getAcceptUserId())
            )
        )
        .setQuantity(cmd.getQuantity())
        .setGiftConfig(toGiveGiftConfig(giftConfig))
        .setRoomId(cmd.getRoomId())
        .setTrackId(trackId)
        .setBagGift(Boolean.TRUE)
        .setCreateTime(Objects.isNull(cmd.getReqTime()) ? TimestampUtils.now()
            : new Timestamp(cmd.getReqTime().getTime()));
  }

}
