package com.hz.pm.api.meta.assembler;

import com.hz.pm.api.meta.model.entity.MetaTag;
import com.hz.pm.api.meta.model.dto.TagDTO;
import com.hz.pm.api.meta.model.dto.TagTreeDTO;
import com.hz.pm.api.meta.model.vo.ResTagTreeVO;
import org.apache.commons.collections4.CollectionUtils;

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

/**
 * <p>
 * MetaTagAssembler
 * </p>
 *
 * @author WendyYang
 * @since 17:06 2024/1/8
 */
public class MetaTagAssembler {

    private MetaTagAssembler() {
    }

    public static TagDTO convert(MetaTag metaTag) {
        TagDTO tagDTO = new TagDTO();
        tagDTO.setParentCode(metaTag.getParentCode());
        tagDTO.setTagCode(metaTag.getTagCode());
        tagDTO.setTagLevel(metaTag.getTagLevel());
        tagDTO.setTagName(metaTag.getTagName());
        return tagDTO;
    }

    public static List<TagTreeDTO> convert(List<TagDTO> tagList) {
        if (CollectionUtils.isEmpty(tagList)) {
            return Collections.emptyList();
        }
        return tagList.stream().map(MetaTagAssembler::convert).collect(Collectors.toList());

    }

    public static TagTreeDTO convert(TagDTO tagDTO) {
        TagTreeDTO tagTreeDTO = new TagTreeDTO();
        tagTreeDTO.setParentCode(tagDTO.getParentCode());
        tagTreeDTO.setTagCode(tagDTO.getTagCode());
        tagTreeDTO.setTagLevel(tagDTO.getTagLevel());
        tagTreeDTO.setTagName(tagDTO.getTagName());
        return tagTreeDTO;
    }

    public static List<ResTagTreeVO> to(List<TagTreeDTO> tagTreeDTOList) {
        List<ResTagTreeVO> resTagTreeVOList = new ArrayList<>();
        for (TagTreeDTO tagTreeDTO : tagTreeDTOList) {
            ResTagTreeVO resTagTreeVO = new ResTagTreeVO();
            resTagTreeVO.setTagLevel(tagTreeDTO.getTagLevel());
            resTagTreeVO.setTagName(tagTreeDTO.getTagName());
            resTagTreeVO.setParentCode(tagTreeDTO.getParentCode());
            resTagTreeVO.setTagCode(tagTreeDTO.getTagCode());
            resTagTreeVO.setUnionCode(tagTreeDTO.getTagName() + "##" + tagTreeDTO.getTagCode());
            if (CollectionUtils.isNotEmpty(tagTreeDTO.getChildren())) {
                resTagTreeVO.setChildren(to(tagTreeDTO.getChildren()));
            }
            resTagTreeVOList.add(resTagTreeVO);
        }
        return resTagTreeVOList;
    }
}
