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

import com.google.common.collect.Lists;
import com.red.circle.other.app.convertor.material.PropsMaterialAppConvertor;
import com.red.circle.other.app.dto.clientobject.material.PropsNobleVipStoreCO;
import com.red.circle.other.app.dto.cmd.material.PropsNobleVipQryCmd;
import com.red.circle.other.domain.gateway.props.PropsStoreGateway;
import com.red.circle.other.infra.database.rds.entity.props.PropsBackpack;
import com.red.circle.other.infra.database.rds.service.props.PropsBackpackService;
import com.red.circle.other.inner.enums.material.PropsCommodityType;
import com.red.circle.other.inner.model.dto.material.props.NobleVipAbility;
import com.red.circle.other.inner.model.dto.material.props.PropsStoreCommodity;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 获取道具商店.
 *
 * @author pengliang on 2021/6/1
 */
@Component
@RequiredArgsConstructor
public class NobleVipStoreListQryExe {

  private final PropsMaterialAppConvertor propsMaterialAppConvertor;
  private final PropsStoreGateway propsStoreGateway;
  private final PropsBackpackService propsBackpackService;

  public List<PropsNobleVipStoreCO> execute(PropsNobleVipQryCmd cmd) {

    List<PropsStoreCommodity> propsStoreCommodities = propsStoreGateway.listReleaseStoreCommodity(
        cmd.requireReqSysOriginEnum(), PropsCommodityType.NOBLE_VIP);

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

    List<PropsBackpack> propsBackpacks = propsBackpackService
        .listNotExpiredByType(cmd.getReqUserId(), PropsCommodityType.NOBLE_VIP);

    return propsStoreCommodities.stream().map(nobleVipStore -> {
      NobleVipAbility nobleVipAbility = (NobleVipAbility) nobleVipStore.getPropsAbility();
      return new PropsNobleVipStoreCO()
          .setId(nobleVipStore.getId())
          .setNobleVipAbility(propsMaterialAppConvertor.toNobleVipAbilityCO(nobleVipAbility))
          .setPropsResources(
              propsMaterialAppConvertor.toPropsResourcesDTO(nobleVipStore.getPropsResources()))
          .setAvatarFrame(propsMaterialAppConvertor.toPropsResourcesDTO(nobleVipAbility.getAvatarFrame()))
          .setCar(propsMaterialAppConvertor.toPropsResourcesDTO(nobleVipAbility.getCar()))
          .setChatBubble(propsMaterialAppConvertor.toPropsResourcesDTO(nobleVipAbility.getChatBubble()))
          .setDataCard(propsMaterialAppConvertor.toPropsResourcesDTO(nobleVipAbility.getDataCard()))
          .setPropsPrices(nobleVipStore.getPropsPrices())
          .setOwn(contains(propsBackpacks, nobleVipStore.propsResourceId()))
          .setOwnArbitrary(CollectionUtils.isNotEmpty(propsBackpacks));
    }).collect(Collectors.toList());
  }

  private Boolean contains(List<PropsBackpack> propsBackpacks, Long sourceId) {
    return propsBackpacks.stream().filter(propsBackpack -> Objects
            .equals(propsBackpack.getPropsId(), sourceId))
        .findFirst()
        .map(propsBackpack -> Objects.nonNull(propsBackpack.getId()))
        .orElse(Boolean.FALSE);
  }

}
