package com.capgemini.framework.common;

/**
 * Class for time management (conversions...).
 **/
public final class TimeHelper {

    /**
     * Milliseconds count by second.
     **/
    private static final long MS_TO_SEC = 1000L;
    /**
     * Seconds count per hour.
     **/
    private static final long SEC_PER_HOUR = 3600L;
    /**
     * Seconds count per minute.
     **/
    private static final long SEC_PER_MIN = 60L;
    private static final int TWO_DIGITS = 2;
    private static final int THREE_DIGITS = 3;

    private TimeHelper() {
        // Nothing to do
    }

    /**
     * Set default unit for timeStamp.
     *
     * @param aDuration long value (seconds)
     * @return formated duration (by hours, minutes...)
     */
    public static String formatDuration(final Long aDuration) {
        return formatDuration(aDuration, false);
    }

    /**
     * Return the current date with a timeStamp.
     *
     * @param aDuration long value (seconds)
     * @param isMsUnit  Flag setting if milliseconds should be displayed
     * @return formated duration (by hours, minutes...)
     */
    private static String formatDuration(final Long aDuration, final boolean isMsUnit) {

        double nbMilliseconds = 0;
        StringBuilder theStringValue = new StringBuilder();

        /* Manage null values */
        if (aDuration == null) {
            return StringHelper.EMPTY;
        }

        long remainingSeconds = aDuration;

        /* Only required to parse milliseconds */
        if (isMsUnit) {
            nbMilliseconds = remainingSeconds % MS_TO_SEC;
            remainingSeconds = remainingSeconds / MS_TO_SEC;
        }

        /* Compute duration components */
        long nbHours = remainingSeconds / SEC_PER_HOUR;
        remainingSeconds = remainingSeconds % SEC_PER_HOUR;
        long nbMinutes = remainingSeconds / SEC_PER_MIN;
        remainingSeconds = remainingSeconds % SEC_PER_MIN;

        /* Hours */
        if (nbHours > 0) {
            theStringValue.append(nbHours).append('h');
        }

        /* Minutes */
        if ((nbMinutes > 0) || (nbHours > 0)) {
            theStringValue.append(StringHelper.fixedDigits(nbMinutes, TWO_DIGITS)).append('m');
        }

        /* Seconds */
        if (!((remainingSeconds == 0) && isMsUnit)) {

            if (nbMinutes > 0) {
                theStringValue.append(StringHelper.fixedDigits(remainingSeconds, TWO_DIGITS));
            } else {
                theStringValue.append(remainingSeconds);
            }
            theStringValue.append('s');
        }

        /* Milliseconds */
        if (isMsUnit) {

            String theMsValue;
            if (aDuration > MS_TO_SEC) {
                theMsValue = StringHelper.fixedDigits(nbMilliseconds, THREE_DIGITS);
            } else {
                theMsValue = String.valueOf((int) nbMilliseconds);
            }
            theStringValue.append(theMsValue).append("ms");
        }

        return theStringValue.toString();
    }
}
