package org.apereo.cas.ticket.refreshtoken;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apereo.cas.ticket.TicketState;
import org.apereo.cas.ticket.support.AbstractCasExpirationPolicy;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

/**
 * This is OAuth refresh token expiration policy (max time to live = 1 month by default).
 *
 * @author Jerome Leleu
 * @since 5.0.0
 */
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include= JsonTypeInfo.As.PROPERTY)
public class OAuthRefreshTokenExpirationPolicy extends AbstractCasExpirationPolicy {

    /** Serialization support. */
    private static final long serialVersionUID = -7144233906843566234L;

    /** The time to kill in milliseconds. */
    private long timeToKillInMilliSeconds;

    /** No-arg constructor for serialization support. */
    public OAuthRefreshTokenExpirationPolicy() {}

    /**
     * Instantiates a new OAuth refresh token expiration policy.
     *
     * @param timeToKillInMilliSeconds the time to kill in milli seconds
     */
    @JsonCreator
    public OAuthRefreshTokenExpirationPolicy(@JsonProperty("timeToLive") final long timeToKillInMilliSeconds) {
        this.timeToKillInMilliSeconds = timeToKillInMilliSeconds;
    }

    @Override
    public boolean isExpired(final TicketState ticketState) {
        return ticketState == null || ticketState.getCreationTime()
                .plus(this.timeToKillInMilliSeconds, ChronoUnit.MILLIS).isBefore(ZonedDateTime.now(ZoneOffset.UTC));
    }

    @Override
    public Long getTimeToLive() {
        return this.timeToKillInMilliSeconds;
    }

    @JsonIgnore
    @Override
    public Long getTimeToIdle() {
        return 0L;
    }


    @Override
    public boolean equals(final Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        final OAuthRefreshTokenExpirationPolicy rhs = (OAuthRefreshTokenExpirationPolicy) obj;
        return new EqualsBuilder()
                .append(this.timeToKillInMilliSeconds, rhs.timeToKillInMilliSeconds)
                .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder()
                .append(timeToKillInMilliSeconds)
                .toHashCode();
    }
}

@@ -730,17 +730,19 @@
 Info(use
-=
+ = 
 JsonType
@@ -763,16 +763,17 @@
  include
+ 
 = JsonTy
@@ -885,16 +885,23 @@
 %0A    /**
+%0A     *
  Seriali
@@ -907,32 +907,37 @@
 ization support.
+%0A    
  */%0A    private 
@@ -1004,16 +1004,23 @@
 %0A    /**
+%0A     *
  The tim
@@ -1045,16 +1045,21 @@
 seconds.
+%0A    
  */%0A    
@@ -1075,37 +1075,32 @@
 ong timeToKillIn
-Milli
 Seconds;%0A%0A    /*
@@ -1100,16 +1100,23 @@
 %0A    /**
+%0A     *
  No-arg 
@@ -1153,16 +1153,21 @@
 support.
+%0A    
  */%0A    
@@ -1210,16 +1210,21 @@
 licy() %7B
+%0A    
 %7D%0A%0A    /
@@ -1316,37 +1316,32 @@
 ram timeToKillIn
-Milli
 Seconds the time
@@ -1356,14 +1356,8 @@
  in 
-milli 
 seco
@@ -1473,37 +1473,32 @@
 ong timeToKillIn
-Milli
 Seconds) %7B%0A     
@@ -1513,29 +1513,24 @@
 timeToKillIn
-Milli
 Seconds = ti
@@ -1531,37 +1531,32 @@
 s = timeToKillIn
-Milli
 Seconds;%0A    %7D%0A%0A
@@ -1730,37 +1730,32 @@
 his.timeToKillIn
-Milli
 Seconds, ChronoU
@@ -1762,13 +1762,14 @@
 nit.
-MILLI
+SECOND
 S).i
@@ -1895,29 +1895,24 @@
 timeToKillIn
-Milli
 Seconds;%0A   
@@ -2438,29 +2438,24 @@
 timeToKillIn
-Milli
 Seconds, rhs
@@ -2459,37 +2459,32 @@
 rhs.timeToKillIn
-Milli
 Seconds)%0A       
@@ -2631,13 +2631,8 @@
 llIn
-Milli
 Seco

Convert oauth expiration policy to correctly use seconds instead