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

import com.red.circle.framework.core.convertor.ConvertorModel;
import com.red.circle.other.infra.database.rds.entity.badge.BadgeConfig;
import com.red.circle.other.infra.database.rds.entity.props.PropsBackpack;
import com.red.circle.other.infra.database.rds.entity.props.PropsCommodityStore;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.inner.model.dto.material.PropsResourcesDTO;
import com.red.circle.other.inner.model.dto.material.UseBadgeDTO;
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.PropsStoreCommodity;
import com.red.circle.other.inner.model.dto.material.props.UserBackpackProps;
import java.util.List;
import java.util.Objects;
import org.mapstruct.Mapper;

/**
 * @author pengliang on 2021/5/29
 */
@Mapper(componentModel = ConvertorModel.SPRING)
public interface PropsMaterialInfraConvertor {

  UserBackpackProps toUserBackpackProps(PropsBackpack propsBackpack);

  PropsStoreCommodity toPropsStoreCommodity(PropsCommodityStore propsCommodityStore);

  PropsResourcesDTO toPropsResourcesDTO(PropsSourceRecord propsSourceRecord);

  UseBadgeDTO toUseBadgeDTO(BadgeConfig badgeConfig);

  PropsResources toPropsSources(PropsSourceRecord propsSourceRecord);

  PropsResourcesDTO toPropsResourcesDTO(PropsResources propsResources);

  PropsResources toPropsResources(PropsSourceRecord propsSourceRecord);


  /**
   * 注意： 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;
  }

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


}
