package com.hotwj.platform.securityManagement.training.support;

import com.hotwj.platform.config.companyBasicConfig.domain.bo.HotCompanyBasicConfigBo;
import com.hotwj.platform.config.companyBasicConfig.domain.vo.HotCompanyBasicConfigVo;
import com.hotwj.platform.config.companyBasicConfig.service.IHotCompanyBasicConfigService;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;

@Component
@RequiredArgsConstructor
public class StudyForbidPeriodSupport {

    public static final String FORBID_STUDY_MESSAGE = "当前处于禁学时间";

    private static final Long STATUS_ENABLED = 1L;
    private static final DateTimeFormatter TIME_FORMATTER_MINUTE = DateTimeFormatter.ofPattern("H:mm");
    private static final DateTimeFormatter TIME_FORMATTER_SECOND = DateTimeFormatter.ofPattern("H:mm:ss");

    private final IHotCompanyBasicConfigService companyBasicConfigService;

    public void checkStudyAllowed(Long companyId) {
        String forbidMessage = getForbidStudyMessage(companyId);
        if (StringUtils.isNotBlank(forbidMessage)) {
            throw new ServiceException(forbidMessage);
        }
    }

    public String getForbidStudyMessage(Long companyId) {
        if (companyId == null) {
            return null;
        }
        HotCompanyBasicConfigVo config = getCompanyBasicConfig(companyId);
        if (config == null || !STATUS_ENABLED.equals(config.getForbidPeriodEnabled())) {
            return null;
        }
        if (!isNowInForbidPeriod(config.getForbidStartTime(), config.getForbidEndTime())) {
            return null;
        }
        return FORBID_STUDY_MESSAGE;
    }

    private HotCompanyBasicConfigVo getCompanyBasicConfig(Long companyId) {
        HotCompanyBasicConfigBo bo = new HotCompanyBasicConfigBo();
        bo.setCompanyId(companyId);
        bo.setIsDeleted(0L);
        List<HotCompanyBasicConfigVo> configs = companyBasicConfigService.queryList(bo);
        if (configs == null || configs.isEmpty()) {
            return null;
        }
        return configs.get(0);
    }

    private boolean isNowInForbidPeriod(String forbidStartTime, String forbidEndTime) {
        LocalTime startTime = parseTime(forbidStartTime);
        LocalTime endTime = parseTime(forbidEndTime);
        if (startTime == null || endTime == null) {
            return false;
        }
        LocalTime now = LocalTime.now();
        if (startTime.equals(endTime)) {
            return true;
        }
        if (startTime.isBefore(endTime)) {
            return !now.isBefore(startTime) && !now.isAfter(endTime);
        }
        return !now.isBefore(startTime) || !now.isAfter(endTime);
    }

    private LocalTime parseTime(String value) {
        if (StringUtils.isBlank(value)) {
            return null;
        }
        try {
            return LocalTime.parse(value, TIME_FORMATTER_SECOND);
        } catch (DateTimeParseException ignored) {
        }
        try {
            return LocalTime.parse(value, TIME_FORMATTER_MINUTE);
        } catch (DateTimeParseException ignored) {
        }
        return null;
    }
}
