/*
 * COPYRIGHT INFORMATION - DO NOT REMOVE
 *
 * Copyright (c) LinuxMagic Inc. 2019-2021 All Rights Reserved
 *
 * This file contains Original Code as created by LinuxMagic Inc.
 *
 * The Original Code is distributed on an 'AS IS' basis,
 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND LINUXMAGIC
 * HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 *
 * Do NOT download, distribute, use or alter this software or file in any
 * way without express written permission from LinuxMagic Inc. or its parent
 * company Wizard Tower TechnoServices signed by an authorized company officer.
 *
 * Author(s): Alex You <alexy@linuxmagic.com>
 *            Steven Hall <stevenh@linuxmagic.com>
 *            Shaun A. Johnson <shaun@linuxmagic.com>
 *
 * $Id: redirect_portal.js 44239 2022-10-04 21:41:30Z shaun $
 */

// I am generally not a fan of permitting JS to run inline like this, but
// since we need to know the state of 'mobility' prior to a document
// onload trigger, and because this state is not dependent on the document
// completing its load first, we allow this in order to establish a global
// variable that the onload function will subsequently use for processing
// its onload behavior.

// global flag to indicate if the screen size is small enough to warrant
// a mobility interface.
var isMobile = false;

// Any screen that has a width less than this will be deemed 'mobile' and will
// be redirected to the portal
// 1080px is the minimum width required to view the user interface on the screen
// without horizontal scroll bars
const MOBILE_WIDTH = 780;
const COOKIE_NAME = 'MMWMMOBILE';
const COOKIE_LTIME = 180;

if (screen.width <= MOBILE_WIDTH) {
    isMobile = true;
}

function loginMobilitySteps() {
    // at this stage we know the following:
    //  - mobility redirect feature is enabled
    //  - screen size is smaller than our defined
    //    threshold of width
    //
    // now, we want to know if the user has a cookie set indicating they
    // have been asked this before, and have answered one way or another
    // with 'remember this choice' selected.
    //
    let cval = mmGetCookie(COOKIE_NAME);
    if (cval == 'desktop') {
        // NOTE: there is a mismatch of jquery and
        //       vanilla javascript going on here
        //       - the following was copied from login.js
        //       to save time.
        let theForm = document.forms["login-form"];
        theForm["login-email"].focus();
        return;
    } else if (cval == 'mobile') {
        let path = destination();
        window.location.href = path;
        return;
    } else {
        // either not asked yet, or declined to remember answer
        // or cookie value is not one of the permitted values.
        // display the prompt dialog
        let dialog = $('#mobileOption');
        let modal = $('#mobileOption .mm-modal-content');
        if (dialog && modal) {
            // size the dialog down to match visible screen width
            let w = screen.width - 20;
            modal.css('min-width', w + 'px');
            modal.css('width', w + 'px');
            dialog.css('width', '100vw');
            dialog.show();
            // going to have to use a focus function to bring the modal into view...
            // NOTE: this doesn't seem to work.. not sure if this is firefox issue
            //       or something else....
            $('#mobileChoiceMobile').focus();
        }
        return;
    }
}

//! utility function to determine if 'remember' was checked, and save.
/*!
 * @param   string  the specific choice (cookie value) to set
 *
 * @return  void
 *
 * NOTE: the cookie will only be saved if the user had the 'remember'
 *       checkbox checked.
 */
function rememberChoice(choice) {
    // validate argument is one of our permitted options
    if(choice !== 'desktop' && choice !== 'mobile'){
        return;
    }
    // get a handle to our checkbox element
    let remember = $('[name="remember"]');
    if(remember.prop('checked')){
        // if the checkbox element was checked
        // set the cookie using common mmSetCookie function
        mmSetCookie(COOKIE_NAME, choice, COOKIE_LTIME);
    }
    return;
}

function choiceDesktop() {
    rememberChoice('desktop');
    
    let dialog = $('#mobileOption');
    if (dialog) {
        dialog.hide();
    }

    // NOTE: there is a mismatch of jquery and
    //       vanilla javascript going on here
    //       - the following was copied from login.js
    //       to save time.
    let theForm = document.forms["login-form"];
    theForm["login-email"].focus();
}

function choiceMobile(urlRef) {
    rememberChoice('mobile');
    window.location.href = destination();
}

function destination() {
    let urlRef = $(location).attr('pathname');
    let path = '';
    switch (urlRef) {
        case '/login.php':
            path = '/portal'
            break;
        case '/security.php':
            path = '/portal/communication/email/preferences/cid'
            break;
        default:
            path = '/portal'
    }
    return path;
}
