package cn.teammodel.controller.admin.service.impl;

import cn.teammodel.controller.admin.service.TeacherService;
import cn.teammodel.model.dto.admin.teacher.TeacherDto;
import cn.teammodel.model.entity.common.GroupList;
import cn.teammodel.model.entity.school.School;
import cn.teammodel.model.entity.school.SchoolTeacher;
import cn.teammodel.repository.SchoolGroupListRepository;
import cn.teammodel.repository.SchoolRepository;
import cn.teammodel.repository.SchoolTeacherRepository;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private SchoolGroupListRepository schoolGroupListRepository;
    @Resource
    private SchoolTeacherRepository schoolTeacherRepository;
    @Resource
    private SchoolRepository  schoolRepository;
    @Override
    public List<Map<String, Integer>> getTeacherList(TeacherDto teacherDto) {
        List<Map<String, Integer>> result = new ArrayList<>();
       List<GroupList> groupLists = schoolGroupListRepository.findAllByCode("GroupList-" + teacherDto.getCode());
       List<SchoolTeacher> schoolTeachers = schoolTeacherRepository.findAllByCode("Teacher-" + teacherDto.getCode());
       List<School> school = schoolRepository.findBaseById(teacherDto.getCode());
       List<School.Subject> subjects = new ArrayList<>();
       List<School.Period> periods = new ArrayList<>();
       for (School sc : school) {
           if (sc.getPeriod() != null) {
               // 遍历每个 Period 对象
               for (School.Period period : sc.getPeriod()) {
                   periods.add(period);
                   // 检查 period 是否有 subjects 列表
                   if (period.getSubjects() != null) {
                       // 将 period 中的 subjects 添加到总的 subjects 列表中
                       subjects.addAll(period.getSubjects());
                   }
               }
           }

       }

        Map<String, Integer> updatedSubjectCountMap = getStringIntegerMap(subjects, schoolTeachers);

        // 创建一个映射，用于存储每个职位的教师数量
        Map<String, Integer> jobCountMap = new HashMap<>();

        // 遍历所有教师
        for (SchoolTeacher teacher : schoolTeachers) {
            // 获取教师的职位
            String job = teacher.getJob();
            if (job != null) {
                // 更新每个职位的教师数量
                jobCountMap.put(job, jobCountMap.getOrDefault(job, 0) + 1);
            }
        }
        jobCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
        Map<String, Integer> periodCountMap = getPeriodCountMap(groupLists, periods);

        periodCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
        updatedSubjectCountMap.entrySet().removeIf(entry -> entry.getKey().isEmpty());
        result.add(updatedSubjectCountMap);
        result.add(jobCountMap);
        result.add(periodCountMap);
        return result;
    }

    private static @NotNull Map<String, Integer> getStringIntegerMap(List<School.Subject> subjects, List<SchoolTeacher> schoolTeachers) {
        Map<String, String> subjectNameMap = new HashMap<>();
        for (School.Subject subject : subjects) {
            subjectNameMap.put(subject.getId(), subject.getName());
        }
        Map<String, Integer> subjectCountMap = new HashMap<>();

        // 遍历所有教师
        for (SchoolTeacher teacher : schoolTeachers) {
            // 获取教师的科目列表
            List<String> subjectIds = teacher.getSubjectIds();
            if (subjectIds != null) {
                for (String subjectId : subjectIds) {
                    // 更新每个科目的教师数量
                    subjectCountMap.put(subjectId, subjectCountMap.getOrDefault(subjectId, 0) + 1);
                }
            }
        }
        Map<String, Integer> updatedSubjectCountMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : subjectCountMap.entrySet()) {
            String subjectId = entry.getKey();
            Integer count = entry.getValue();
            String subjectName = subjectNameMap.get(subjectId);
            if (subjectName != null) {
                updatedSubjectCountMap.put(subjectName, count);
            }
        }
        return updatedSubjectCountMap;
    }

    private static @NotNull Map<String, Integer> getPeriodCountMap(List<GroupList> groupLists,List<School.Period> periods) {

        Map<String, String> periodNameMap = new HashMap<>();
        for (School.Period period : periods) {
            periodNameMap.put(period.getId(), period.getName());
        }
        Map<String, Integer> periodCountMap = getStringIntegerMap(groupLists);
        // 更新 periodCountMap 的 key 为 period 名称
        Map<String, Integer> updatedPeriodCountMap = new HashMap<>();
        for (Map.Entry<String, Integer> entry : periodCountMap.entrySet()) {
            String periodId = entry.getKey();
            Integer count = entry.getValue();
            String periodName = periodNameMap.get(periodId);
            if (periodName != null) {
                updatedPeriodCountMap.put(periodName, count);
            }
        }
        return updatedPeriodCountMap;
    }

    private static @NotNull Map<String, Integer> getStringIntegerMap(List<GroupList> groupLists) {
        Map<String, Integer> periodCountMap = new HashMap<>();

        // 遍历所有 GroupList 对象
        for (GroupList group : groupLists) {
            // 获取 periodId
            String periodId = group.getPeriodId();
            if (periodId != null) {
                    // 计算成员数量
                    int memberCount = group.getMembers().size();
                    // 更新 periodCountMap
                    periodCountMap.put(periodId, periodCountMap.getOrDefault(periodId, 0) + memberCount);

            }
        }
        return periodCountMap;
    }
}
