package com.ningdatech.pmapi.sys.convert;

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.getName());
        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.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 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 treeNode = new RegionTreeVO();
            treeNode.setRegionLevel(region.getRegionLevel());
            treeNode.setName(region.getRegionName());
            treeNode.setParentCode(region.getParentCode());
            treeNode.setRegionCode(region.getRegionCode());
            List<RegionDTO> regionList = regions.get(region.getId());
            if (CollectionUtils.isNotEmpty(regionList)) {
                treeNode.setChildren(toRegionTree(region.getId(), regions, showDeleted));
            }
            treeNode.setId(region.getId());
            treeNode.setUnionCode(region.getUnionCode());
            treeNode.setGovUnit(region.getGovUnit());
            treeList.add(treeNode);
        }
        return treeList;
    }
}
