import applicationConfig from '@/constants/application-config';
import { getISSCookie } from '@/helpers/cookie-helper.js';

/*
    Method to determine if our analytics session has timed out or not.
    Amount used for timeout is configurable in application-config.js
*/
export function isAnalyticsSessionStillActive() {
    if (getISSCookie() !== null) {
        const lastTouchedValue = getISSCookie().LastTouched;
        const timeoutAmount = applicationConfig.ANALYTICS_SESSION_TIMEOUT_MINUTES;

        const isMoreThanHalfHourAgo = (new Date() - new Date(lastTouchedValue)) / 60000 > timeoutAmount;

        if (isMoreThanHalfHourAgo) {
            return false;
        }

        return true;
    }
    return false;
}

/*
    Method to determine if the users 'saved' session is still active.
    When user state is created, there is a date that is saved into state
    this method checks against that date.

    Note: That time for saved session timeout is configurable in application-config.js
*/
export function isSavedSessionStillActive() {
    if (getISSCookie() !== null) {
        const savedSessionTimeStamp = new Date(getISSCookie().SavedSessionTimeoutDate);
        const isSavedSessionTimedOut = new Date(new Date().toUTCString()) > savedSessionTimeStamp;

        return !isSavedSessionTimedOut;
    }
    return false;
}

/*
Function to calculate the date for the saved session timeout.
*/

export function getDateForSavedSessionTimeout() {
    const currentDate = new Date(new Date().toUTCString());
    currentDate.setDate(currentDate.getDate() + applicationConfig.SAVED_SESSION_TIMEOUT_DAYS);
    return currentDate.toUTCString();
}


/*
Function to return the iOS version of the device.
*/

export function getiOSversion() {
    if (/iP(hone|od|ad)/.test(navigator.platform)) {
        // supports iOS 2.0 and later:
        var v = navigator.appVersion.match(/OS (\d+)_(\d+)_?(\d+)?/);
        return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];
    }
}

/*
Function to return true/false if the user agent is a mobile device or not.
*/

export function isMobileDevice() {
    const userAgent = navigator.userAgent;
    return (
        userAgent.includes("Android") ||
        userAgent.includes("Mobile") ||
        userAgent.includes("iPod") ||
        userAgent.includes("iPhone") ||
        userAgent.includes("IEMobile") ||
        userAgent.includes("BlackBerry") ||
        userAgent.includes("webOS")
    );
}

/*
Function to return true/false if the user is using an apple browser on a device.
*/

export function isAppleBrowser() {
    const userAgent = navigator.userAgent;
    return (
        userAgent.includes("iPod") ||
        userAgent.includes("iPad") ||
        userAgent.includes("iPhone") ||
        userAgent.includes("Mac")
    );
}

