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

import com.red.circle.framework.core.convertor.ConvertorModel;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRewardConfig;
import com.red.circle.other.infra.database.rds.entity.activity.PropsActivityRewardGroup;
import com.red.circle.other.infra.database.rds.entity.props.PropsBackpack;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.inner.model.cmd.material.PropsActivityRewardConfigParamCmd;
import com.red.circle.other.inner.model.cmd.material.PropsActivityRewardGroupParamCmd;
import com.red.circle.other.inner.model.dto.material.PropsActivityRewardConfigInfoDTO;
import com.red.circle.other.inner.model.dto.material.PropsActivityRewardGroupDTO;
import com.red.circle.other.inner.model.dto.material.PropsBackpackDTO;
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.PropsResources;
import com.red.circle.other.inner.model.dto.material.props.UserBackpackProps;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.mapstruct.Mapper;

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

  PropsResourcesDTO toPropsResourcesDTO(PropsSourceRecord propsSourceRecord);

  default UsePropsDTO toUsePropsDTO(PropsBackpack propsBackpack,
      PropsSourceRecord propsSourceRecord) {
    if (Objects.isNull(propsBackpack) || Objects.isNull(propsSourceRecord)) {
      return null;
    }
    UsePropsDTO useProps = new UsePropsDTO();
    useProps.setUserId(propsBackpack.getUserId());
    useProps.setPropsResources(toPropsResourcesDTO(propsSourceRecord));
    useProps.setExpireTime(Objects.nonNull(propsBackpack.getExpireTime())
        ? propsBackpack.getExpireTime().getTime()
        : null);
    return useProps;
  }

  List<PropsBackpack> toListPropsBackpack(List<PropsBackpackDTO> props);

  List<PropsBackpackDTO> toListPropsBackpackDTO(List<PropsBackpack> props);

  PropsBackpack toPropsBackpack(PropsBackpackDTO props);

  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<UsePropsDTO> toListUsePropsDTO(List<UserBackpackProps> userBackpackProps);

  Map<Long, List<UsePropsDTO>> toMapUsePropsDTO(Map<Long, List<UserBackpackProps>> map);

  PropsActivityRewardGroupDTO toPropsActivityRewardGroupDTO(
      PropsActivityRewardGroup propsActivityRewardGroup);

  PropsActivityRewardConfigInfoDTO toPropsActivityRewardConfigInfoDTO(
      PropsActivityRewardConfig config);

  PropsActivityRewardConfig toPropsActivityRewardConfig(PropsActivityRewardConfigParamCmd config);

  PropsActivityRewardGroup toPropsActivityRewardGroup(
      PropsActivityRewardGroupParamCmd rewardGroupParam);
}

