package com.ningdatech.pmapi.sys.convert;

import cn.hutool.core.collection.CollUtil;
import com.ningdatech.pmapi.sys.model.entity.Region;
import com.ningdatech.pmapi.sys.model.dto.RegionDTO;
import com.ningdatech.pmapi.sys.model.dto.RegionTreeDTO;
import com.ningdatech.pmapi.sys.model.vo.RegionTreeVO;
import org.apache.commons.collections4.CollectionUtils;

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

/**
 * <p>
 * RegionConverter
 * </p>
 *
 * @author WendyYang
 * @since 10:31 2022/10/12
 */
public class RegionConverter {

    public static RegionDTO toRegionDTO(Region region) {
        RegionDTO dto = new RegionDTO();
        dto.setRegionLevel(region.getRegionLevel());
        dto.setRegionName(region.getRegionName());
        dto.setParentCode(region.getParentCode());
        dto.setRegionCode(region.getRegionCode());
        dto.setId(region.getId());
        dto.setDeleted(region.getDeleted());
        dto.setGovUnit(region.getGovUnit());
        dto.setParentId(region.getParentId());
        dto.setRegionCodePath(region.getRegionCodePath());
        dto.unionCode();
        return dto;
    }

    public static List<RegionTreeDTO> toRegionTreeDTOList(List<RegionDTO> regionDTOList) {
        if (CollectionUtils.isEmpty(regionDTOList)) {
            return new ArrayList<>();
        }
        return regionDTOList.stream().map(RegionConverter::toRegionTreeDTO).collect(Collectors.toList());
    }

    public static RegionTreeDTO toRegionTreeDTO(RegionDTO regionDTO) {
        RegionTreeDTO treeDto = new RegionTreeDTO();
        treeDto.setLevel(regionDTO.getRegionLevel());
        treeDto.setName(regionDTO.getRegionName());
        treeDto.setParentCode(regionDTO.getParentCode());
        treeDto.setRegionCode(regionDTO.getRegionCode());
        treeDto.setUnionCode(regionDTO.getUnionCode());
        treeDto.setId(regionDTO.getId());
        treeDto.setDeleted(regionDTO.getDeleted());
        treeDto.setGovUnit(regionDTO.getGovUnit());
        return treeDto;
    }

    public static RegionTreeVO toRegionTreeVO(RegionDTO region) {
        RegionTreeVO node = new RegionTreeVO();
        node.setRegionLevel(region.getRegionLevel());
        node.setName(region.getRegionName());
        node.setParentCode(region.getParentCode());
        node.setRegionCode(region.getRegionCode());
        node.setUnionCode(region.getUnionCode());
        node.setId(region.getId());
        node.setUnionCode(region.getUnionCode());
        node.setGovUnit(region.getGovUnit());
        return node;
    }

    public static List<RegionTreeVO> toRegionTree(Long parentId, Map<Long, List<RegionDTO>> regions,
                                                  boolean showDeleted) {
        List<RegionTreeVO> treeList = new ArrayList<>();
        List<RegionDTO> children = regions.getOrDefault(parentId, Collections.emptyList());
        for (RegionDTO region : children) {
            if (!showDeleted && region.getDeleted()) {
                continue;
            }
            RegionTreeVO node = toRegionTreeVO(region);
            List<RegionDTO> regionList = regions.get(region.getId());
            if (CollUtil.isNotEmpty(regionList)) {
                node.setChildren(toRegionTree(region.getId(), regions, showDeleted));
            }
            treeList.add(node);
        }
        return treeList;
    }
}
