package com.koobietech.eas.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.UUID;
public class StudentArchiveGenerator {

    private static final String ARCHIVE_CODE_PREFIX = "SA";
    private static final int ARCHIVE_CODE_LENGTH = 25;

    private static final String STUDENT_CODE_PEREFIX = "ST";
    private static final int STUDENT_CODE_LENGTH = 25;
    private static final int STUDENT_NUM_LENGTH = 8;

    public  static String generateArchiveCode(String generateStudentCode, String fileTypeCode) {
        String studentNum = generateStudentCode.substring(2, STUDENT_NUM_LENGTH + 3).replace("X", "");
        String nowDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHhmmss", Locale.CHINA));
        String archiveCode = ARCHIVE_CODE_PREFIX + studentNum + nowDate + fileTypeCode;
        if (archiveCode.length() < ARCHIVE_CODE_LENGTH) {
            archiveCode += UUID.randomUUID().toString()
                    .substring(0, ARCHIVE_CODE_LENGTH - archiveCode.length());
        }
        return archiveCode;
    }

    public static String generateStudentCode(String studentNumber, String studentId, String schoolName, String enrollmentDate) {
        LocalDate localDateEnrollmentDate = LocalDate.of(Integer.parseInt(enrollmentDate), 1, 1);
        String studentAge = String.valueOf(getStudentAge(studentId));
        String studentNum = getStudentNum(studentNumber);
        String studentGender = getStudentGender(studentId).substring(0, 1); // 取性别代码的首字母
        String graduationDate = String.valueOf(getStudentGraduationDate(localDateEnrollmentDate).getYear());

        String studentCode = STUDENT_CODE_PEREFIX + studentNum + studentAge +
                studentGender + graduationDate + schoolName;

        // 如果生成的档案编码长度不足，用UUID填充
        if (studentCode.length() < STUDENT_CODE_LENGTH) {
            studentCode += UUID.randomUUID().toString()
                    .substring(0, STUDENT_CODE_LENGTH - studentCode.length());
        }
        return studentCode;
    }

    private static String getStudentNum(String studentNumber) {
        if (studentNumber.length() < STUDENT_NUM_LENGTH) {
            studentNumber = studentNumber + "X" + UUID.randomUUID().toString()
                    .toUpperCase(Locale.ROOT).substring(0, STUDENT_NUM_LENGTH - studentNumber.length());
        }
        return studentNumber;
    }

    private static int getStudentAge(String studentId) {
        LocalDate currentDate = LocalDate.now();
        LocalDate birthDateirthDate = extractBirthDate(studentId);
        Period period = Period.between(birthDateirthDate, currentDate);
        return period.getYears();
    }

    private static String getStudentGender(String studentId) {
        // 根据身份证号获取性别，这里只取身份证号的倒数第二位判断性别，假设性别代码为奇数表示男性，偶数表示女性
        int genderCode = Integer.parseInt(studentId.substring(16, 17));
        return genderCode % 2 == 0 ? "F" : "M";
    }

    private static LocalDate getStudentGraduationDate(LocalDate enrollmentDate) {
        // 假设毕业时间为入学时间的四年后
        LocalDate graduationDate = enrollmentDate.plusYears(4);
        return graduationDate;
    }
    // 提取身份证号码中的出生日期部分
    private static LocalDate extractBirthDate(String idCard) {
        String birthdate = idCard.substring(6, 14);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return LocalDate.parse(birthdate, formatter);
    }
}
