package com.secureline.server;

public final class SecureLinePolicy {

    private SecureLinePolicy() {}

    // ===============================
    // Connectivity Sabotage Protection
    // ===============================

    public static final long CONNECTIVITY_15M =
            15L * 60L * 1000L;

    public static final long CONNECTIVITY_30M =
            30L * 60L * 1000L;

    // ===============================
    // Inactivity Protection
    // ===============================

    public static final long INACTIVITY_24H =
            24L * 60L * 60L * 1000L;

    public static final long INACTIVITY_48H =
            48L * 60L * 60L * 1000L;

    public static final long INACTIVITY_96H =
            96L * 60L * 60L * 1000L;

    public static final long INACTIVITY_168H =
            168L * 60L * 60L * 1000L;

    public static final long INACTIVITY_336H =
            336L * 60L * 60L * 1000L;

    public static final long DEFAULT_INACTIVITY_TIMEOUT =
            INACTIVITY_96H;

    // ===============================
    // BFU Timer Protection
    // ===============================

    public static final long BFU_TIMER_1H =
            1L * 60L * 60L * 1000L;

    public static final long BFU_TIMER_2H =
            2L * 60L * 60L * 1000L;

    public static final long BFU_TIMER_4H =
            4L * 60L * 60L * 1000L;

    public static final long BFU_TIMER_6H =
            6L * 60L * 60L * 1000L;

    // ===============================
    // Tamper Lock
    // ===============================

    public static final int TAMPER_SENSITIVITY_NORMAL = 1;
    public static final int TAMPER_SENSITIVITY_HIGH = 2;

    public static final float TAMPER_THRESHOLD_NORMAL = 24f;
    public static final float TAMPER_THRESHOLD_HIGH = 20f;

    // ===============================
    // License Policy
    // ===============================

    private static final int MID_LOCK_GRACE_DAYS = 5;

    public static int calculateState(SecureLineLicenseStore licenseStore) {

	if (!licenseStore.licenseExists())
	    return SecureLineState.WAITING_FIRST_UNLOCK;

        long today = System.currentTimeMillis() / 86400000L;
        long expires = licenseStore.getExpiresDay();

        if (today <= expires)
            return SecureLineState.ACTIVE;

        if (today <= expires + MID_LOCK_GRACE_DAYS)
            return SecureLineState.MID_LOCK;

        return SecureLineState.HARD_LOCK;
    }
}
