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.other.app.dto.clientobject.activity.ActivityResourceCO;
import com.red.circle.other.app.dto.clientobject.material.NobleVipAbilityCO;
import com.red.circle.other.app.dto.clientobject.material.PropsStoreCommodityInfoCO;
import com.red.circle.other.inner.model.dto.activity.props.ActivityResource;
import com.red.circle.other.inner.model.dto.material.PropsResourcesDTO;
import com.red.circle.other.inner.model.dto.material.UsePropsDTO;
import com.red.circle.other.inner.model.dto.material.props.NobleVipAbility;
import com.red.circle.other.inner.model.dto.material.props.PropsResources;
import com.red.circle.other.inner.model.dto.material.props.PropsStoreCommodity;
import com.red.circle.other.inner.model.dto.material.props.UserBackpackProps;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import com.red.circle.other.inner.model.dto.user.props.UserPropsResourcesDTO;
import com.red.circle.other.inner.model.dto.user.props.UserUsePropsDTO;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;

/**
 * @author pengliang on 2023/6/3
 */
@Mapper(componentModel = ConvertorModel.SPRING)
public interface PropsMaterialAppConvertor {

  default List<UserBackpackProps> userUsePropsToPropsUserBackpackCOList(
      List<UserUsePropsDTO> useProps) {
    if (CollectionUtils.isEmpty(useProps)) {
      return Lists.newArrayList();
    }
    return useProps.stream().map(props -> {
      if (Objects.isNull(props.getPropsResources()) || Objects.isNull(props.getUserId()) ||
          Objects.isNull(props.getExpireTime())) {
        return null;
      }
      if (props.getExpireTime() <= TimestampUtils.now().getTime()) {
        return null;
      }
      return userUsePropsToPropsUserBackpackCO(props);
    }).filter(Objects::nonNull).collect(Collectors.toList());
  }

  default UserBackpackProps userUsePropsToPropsUserBackpackCO(UserUsePropsDTO useProps) {
    UserBackpackProps userBackpackProps = new UserBackpackProps();
    userBackpackProps.setUserId(useProps.getUserId());
    userBackpackProps.setPropsResources(toPropsResources(useProps.getPropsResources()));
    userBackpackProps.setExpireTime(TimestampUtils.convert(useProps.getExpireTime()));
    return userBackpackProps;
  }

  PropsResources toPropsResources(UserPropsResourcesDTO userPropsResourcesDTO);

  PropsResourcesDTO toPropsResourcesDTO(PropsResources propsResources);


  /**
   * 注意： UsePropsDTO.expireTime 和 UserBackpackProps.expireTime 类型不一样， 手动转换(方法会被编译后的toList/toMap)引用.
   */
  default UsePropsDTO toUsePropsDTO(UserBackpackProps userBackpackProps) {
    if (Objects.isNull(userBackpackProps)) {
      return null;
    }
    UsePropsDTO useProps = new UsePropsDTO();
    useProps.setUserId(userBackpackProps.getUserId());
    useProps.setPropsResources(toPropsResourcesDTO(userBackpackProps.getPropsResources()));
    useProps.setExpireTime(Objects.nonNull(userBackpackProps.getExpireTime())
        ? userBackpackProps.getExpireTime().getTime()
        : null);
    return useProps;
  }

  List<PropsStoreCommodityInfoCO> toListPropsStoreCommodityInfoCO(
      List<PropsStoreCommodity> propsStoreCommodities);

  NobleVipAbilityCO toNobleVipAbilityCO(NobleVipAbility nobleVipAbility);

  List<ActivityResourceCO> toActivityResourceCOList(List<ActivityResource> list);

}

