package com.tanpu.community.util;

import com.tanpu.community.api.beans.qo.TopicTitileQo;
import com.tanpu.community.api.beans.TopicDO;
import com.tanpu.community.api.beans.qo.CommentLv2Qo;
import com.tanpu.community.api.beans.qo.CommentQo;
import com.tanpu.community.api.beans.qo.FollowQo;
import com.tanpu.community.api.beans.qo.ThemeQo;
import com.tanpu.community.api.beans.req.theme.CreateThemeReq;
import com.tanpu.community.api.enums.DeleteTagEnum;
import com.tanpu.community.dao.entity.community.*;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class ConvertUtil {

    public static ThemeQo themeEntityToDTO(ThemeEntity themeEntity) {
        ThemeQo themeQO = new ThemeQo();
        BeanUtils.copyProperties(themeEntity, themeQO);
        return themeQO;
    }


    public static ThemeEntity themeDTOToEntity(ThemeQo themeQO) {
        ThemeEntity themeEntity = new ThemeEntity();
        BeanUtils.copyProperties(themeQO, themeEntity);
        return themeEntity;
    }

    public static List<ThemeQo> themeEntitiesToDTOs(List<ThemeEntity> themeEntities) {
        return themeEntities.stream().map(ConvertUtil::themeEntityToDTO).collect(Collectors.toList());
    }


    public static List<ThemeEntity> themeDTOSToEntitys(List<ThemeQo> themeQos) {
        return themeQos.stream().map(ConvertUtil::themeDTOToEntity).collect(Collectors.toList());
    }


    public static TopicDO topicEntityToDTO(TopicEntity topicEntity) {
        TopicDO topicDO = new TopicDO();
        BeanUtils.copyProperties(topicEntity, topicDO);
        return topicDO;
    }

    public static TopicTitileQo topicToBriefInfoDTO(TopicEntity topicEntity) {
        TopicTitileQo topicTitileQo = new TopicTitileQo();
        BeanUtils.copyProperties(topicEntity, topicTitileQo);
        return topicTitileQo;
    }
    public static CommentQo commentEntity2Qo(CommentEntity entity) {
        CommentQo qo = new CommentQo();
        BeanUtils.copyProperties(entity, qo);
        return qo;
    }

    public static List<CommentQo> commentEntity2Qos(List<CommentEntity> entities){
        return entities.stream().map(ConvertUtil::commentEntity2Qo).collect(Collectors.toList());
    }

    public static CommentLv2Qo commentLv2Entity2Qo(CommentEntity entity) {
        CommentLv2Qo qo = new CommentLv2Qo();
        BeanUtils.copyProperties(entity, qo);
        return qo;
    }

    public static List<CommentLv2Qo> commentLv2Entity2Qos(List<CommentEntity> entities){
        return entities.stream().map(ConvertUtil::commentLv2Entity2Qo).collect(Collectors.toList());
    }

    public static List<TopicDO> topicEntitiesToDTOs(List<TopicEntity> topicEntities) {
        return topicEntities.stream().map(ConvertUtil::topicEntityToDTO).collect(Collectors.toList());
    }

    public static List<TopicTitileQo> topicEntitiesToBriefDTOs(List<TopicEntity> topicEntities) {
        return topicEntities.stream().map(ConvertUtil::topicToBriefInfoDTO).collect(Collectors.toList());
    }




    public static DeleteTagEnum deleteTagShift(DeleteTagEnum deleteTagEnum) {
        if (deleteTagEnum.getCode().equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED;
        } else {
            return DeleteTagEnum.NOT_DELETED;
        }
    }

    public static Integer deleteTagShift(Integer deleteTag) {
        if (deleteTag.equals(DeleteTagEnum.NOT_DELETED.getCode())) {
            return DeleteTagEnum.DELETED.getCode();
        } else {
            return DeleteTagEnum.NOT_DELETED.getCode();
        }
    }

    public static List<ThemeAttachmentEntity> themeReqToAttachmentList(CreateThemeReq req, String themeId) {
        if (req == null || req.getAttachment() == null || req.getAttachment().size() == 0) {
            return Collections.emptyList();
        }
        List<ThemeAttachmentEntity> list = new ArrayList<>();
        req.getAttachment().forEach((k, v) -> {
            list.add(ThemeAttachmentEntity.builder()
                    .attachType(Integer.valueOf(k))
                    .attachId(v)
                    .themeId(themeId)
                    .build());
        });
        return list;
    }

    public static FollowQo homePageEntity2FollowQo(HomePageEntity entity) {
        if (entity == null) {
            return null;
        }
        FollowQo followQo = new FollowQo();
        BeanUtils.copyProperties(entity, followQo);
        return followQo;
    }


}