package com.diagbot.facade;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.dto.BasDeptInfoDTO;
import com.diagbot.dto.BehospitalInfoAgeDTO;
import com.diagbot.dto.CheckWorkDTO;
import com.diagbot.dto.MedicalRecordDTO;
import com.diagbot.dto.MedoupDTO;
import com.diagbot.dto.RegionDTO;
import com.diagbot.dto.RegionMedoupDTO;
import com.diagbot.entity.BasDoctorInfo;
import com.diagbot.entity.DeptMedoup;
import com.diagbot.entity.MedBehospitalType;
import com.diagbot.entity.MedCheckInfo;
import com.diagbot.entity.Medoup;
import com.diagbot.entity.Region;
import com.diagbot.entity.RegionDept;
import com.diagbot.entity.SysTaskCron;
import com.diagbot.entity.SysUser;
import com.diagbot.enums.CheckJobTypeEnum;
import com.diagbot.enums.CheckerRoleEnum;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.DeptMedoupServiceImpl;
import com.diagbot.service.impl.MedBehospitalTypeServiceImpl;
import com.diagbot.service.impl.MedCheckInfoServiceImpl;
import com.diagbot.service.impl.MedicalRecordServiceImpl;
import com.diagbot.service.impl.MedoupServiceImpl;
import com.diagbot.service.impl.RegionDeptServiceImpl;
import com.diagbot.service.impl.RegionServiceImpl;
import com.diagbot.util.DateUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.SpringContextUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.util.SysUserUtils;
import com.diagbot.vo.CheckWorkPageVO;
import com.diagbot.vo.MedCheckWorkAddVO;
import com.diagbot.vo.RecordTypeVO;
import com.diagbot.vo.SysUserBaseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * @author songxl
 * @time: 2024-05-11
 */
@Slf4j
@Component
public class MedCheckWorkFacade {
    @Autowired
    SysUserFacade sysUserFacade;
    @Autowired
    private MedCheckInfoServiceImpl medCheckInfoServiceImpl;
    @Autowired
    private MedCheckInfoFacade medCheckInfoFacade;
    @Autowired
    private MedBehospitalTypeServiceImpl medBehospitalTypeServiceImpl;
    @Autowired
    private MedicalRecordServiceImpl medicalRecordServiceImpl;
    @Autowired
    private SysTaskCronFacade sysTaskCronFacade;
    @Autowired
    private BehospitalInfoFacade behospitalInfoFacade;
    @Autowired
    private RegionDeptServiceImpl regionDeptService;
    @Autowired
    private RegionServiceImpl regionService;
    @Autowired
    private DeptMedoupServiceImpl deptMedoupService;
    @Autowired
    private MedoupServiceImpl medoupService;
    @Autowired
    private BasDoctorInfoFacade medoupUserService;
    @Autowired
    private BasDoctorInfoFacade basDoctorInfoFacade;
    @Autowired
    private SysHospitalSetFacade sysHospitalSetFacade;

    /**
     * @Author songxl
     * @Description 批量操作核查任务（增加和删除）
     * @time: 2024/5/11
     * @Param [medCheckWorkVO]
     * @Return java.lang.Boolean
     * @MethodName addCheck
     */
    public Boolean addCheckWork(MedCheckWorkAddVO medCheckWorkVO) {

        boolean flag = false;
        Date now = DateUtil.now();
        //获取核查任务类型(0-科室任务 1-院级 2-质控科)
        Integer workType = medCheckWorkVO.getJobType();
        //获取操作类型（0-取消，1-添加）
        Integer checkStatus = medCheckWorkVO.getCheckStatus();
        Long hospitalId = Long.parseLong(SysUserUtils.getCurrentHospitalID());
        Long principleId = Long.valueOf(SysUserUtils.getCurrentPrincipleID());
        SysUser user = sysUserFacade.getOne(new QueryWrapper<SysUser>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id", principleId)
                .eq("status", 1));
        String principleName = user.getLinkman();
        //取消核查任务【操作类型（0-取消，1-添加）】
        if (checkStatus != null && checkStatus == 0) {
            //判断核查任务是否生成
            QueryWrapper<MedCheckInfo> medicalRecordQe = new QueryWrapper<>();
            medicalRecordQe.eq("hospital_id", hospitalId);
            medicalRecordQe.eq("is_deleted", IsDeleteEnum.N.getKey());
            if (medCheckWorkVO.getBehospitalCodeList().size() > 0) {
                medicalRecordQe.in("behospital_code", medCheckWorkVO.getBehospitalCodeList());
            }
            List<MedCheckInfo> list = medCheckInfoFacade.list(medicalRecordQe);
            if (ListUtil.isEmpty(list)) {
                throw new CommonException(CommonErrorCode.UPDATE_INFO_FAIL, "存在未生成核查任务！");
            }

            //创建用户集合
            Set<Long> creaters = list.stream().map(MedCheckInfo::getJobCreator).collect(Collectors.toSet());
            if (creaters != null) {
                //移除当前用户如果还有其他用户
                creaters.remove(principleId);
                if (creaters.size() > 0) {
                    throw new CommonException(CommonErrorCode.UPDATE_INFO_FAIL, "无法取消他人生成任务！");
                }
            }
            int i = medCheckInfoFacade.getBaseMapper().deleteBatchCodes(medCheckWorkVO.getBehospitalCodeList());
            if (i != 0) {
                return true;
            }
        } else if (checkStatus != null && checkStatus == 1) {
            //生成核查任务
            MedCheckInfo data = medCheckInfoFacade.getOne(new QueryWrapper<MedCheckInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("hospital_id", hospitalId)
                    .in("behospital_code", medCheckWorkVO.getBehospitalCodeList())
            );
            //任务是否存在
            if (data != null) {
                throw new CommonException(CommonErrorCode.UPDATE_INFO_FAIL, "核查任务已存在！");
            }

            //该病历没有核查的时候， 就添加核查记录
            List<MedCheckInfo> medCheckInfoList = new ArrayList<>();
            for (String behospitalCode : medCheckWorkVO.getBehospitalCodeList()) {
                MedCheckInfo medCheckInfo = new MedCheckInfo();
                medCheckInfo.setIsDeleted(IsDeleteEnum.N.getKey());
                medCheckInfo.setGmtCreate(now);
                medCheckInfo.setBehospitalCode(behospitalCode);
                medCheckInfo.setHospitalId(hospitalId);
                medCheckInfo.setCheckType(medCheckWorkVO.getCheckType());
                medCheckInfo.setJobCreator(principleId);
                medCheckInfo.setJobCreatorName(principleName);
                medCheckInfo.setJobCreateTime(now);
                medCheckInfo.setJobType(medCheckWorkVO.getJobType());
                medCheckInfoList.add(medCheckInfo);
            }
            flag = medCheckInfoServiceImpl.saveBatch(medCheckInfoList);
        }
        return flag;
    }

    /**
     * @Author songxl
     * @Description 获取每日住院患者的类型（出院、病危、抢救。。。）
     * @time: 2024/5/11
     * @Param [param]
     * @Return void
     * @MethodName execute
     */
    public void execute(String param) {
        JSONObject paramJson = JSONObject.parseObject(param);
        JSONObject typeValue = paramJson.getJSONObject("typeVal");
        JSONObject typeName = paramJson.getJSONObject("typeName");
        int searchSize = paramJson.getInteger("searchSize");
        int batchSize = paramJson.getInteger("batchSize");


        //定义批量插入的集合
        HashMap<String, MedBehospitalType> medBehospitalTypeMap = new HashMap<>();
        //1.获取有指定文书、没有患者类型的患者数据
        RecordTypeVO recordContentVO = new RecordTypeVO();
        recordContentVO.setModeList(new ArrayList<>(typeValue.keySet()));
        List<MedicalRecordDTO> medicalRecordList = medicalRecordServiceImpl.getMedicalRecord(recordContentVO);
        if (medicalRecordList != null && !medicalRecordList.isEmpty()) {
            getPatientRecordType(medBehospitalTypeMap, medicalRecordList, typeValue, typeName);
        }
        //2.批量执行插入操作
        try {
            if (medBehospitalTypeMap != null && medBehospitalTypeMap.size() > 0) {
                List<MedBehospitalType> medBehospitalTypeList = new ArrayList<>(medBehospitalTypeMap.values());
                medBehospitalTypeServiceImpl.saveOrUpdateBatch(medBehospitalTypeList, batchSize);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * @Author songxl
     * @Description 获取患者类型存入批量插入map
     * @time: 2024/5/11
     * @Param [medBehospitalTypeMap, medicalRecordList, typeValue, typeName, hospiatlId]
     * @Return void
     * @MethodName getPatientRecordType
     */
    private void getPatientRecordType(HashMap<String, MedBehospitalType> medBehospitalTypeMap,
                                      List<MedicalRecordDTO> medicalRecordList,
                                      JSONObject typeValue,
                                      JSONObject typeName) {

        if (medicalRecordList == null || medicalRecordList.isEmpty()) {
            return;
        }
        //1.遍历查询出来的结果
        for (MedicalRecordDTO medicalRecordDTO : medicalRecordList) {
            //2.判断批量插入map中有没有这个患者
            if (medBehospitalTypeMap.containsKey(medicalRecordDTO.getBehospitalCode())) {
                //3.比较这两次的分值大小
                int lval = medBehospitalTypeMap.get(medicalRecordDTO.getBehospitalCode()).getValue();
                int nval = typeValue.getInteger(medicalRecordDTO.getModeId() + "");
                if (nval > lval) {
                    medBehospitalTypeMap.get(medicalRecordDTO.getBehospitalCode()).setValue(nval);
                    medBehospitalTypeMap.get(medicalRecordDTO.getBehospitalCode())
                            .setBehospitalType(typeName.getString(medicalRecordDTO.getModeId() + ""));
                }
            } else {
                //4.获取分值记录这个患者
                MedBehospitalType medBehospitalType = new MedBehospitalType();
                medBehospitalType.setBehospitalCode(medicalRecordDTO.getBehospitalCode());
                medBehospitalType.setBehospitalType(typeName.getString(medicalRecordDTO.getModeId() + ""));
                medBehospitalType.setValue(typeValue.getInteger(medicalRecordDTO.getModeId() + ""));
                medBehospitalType.setCreateTime(DateUtil.now());
                medBehospitalType.setHospitalId(medicalRecordDTO.getHospitalId());
                medBehospitalTypeMap.put(medicalRecordDTO.getBehospitalCode(), medBehospitalType);
            }
        }
    }


    /**
     * 分页
     *
     * @param checkWorkPageVO
     * @return
     */
    public IPage<CheckWorkDTO> pageFac(CheckWorkPageVO checkWorkPageVO) {
        checkWorkPageSet(checkWorkPageVO);
        IPage<CheckWorkDTO> res = medCheckInfoFacade.getBaseMapper().getCheckWorkPage(checkWorkPageVO);
        if (res == null || ListUtil.isEmpty(res.getRecords())) {
            return res;
        }

        List<String> behospitalCodes = res.getRecords().stream().map(CheckWorkDTO::getBehospitalCode).collect(Collectors.toList());
        Map<String, BehospitalInfoAgeDTO> ageMap = behospitalInfoFacade.getBehospitalInfoAge(checkWorkPageVO.getHospitalId(), behospitalCodes).stream().collect(Collectors.toMap(i -> i.getBehospitalCode(), i -> i));
        res.getRecords().forEach(i -> {
            i.setAge(ageMap.get(i.getBehospitalCode()) == null ? "" : ageMap.get(i.getBehospitalCode()).getAge());
        });

        //更新delFlag
        Long principleId = Long.valueOf(SysUserUtils.getCurrentPrincipleID());
        res.getRecords().stream().forEach(checkWorkDTO -> {
            if (principleId.equals(checkWorkDTO.getJobCreator())) {
                checkWorkDTO.setDelFlag(true);
            }
        });
        return res;
    }

    private void checkWorkPageSet(CheckWorkPageVO checkWorkPageVO) {
        //入参验证
        //出院时间
        if (null != checkWorkPageVO && null != checkWorkPageVO.getStartDate()) {
            checkWorkPageVO.setStartDate(DateUtil.getFirstTimeOfDay(checkWorkPageVO.getStartDate()));
        }
        if (null != checkWorkPageVO && null != checkWorkPageVO.getEndDate()) {
            checkWorkPageVO.setEndDate(DateUtil.getFirstTimeOfDay(DateUtil.addDay(checkWorkPageVO.getEndDate(), 1)));
        }
        if (null != checkWorkPageVO && null != checkWorkPageVO.getStartDate() && null != checkWorkPageVO.getEndDate()) {
            if (DateUtil.after(checkWorkPageVO.getStartDate(), checkWorkPageVO.getEndDate())) {
                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "出院时间的开始时间必须小于结束时间！");
            }
        }
        //住院天数
        if (null != checkWorkPageVO && null != checkWorkPageVO.getSmallDay() && null != checkWorkPageVO.getBigDay()) {
            if (checkWorkPageVO.getSmallDay() > checkWorkPageVO.getBigDay()) {
                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "住院天数区间有误！");
            }
        }
        checkWorkPageVO.setHospitalId(Long.parseLong(SysUserUtils.getCurrentHospitalID()));
        //科室任务-获取本科室的用户(改为登录用户科室所在医疗组，通过医疗组获取医生再获取医生的患者）
        if (StringUtil.isNotBlank(checkWorkPageVO.getJobType()) && (CheckJobTypeEnum.DEPT_SUPERVISOR.getKey() + "").equals(checkWorkPageVO.getJobType())) {
//            SysUserBaseVO sysUserBaseVO = new SysUserBaseVO();
//            sysUserBaseVO.setUserId(Long.parseLong(SysUserUtils.getCurrentPrincipleID()));
//            List<String> deptIds = Lists.newArrayList();
//            List<BasDeptInfoDTO> selDepts = sysUserFacade.getUserDepts(sysUserBaseVO).getSelDepts();
//            if (ListUtil.isNotEmpty(selDepts)) {
//                deptIds = selDepts.stream().map(i -> i.getDeptId()).collect(Collectors.toList());
//            }
//            if (ListUtil.isNotEmpty(checkWorkPageVO.getDepartment())) {
//                deptIds.retainAll(checkWorkPageVO.getDepartment());
//            }
//            if (ListUtil.isEmpty(deptIds)) {
//                deptIds.add("999999999999");
//            }
//            checkWorkPageVO.setDepartment(deptIds);
            //如果没有添加医疗组筛选条件默认查询科室所有数据+医疗组所有人数据
            List<String> codes = null;
            if (ListUtil.isEmpty(checkWorkPageVO.getMedoups())) {
                //获取当前用户科室
                SysUserBaseVO sysUserBaseVO = new SysUserBaseVO();
                sysUserBaseVO.setUserId(Long.parseLong(SysUserUtils.getCurrentPrincipleID()));
                List<String> deptIds = new ArrayList<>();
                List<BasDeptInfoDTO> selDepts = sysUserFacade.getUserDepts(sysUserBaseVO).getSelDepts();
                if (ListUtil.isNotEmpty(selDepts)) {
                    deptIds = selDepts.stream().map(i -> i.getDeptId()).collect(Collectors.toList());
                }
                //通过科室获取医疗组
                List<DeptMedoup> deptMedoups = deptMedoupService.list(new QueryWrapper<DeptMedoup>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("dept_id", deptIds));
                if (ListUtil.isNotEmpty(deptMedoups)) {
                    codes = deptMedoups.stream().map(DeptMedoup::getMedoupCode).collect(Collectors.toList());
                }
            } else {
                codes = checkWorkPageVO.getMedoups();
            }
            if (ListUtil.isNotEmpty(codes)) {
                List<BasDoctorInfo> basDoctorInfos = basDoctorInfoFacade.list(new QueryWrapper<BasDoctorInfo>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("group_id", codes)
                        .or().isNull("group_id")
                        .in(ListUtil.isNotEmpty(checkWorkPageVO.getDepts()), "dept_id", checkWorkPageVO.getDepts()));
                checkWorkPageVO.setDoctorIds(basDoctorInfos.stream().map(BasDoctorInfo::getDoctorId).collect(Collectors.toList()));
            }
            //获取VIP开房病区科室id
            String deptsStr = sysHospitalSetFacade.getValue(Long.parseLong(SysUserUtils.getCurrentHospitalID()), "check_order_info");
            if (StringUtil.isNotEmpty(deptsStr)) {
                checkWorkPageVO.setOrderDepts(Arrays.asList(deptsStr.split(",")));
            }
        }
    }

    public boolean createMedBeHospitalInfoType() {
        //1.获取定时任务
        SysTaskCron task = sysTaskCronFacade.getOne(new QueryWrapper<SysTaskCron>()
                .eq("cron_code", "TASK019"), false);
        //2.执行定时任务
        if (null != task && StringUtil.isNotBlank(task.getCron())) {
            try {
                execute(task.getParam());
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * @param
     * @Description获取当前用户的病区信息
     * @Return java.util.List<com.diagbot.dto.RegionDTO>
     */
    public RegionMedoupDTO getLocalRegionMedoup() {

        Long userId = Long.parseLong(SysUserUtils.getCurrentPrincipleID());
        SysUserBaseVO sysUserRoleBaseVO = new SysUserBaseVO();
        sysUserRoleBaseVO.setUserId(userId);
        List<Long> roleIds = sysUserFacade.getUserRoles(sysUserRoleBaseVO).getSelRoles().stream().map(i -> i.getId()).collect(Collectors.toList());
        if (ListUtil.isEmpty(roleIds)) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "该用户无角色信息");
        }
        RegionMedoupDTO regionMedoup = new RegionMedoupDTO();
        SysUserBaseVO sysUserBaseVO = new SysUserBaseVO();
        sysUserBaseVO.setUserId(Long.parseLong(SysUserUtils.getCurrentPrincipleID()));
        //获取当前登录用户的科室信息
        List<BasDeptInfoDTO> selDepts = sysUserFacade.getUserDepts(sysUserBaseVO).getSelDepts();
        //获取科室关联的病区
        List<RegionDTO> regions = getLocalRegion(selDepts);
        if (ListUtil.isEmpty(regions)) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "该用户无病区关联信息");
        }
        regionMedoup.setRegions(regions);
        //获取科室关联的医疗组
        List<MedoupDTO> medoups = getLocalMdeoup(selDepts);
        if (ListUtil.isEmpty(medoups)) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "当前用户所属科室无医疗组关联信息");
        }
        regionMedoup.setMedoups(medoups);
        //获取当前用户的医疗组信息
        List<String> userNames = sysUserFacade.list(new QueryWrapper<SysUser>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id", SysUserUtils.getCurrentPrincipleID())
        ).stream().map(SysUser::getUsername).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(userNames)) {
            List<String> medoupTemps = medoupUserService.list(new QueryWrapper<BasDoctorInfo>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("doctor_id", userNames)
            ).stream().map(BasDoctorInfo::getGroupId).collect(Collectors.toList());
            if (ListUtil.isNotEmpty(medoups)) {
                regionMedoup.setLocalMedoupCode(medoupTemps.get(0));
            }
        }
        if (roleIds.contains(CheckerRoleEnum.DEPT_SUPERVISOR.getKey() * 1l)) {
            regionMedoup.setShowAllFlag(true);
        }
        return regionMedoup;
    }

    private List<MedoupDTO> getLocalMdeoup(List<BasDeptInfoDTO> selDepts) {
        List<MedoupDTO> medoups = new ArrayList<>();
        if (ListUtil.isNotEmpty(selDepts)) {
            List<String> deptIds = selDepts.stream().map(BasDeptInfoDTO::getDeptId).collect(Collectors.toList());
            //获取本人科室关联的医疗组code
            List<DeptMedoup> deptMedoups = deptMedoupService.list(new QueryWrapper<DeptMedoup>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("dept_id", deptIds));
            List<String> medoupCodes = null;
//            List<String> doctorIds = sysUserFacade.list(new QueryWrapper<SysUser>()
//                    .eq("is_deleted", IsDeleteEnum.N.getKey())
//                    .eq("id", SysUserUtils.getCurrentPrincipleID())
//            ).stream().map(SysUser::getUsername).collect(Collectors.toList());
            if (ListUtil.isNotEmpty(deptMedoups)) {
                medoupCodes = deptMedoups.stream().map(DeptMedoup::getMedoupCode).collect(Collectors.toList());
//                medoupCodes = medoupUserService.list(new QueryWrapper<BasDoctorInfo>()
//                        .eq("is_deleted", IsDeleteEnum.N.getKey())
//                        .in("doctor_id", doctorIds)
//                ).stream().map(BasDoctorInfo::getGroupId).collect(Collectors.toList());
            }

            if (ListUtil.isNotEmpty(medoupCodes)) {
                //通过code获取医疗组详情
                if (ListUtil.isNotEmpty(medoupCodes)) {
                    List<Medoup> medoupTemps = medoupService.list(new QueryWrapper<Medoup>()
                            .in("code", medoupCodes)
                            .eq("is_deleted", IsDeleteEnum.N.getKey()));
                    if (ListUtil.isNotEmpty(medoupTemps)) {
                        medoupTemps.stream().forEach(medoup -> {
                            MedoupDTO MedoupDTOTemp = new MedoupDTO();
                            BeanUtils.copyProperties(medoup, MedoupDTOTemp);
                            medoups.add(MedoupDTOTemp);
                        });
                    }
                }
            }
        }
        return medoups;
    }

    private List<RegionDTO> getLocalRegion(List<BasDeptInfoDTO> selDepts) {
        List<RegionDTO> regions = new ArrayList<>();
        if (ListUtil.isNotEmpty(selDepts)) {
            List<String> deptIds = selDepts.stream().map(BasDeptInfoDTO::getDeptId).collect(Collectors.toList());
            List<RegionDept> regionDepts = regionDeptService.list(new QueryWrapper<RegionDept>()
                    .eq("hospital_id", SysUserUtils.getCurrentHospitalID())
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .in("dept_id", deptIds));
            if (ListUtil.isNotEmpty(regionDepts)) {
                List<String> regionCodes = regionDepts.stream().map(RegionDept::getRegionCode).collect(Collectors.toList());
                if (ListUtil.isNotEmpty(regionCodes)) {
                    List<Region> regionTemps = regionService.list(new QueryWrapper<Region>()
                            .in("code", regionCodes)
                            .eq("is_deleted", IsDeleteEnum.N.getKey())
                            .eq("hospital_id", SysUserUtils.getCurrentHospitalID()));
                    if (ListUtil.isNotEmpty(regionTemps)) {
                        regionTemps.stream().forEach(region -> {
                            RegionDTO RegionDTOTemp = new RegionDTO();
                            BeanUtils.copyProperties(region, RegionDTOTemp);
                            regions.add(RegionDTOTemp);
                        });
                    }
                }
            }
        }
        return regions;
    }
}
