package com.cf.imes.module.system.enums.token;


import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static java.time.temporal.ChronoUnit.*;

/**
 * @author token有效时间的类型枚举
 */

@Getter
@AllArgsConstructor
public enum TokenExpiresTimeTypeEnum {

    PERMANENT("permanent",99,FOREVER,"永久"),


    ONEHOURS("1_hour",1, HOURS,"1小时"),


    EIGHTHOURS("8_hour",8, HOURS,"8小时"),


    TWELVEHOURS("12_hour",12, HOURS,"12小时"),


    ONEDAYS("1_day",1, DAYS,"1天"),


    SEVENDAYS("7_day",7, DAYS,"7天"),


    FIFTEENDAYS("15_day",15, DAYS,"15天"),


    ONEMONTHS("1_month",1, MONTHS,"1个月"),


    THREEMONTHS("3_month",3, MONTHS,"3个月"),


    SIXMONTHS("6_month",6, MONTHS,"6个月");


    /**
     * 字段类型名称
     */
    private final String type;


    /**
     * 时长
     */
    private final Integer value;


    /**
     * 类型
     */
    private final ChronoUnit timeType;


    /**
     * 总和
     */
    private final String name;




    public static LocalDateTime adjustTime(String type, LocalDateTime dateTime) {

        TokenExpiresTimeTypeEnum typeEnum = null;

        for (TokenExpiresTimeTypeEnum value : values()) {
            if (ObjectUtil.equal(value.getType(), type)) {
                typeEnum = value;
                break;
            }
        }


        if(typeEnum == null){
            return null;
        }

        ChronoUnit typeEnumType = typeEnum.getTimeType();

        return switch (typeEnumType) {
            case FOREVER -> LocalDateTime.of(2999, 1,1, 0, 0, 0);
            case YEARS -> dateTime.plusMinutes(typeEnum.getValue());
            case HOURS -> dateTime.plusHours(typeEnum.getValue());
            case DAYS -> dateTime.plusDays(typeEnum.getValue());
            case MONTHS -> dateTime.plusMonths(typeEnum.getValue());
            default -> null;
        };


    }








}
