package com.ningdatech.pmapi.common.helper.impl;

import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.common.helper.basic.AbstractRegionLimitHelper;
import com.ningdatech.pmapi.expert.service.IExpertUserFullInfoService;
import com.ningdatech.pmapi.meta.model.ExpertRegionInfo;
import com.ningdatech.pmapi.meta.model.bo.RegionContainsBO;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2022/8/1 下午2:27
 */
@Component
public class RegionLimitHelperImpl extends AbstractRegionLimitHelper {

    private static final Logger logger = LoggerFactory.getLogger(RegionLimitHelperImpl.class);

    public RegionLimitHelperImpl(RegionCacheHelper regionCache, IExpertUserFullInfoService expertUserFullInfoService) {
        super(regionCache, expertUserFullInfoService);
    }

    public static Boolean contains(Integer adminRegionLevel, List<String> adminAllContainsRegionCodes
            , Integer expertRegionLevel, String expertRegionCode) {
        for (String adminAllContainsRegionCode : adminAllContainsRegionCodes) {
            if (adminAllContainsRegionCode.equals(expertRegionCode) && adminRegionLevel >= expertRegionLevel) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean regionContainsCheck(Long expertUserId, Long expertAdminUserId) {
        List<RegionContainsBO> expertAdminContainsRegion = getExpertAdminContainsRegion(expertAdminUserId);
        ExpertRegionInfo expertRegionInfo = getExpertRegionInfo(expertUserId);
        return regionContains(expertAdminContainsRegion, expertRegionInfo.getRegionCode(), expertRegionInfo.getRegionLevel());
    }

    @Override
    public Boolean regionContains(List<RegionContainsBO> regionContainsBOList, String regionCode, Integer regionLevel) {
        for (RegionContainsBO regionContainsBO : regionContainsBOList) {
            Integer parentRegionTreeLevel = regionContainsBO.getParentRegionTreeLevel();
            List<String> containsRegionCodeList = regionContainsBO.getContainsRegionCodeList();
            for (String containsRegionCode : containsRegionCodeList) {
                if (containsRegionCode.equals(regionCode) && parentRegionTreeLevel <= regionLevel) {
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    public List<RegionContainsBO> queryContainsRegionAssembler(RegionContainsBO queryRegionContainBo, List<RegionContainsBO> expertAdminContainsRegionList) {
        List<RegionContainsBO> resultBoList = new ArrayList<>();
        if (Objects.nonNull(queryRegionContainBo)) {
            resultBoList.add(queryRegionContainBo);
            Integer parentRegionTreeLevel = queryRegionContainBo.getParentRegionTreeLevel();
            List<String> containsRegionCodeList = queryRegionContainBo.getContainsRegionCodeList();

            // 查询取并级
            if (CollectionUtils.isNotEmpty(expertAdminContainsRegionList)) {
                for (RegionContainsBO regionContainsBO : expertAdminContainsRegionList) {
                    regionContainsBO.setParentRegionTreeLevel(parentRegionTreeLevel);
                    List<String> regionCodeList = regionContainsBO.getContainsRegionCodeList();
                    regionCodeList = regionCodeList.stream().filter(containsRegionCodeList::contains).collect(Collectors.toList());
                    if (CollectionUtils.isNotEmpty(regionCodeList)) {
                        regionContainsBO.setContainsRegionCodeList(regionCodeList);
                        resultBoList.add(regionContainsBO);
                    }
                }
            }
            return resultBoList;
        } else {
            return expertAdminContainsRegionList;
        }
    }

    @Override
    public RegionContainsBO getContainsRegionBo(Integer regionLevel, String regionCode) {
        Collection<String> regionCodes = regionCache.listChildRegionCodeList(regionCode, regionLevel);
        RegionContainsBO regionContains = new RegionContainsBO();
        regionContains.setContainsRegionCodeList(new ArrayList<>(regionCodes));
        regionContains.setParentRegionTreeLevel(regionLevel);
        return regionContains;
    }

    @Override
    public List<RegionContainsBO> getExpertAdminContainsRegion(Long expertAdminUserId) {
        if (Objects.isNull(expertAdminUserId)) {
            logger.error("getExpertAdminContainsRegion expertAdminUserId is null");
            return new ArrayList<>();
        }
        // TODO
        /*LambdaQueryWrapper<ExpertAdminRegion> expertAdminRegionEq = Wrappers.lambdaQuery(ExpertAdminRegion.class)
                .eq(ExpertAdminRegion::getUserId, expertAdminUserId);
        List<ExpertAdminRegion> expertAdminRegionList = iExpertAdminRegionService.list(expertAdminRegionEq);
        List<RegionContainsBO> regionContainsBOList = new ArrayList<>();
        for (ExpertAdminRegion expertAdminRegion : expertAdminRegionList) {
            List<String> regionCodes = regionCacheHelper
                    .getAllChildrenRegionCodeList(expertAdminRegion.getRegionCode(), expertAdminRegion.getRegionLevel());
            RegionContainsBO regionContainsBO = new RegionContainsBO();
            regionContainsBO.setContainsRegionCodeList(regionCodes);
            regionContainsBO.setParentRegionTreeLevel(expertAdminRegion.getRegionLevel());
            regionContainsBOList.add(regionContainsBO);
        }*/
        return null;
    }


}
