// loaded after Okta widget is initialized
var coxLogin = {
    init: function () {
        // move the helps links into the sign in box
        //$('.auth-footer').replaceWith($('.login-help-links').clone());
        var links = $('.login-help-links').clone();
        $(links).removeClass("d-none");
        $('.auth-footer').replaceWith($(links));

        // check the remember me on page load
        $('input[name="remember"]').prop('checked', true);

        // remove white spaces from form fields
        $('input').blur(function () {
            $(this).val(
                $.trim($(this).val())
            );
        });

        // get the onsuccess query param
        var _onSuccessUrl = this.queryParamsFromUrl('onsuccess');

        // set the finalview query param on links that end in finalview= with the onsuccess query param
        $('.login-help-links a').each(function () {
            var $this = $(this);
            var _href = $this.attr("href");
            if (_href.indexOf("finalview=") >= 0) {
                $this.attr("href", _href + _onSuccessUrl);
            }
        });

        // only load Tealium if a utag_data object is created on the page (so webmail does not load this)
        if (typeof utag_data !== 'undefined') {
            this.loadTeallium();
        }

        // mfa intro messaging toggle
        if (_onSuccessUrl.indexOf('mfa.html') !== -1) {
            console.log('is MFA');
            $('#introBlock').css('display', 'none');
            $('#mfaBlock').css('display', 'block');
        } else {
            console.log('is MFA false');
            $('#introBlock').css('display', 'block');
            $('#mfaBlock').css('display', 'none');
        }
    },

    queryParamsFromUrl: function (queryParamKey) {
        var uri = window.location.href.split("?")[1];
        var queryParams = {};
        if (uri) {
            uri.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"), function ($0, $1, $2, $3) {
                queryParams[$1] = $3;
            });
        }
        var queryParamVal = queryParams[queryParamKey];
        if (queryParamVal) {
            return queryParamVal;
        } else {
            return "";
        }
    },

    // Check the given cookie for a value and return it.
    getCoxCookies: function (cookieName) {
        var cookies = document.cookie.split('; ');
        for (var i = 0, numCookies = cookies.length; i < numCookies; i++) {
            var parts = cookies[i].split('=');
            if (parts[0] === cookieName) {
                //return unescape(parts[1].replace(/\+/g, ' '));
                return unescape(parts[1].replace(/[^A-Za-z0-9\+\/\=]/g, ''));
            }
        }
        ;
        return "";
    },

    // create/destroy cox remember me cookie
    updateRememberMeCookie: function () {
        var userIDval = $("input[name='username']").val();
        var userPwdval = $("input[name='password']").val();
        if (userIDval != undefined && userPwdval != undefined) {
            // remove any @cox.net if it exists to prevent from being saved into cookie
            userIDval = userIDval.replace('@cox.net', '');
            // base64 encode the user id field
            userIDval = window.btoa(userIDval)

            var date = new Date();
            date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
            var expiryCreate = "; expires=" + date.toGMTString();
            var expiryDestroy = "; expires=" + new Date(0).toGMTString();

            var theRememberCheckBox = $("input[name='remember']");
            var currentDomain = window.location.host;
            currentDomain = currentDomain.substring(currentDomain.indexOf('.'));

            if ($(theRememberCheckBox).is(":checked") == true) {
                if (navigator.cookieEnabled) {
                    document.cookie = "cox-rememberme-user=" + userIDval + expiryCreate + "; path=/; domain=" + currentDomain;
                }
            } else {
                document.cookie = "cox-rememberme-user=" + userIDval + expiryDestroy + "; path=/; domain=" + currentDomain;
            };
        }
    },

    // remove remember me cookie
    removeCookie: function (cookieName) {
        var userIDval = $("input[name='username']").val();
        var expiryDestroy = "; expires=" + new Date(0).toGMTString();
        var currentDomain = window.location.host;
        currentDomain = currentDomain.substring(currentDomain.indexOf('.'));

        document.cookie = cookieName + "=" + userIDval + expiryDestroy + "; path=/; domain=" + currentDomain;
    },

    loadTeallium: function () {
        utag_data.dateStamp = new Date().getTime();
        utag_data.visitCount = "";

        if (this.getCoxCookies("cox-current-zipcode")) {
            utag_data.zip = this.getCoxCookies("cox-current-zipcode");
        }

        if (this.getCoxCookies("tealiumMyAccount")) {
            var tealiumMyAccount = this.getCoxCookies("tealiumMyAccount");
            // remove leading quote
            tealiumMyAccount = tealiumMyAccount.replace('"', '');
            // get count from myAccountVisitCount=X and base64 decode it
            tealiumMyAccount = window.atob(tealiumMyAccount).split("=")[1];
            utag_data.visitCount = tealiumMyAccount;
        }

        // set the responsiveDisplayType
        if (window.matchMedia) {
            var respDesktopCheck = window.matchMedia("(min-width: 941px)");
            var respTabletCheck = window.matchMedia("(max-width: 940px) and (min-width: 768px)");
            var respMobileCheck = window.matchMedia("(max-width: 767px)");

            if (respDesktopCheck.matches) {
                utag_data.responsiveDisplayType = 'desktop';
            }
            if (respTabletCheck.matches) {
                utag_data.responsiveDisplayType = 'tablet';
            }
            if (respMobileCheck.matches) {
                utag_data.responsiveDisplayType = 'mobile';
            }
        }

        // load utag.js
        (function (a, b, c, d) {
            a = '//tags.tiqcdn.com/utag/cox/main/prod/utag.js';
            b = document;
            c = 'script';
            d = b.createElement(c);
            d.src = a;
            d.type = 'text/java' + c;
            d.async = true;
            a = b.getElementsByTagName(c)[0];
            a.parentNode.insertBefore(d, a);
        })();
    }

};

// OKTA sign in scripts
$(document).ready(function () {
    var SCOPE = "openid%20internal";
    var HOST_NAME = location.hostname;
    var REDIRECT_URI = "https://" + HOST_NAME + "/authres/code";
    var AJAX_URL = "https://" + HOST_NAME + "/authres/getNonce?onsuccess=";

    //LOCAL Config Values. Uncomment when testing locally
    /* var BASE_URL = 'https://dev-497699.okta.com';
    var CLIENT_ID = '0oaebqg8oQDFWqgUd4x6';
    var ISSUER = 'https://dev-497699.okta.com/oauth2/default';
    var ON_SUCCESS_URL = "http://localhost:8080/okta/success.html";*/

    //DEV Config Values. Uncomment when deploying to DEV
    /*var BASE_URL = 'https://login.dev.cox.com';
    var CLIENT_ID = '0oaqh1lxntLuy2ND30h7';
    var ISSUER = 'https://login.dev.cox.com/oauth2/aust1aeb5qfDIYyNQ0h7';
    var ON_SUCCESS_URL = "https%3A%2F%2Fwww.int.dev.cox.com/residential/home.html";*/


    //QA Config Values. Uncomment when deploying to QA
    /*var BASE_URL = 'https://login.qa.cox.com/';
    var CLIENT_ID = '0oaqxh9h4xyOJq7M20h7';
    var ISSUER = 'https://login.qa.cox.com/oauth2/ausszloopfW0iGRQY0h7';
    var ON_SUCCESS_URL = "https%3A%2F%2Fwww.one.qa.cox.com%2Fresaccount%2Fhome.html";*/

    //STAGING Config Values. Uncomment when deploying to STAGING
    /*var BASE_URL = 'https://cci-res-qa.oktapreview.com/';
    var CLIENT_ID = '0oaqxh9h4xyOJq7M20h7';
    var ISSUER = 'https://cci-res-qa.oktapreview.com/oauth2/ausqwqgk4pWjPDvAr0h7';
    var ON_SUCCESS_URL = "https%3A%2F%2Fwww.one.staging.cox.com%2Fresaccount%2Fhome.html";*/

    //DARK SILO Config Values. Uncomment when deploying to Dark
    /* var BASE_URL = 'https://login.cox.com/';
    var CLIENT_ID = '0oa1iranfsovqR6MG0h8';
    var ISSUER = 'https://login.cox.com/oauth2/aus1jbzlxq0hRR6jG0h8';
    var ON_SUCCESS_URL = "https%3A%2F%2Ftest.cox.com%2Fresaccount%2Fhome.html"; */

    //PROD Config Values. Uncomment when deploying to Prod
    var BASE_URL = 'https://login.cox.com/';
    var CLIENT_ID = '0oa1iranfsovqR6MG0h8';
    var ISSUER = 'https://login.cox.com/oauth2/aus1jbzlxq0hRR6jG0h8';
    var ON_SUCCESS_URL = "https%3A%2F%2Fwww.cox.com%2Fresaccount%2Fhome.html";

    // grab the onsuccess query param and store it to set the signInWidgetConfig.authParams.state variable
    var onSuccessUrl = coxLogin.queryParamsFromUrl('onsuccess');
    if (onSuccessUrl === "") {
        onSuccessUrl = ON_SUCCESS_URL;
    }

    if (HOST_NAME == "espanol.cox.com") {
        onSuccessUrl.replace("www.cox.com", "espanol.cox.com");
    }

    console.log('onSuccessUrl', onSuccessUrl);

    // get the remember me cookie value, base-64 decode it store it to set the signInWidgetConfig.username variable for prefill
    var coxUserID;
    var coxRememberCookie = coxLogin.getCoxCookies("cox-rememberme-user");
    if (coxRememberCookie != "") {
        coxUserID = window.atob(coxRememberCookie);
    }

    var ajaxCallCount = 0;

    // delete the cookie based on domain

    function deleteDefaultCookie(baseName) {

        var fullName = baseName;
        var domain = window.location.hostname;
        console.log("domain: " + domain);

        // Don't delte if localhost
        if (domain.indexOf("localhost") >= 0) {
            return;
        }

        if (domain.indexOf("espanol") >= 0) {
            return;
        }

        // only non-prod domains should be using 'nonprod' suffix
        if (domain != "www.cox.com") {
            fullName = baseName + "nonprod";
        }

        // now delete the cookie
        var cookieValue = "";
        var cookieDate = new Date();
        cookieDate.setMonth(cookieDate.getMonth() - 12);
        console.log("deleteCookie: " + fullName + "=" + cookieValue + ";expires=" + cookieDate + ";path=/");
        document.cookie = fullName + "=" + cookieValue + ";expires=" + cookieDate + ";path=/";
    }

    //Observe for errors on Sign in widget form
    function registerMutation() {
        var targetNode = $("#widget-container .o-form-error-container")[0];

        if ($(targetNode).length > 0) {
            console.log("target node exists");

            //Register mutation to observer errors on OKTA form
            var observer = new MutationObserver(function (mutations) {
                mutations.forEach(function (mutation) {
                    var error = $(".okta-form-infobox-error", mutation.target);
                    if ($(error).length > 0) {
                        coxLogin.removeCookie("cox-rememberme-user");
                    }
                });
            });

            // Configuration of the observer:
            var config = {
                attributes: true,
                childList: true,
                characterData: true
            };

            // Pass in the target node, as well as the observer options
            observer.observe(targetNode, config);
        }
    }

    function makeAjaxCall() {
        // delete the old default cookies so they don't interfere...
        deleteDefaultCookie("_cbidt");
        deleteDefaultCookie("_cidt");

        // load the nonce before we display the login widget
        ajaxCallCount += 1;
        $.ajax({
            url: AJAX_URL + onSuccessUrl,
            xhrFields: {withCredentials: true},
            crossDomain: true
        })
            .done(function (nonceVal) {
                console.log("getNonce success", nonceVal);

                $("body").on("submit", "#widget-container form", function (event) {
                    coxLogin.updateRememberMeCookie();
                });

                signInWidgetConfig = {
                    logo: '',
                    language: 'en',
                    i18n: {
                        // language overrides (full list: https://github.com/okta/okta-signin-widget/blob/master/packages/%40okta/i18n/src/properties/login.properties)
                        'en': {
                            // Page Specific Title
                            'primaryauth.title': 'Residential sign in',

                            // Login Overrides
                            'primaryauth.username.placeholder': 'User ID',
                            'primaryauth.username.tooltip': 'User ID',
                            'error.username.required': 'Please enter a user ID',
                            'remember': 'Remember user ID',
                            'rememberDevice': 'Remember this device',
                            'rememberDevice.timebased': 'Remember this device for the next {0}',
                            'rememberDevice.devicebased': 'Always remember this device',
                            'primaryauth.submit': 'Sign in',

                            // Error Overrides
                            'errors.E0000004': 'An invalid User ID or Password was entered. Please try again.',
                            'errors.E0000069': 'Your account is locked because of too many authentication attempts. Please try again in 30 minutes.',
                            'errors.expired.session': 'Your session has expired. Please sign in again.',

                            // Enroll Choices
                            'enroll.choices.title': 'Set up multifactor authentication',
                            'enroll.choices.description.generic': 'Choose at least one option on where you want to receive your code.',
                            'enroll.choices.optional': 'You can configure any additional optional factor or click finish.',
                            'enroll.choices.list.setup': 'Set up required',
                            'enroll.choices.list.enrolled': ' ',
                            'enroll.choices.list.optional': ' ',
                            'enroll.choices.setup': 'Set up',
                            'enroll.choices.setup.another': 'Set up another',
                            'enroll.choices.submit.finish': 'Finish',
                            'enroll.choices.submit.configure': 'Configure factor',
                            'enroll.choices.submit.next': 'Configure next factor',
                            'enroll.choices.cardinality.setup': '({0} set up)',
                            'enroll.choices.cardinality.setup.remaining': '({0} of {1} set up)',
                            'enroll.choices.setup.skip': 'Skip set up',

                            // Email enrollment and verification
                            'email.button.send': 'Email code',
                            'email.button.resend': 'Send again',
                            'email.code.label': 'Verification code',
                            'email.code.not.received': 'Haven\'t received an email?',
                            'email.enroll.title': 'Receive a code via email',
                            'email.enroll.description': 'Send a verification code to your registered email. Must be different from your @cox.net email address.',
                            'email.link.terminal.msg': 'To finish signing in, return to the screen where you requested the email link.',
                            'email.mfa.title': 'Verify with Email Authentication',
                            'email.mfa.description': 'Send a verification code to {0}.',
                            'email.mfa.email.sent.description': 'A verification code was sent to {0}. Check your email and enter the code below.',
                            'email.mfa.email.sent.description.sentText': 'A verification code was sent to',
                            'email.mfa.email.sent.description.emailCodeText': 'Check your email and enter the code below.',

                            // Enroll SMS
                            'enroll.sms.setup': 'Receive a code via text message',
                            'enroll.sms.try_again': 'The number you entered seems invalid. If the number is correct, please try again.',

                            // Enroll CALL
                            'enroll.call.setup': 'Follow instructions via phone call',

                            // Factors
                            'factor.sms': 'SMS',
                            'factor.sms.description': 'Enter a single-use code sent to your mobile phone.',
                            'factor.sms.time.warning': 'Haven\'t received a text? To try again, click <span style="font-weight:bold">Re-send code</span>.',
                            'factor.call': 'Voice call',
                            'factor.call.description': 'Use a phone to authenticate by following voice instructions.',
                            'factor.call.time.warning': 'Haven\'t received a voice call? To try again, click <span style="font-weight:bold">Redial</span>.',
                            'factor.email': 'Email',
                            'factor.email.description': 'Enter a verification code sent to your email.',

                            // Common properties
                            'signout': 'Sign Out',
                            'signin': 'Sign In',
                            'signup': 'Sign Up',
                            'mfa.challenge.verify': 'Verify',
                            'mfa.challenge.answer.placeholder': 'Answer',
                            'mfa.challenge.answer.tooltip': 'Answer',
                            'mfa.challenge.answer.showAnswer': 'Show',
                            'mfa.challenge.answer.hideAnswer': 'Hide',
                            'mfa.challenge.enterCode.placeholder': 'Enter code',
                            'mfa.challenge.enterCode.tooltip': 'Enter code',
                            'mfa.challenge.password.placeholder': 'Password',
                            'mfa.backToFactors': 'Back',
                            'mfa.phoneNumber.placeholder': 'Phone number',
                            'mfa.phoneNumber.ext.placeholder': 'Extension',
                            'mfa.sendCode': 'Send code',
                            'mfa.sent': 'Sent',
                            'mfa.resendCode': 'Re-send code',
                            'mfa.call': 'Call me',
                            'mfa.calling': 'Calling',
                            'mfa.redial': 'Redial',
                            'mfa.sendEmail': 'Send email',
                            'mfa.resendEmail': 'Re-send email',
                            'mfa.noAccessToEmail': 'Can\'t access email'
                        }
                    },
                    // Changes to widget functionality
                    features: {
                        rememberMe: true, // remember me
                        multiOptionalFactorEnroll: true, // MFA
                        showPasswordToggleOnSignInPage: true
                    },
                    baseUrl: BASE_URL,
                    clientId: CLIENT_ID,
                    redirectUri: REDIRECT_URI,
                    authParams: {
                        issuer: ISSUER,
                        responseType: 'code',
                        state: onSuccessUrl,
                        scopes: ['openid', 'internal'],
                        nonce: nonceVal
                    },
                    username: coxUserID,
                    transformUsername: function (username, operation) {
                        // remove any @cox.net before submitting
                        return username.replace('@cox.net', '');
                    }
                };
                signInWidget = new OktaSignIn(signInWidgetConfig);


                signInWidget.authClient.session.get().then(function (res) {
                    // Session exists, show logged in state.
                    if (res.status === 'ACTIVE') {
                        //window.location = "https://cci-res-dev.oktapreview.com/oauth2/auspxol4lcdTqSjtv0h7/v1/authorize?client_id=0oaqh1lxntLuy2ND30h7&response_type=code&scope="+SCOPE+"&redirect_uri=https%3A%2F%2Fwww.int.dev.cox.com/authres/login&state=https%3A%2F%2Fwww.int.dev.cox.com/residential/home.html&nonce="+nonceVal;
                        var NEW_REDIRECT_URI = ISSUER + "/v1/authorize?client_id=" + CLIENT_ID + "&response_type=code&scope=" + SCOPE + "&redirect_uri=" + REDIRECT_URI + "&state=" + encodeURIComponent(onSuccessUrl) + "&nonce=" + encodeURIComponent(nonceVal);
                        console.log(NEW_REDIRECT_URI);
                        window.location = NEW_REDIRECT_URI;
                        return;
                    }
                    // No session, show the login form
                    signInWidget.renderEl({
                        el: '#widget-container'
                    }, function success(res) {
                        console.log(res);
                    }, function error(err) {
                        // handle errors as needed
                        console.error(err);
                    });
                });

                // init coxLogin after the okta widget has loaded
                signInWidget.on('ready', function () {
                    coxLogin.init();
                    registerMutation();
                });

                // fires with each pane update                
                signInWidget.on('afterRender', function (context) {
                    // Change the forgot Password link
                    if (context.controller == 'primary-auth') {
                        var links = $('.login-help-links').clone();
                        $(links).removeClass("d-none");
                        $('.auth-footer').replaceWith($(links));
                    }
                    // hide the banner above the login and ads post login form
                    if (context.controller == 'enroll-choices' || context.controller == 'mfa-verify') {
                        $('.login-banner').remove();
                        $('.contentBlockRight').remove();
                        $('.contentBlockLeft').removeClass('outer-container');
                        $('.contentBlockLeft').toggleClass('col-lg-6 col-lg-8');
                    }
                    return;
                });
            })
            .fail(function (err) {
                if (ajaxCallCount === 1) {
                    setTimeout(makeAjaxCall, 3000);
                } else {
                    // display an error in the login box since we cannot load the login widget
                    var errorMsg = '<div class="alert alert-danger pl-1 pr-2 ml-alert-err" role="alert"><div class="alert-content error ml-1"></div>Hmmm, something\'s not working. Please try refreshing the page to load the login.</div>'
                    $('#widget-container').html(errorMsg);
                    console.log("getNonce error", err);
                }
            });
    }

    makeAjaxCall();

    coxLogin.removeCookie("cox-remember-me-check");
});