﻿/// <reference path="./API/Agility.UGC.API.js" />

Agility.RegisterNamespace("Doritos.ContextUtil");

(function(ContextUtil) {

    ContextUtil.CurrentUserID = -1;
    ContextUtil.CurrentDisplayName = "";

    ContextUtil.AuthCookieName = null;

    ContextUtil.InitializeAuthenticationCookie = function(cookieName) {
        ContextUtil.AuthCookieName = cookieName;
    };

    ContextUtil.IsAuthenticated = function() {

        if (ContextUtil.GetCurrentUserID() > 0) {
            return true;
        }
        else {
            return false;
        }
    };

    ContextUtil.EnsureAuthentication = function() {
        var userid = Doritos.AgilityData.GetCurrentUserID();
        if (userid > 0) {
            return true;
        }

        return false;
    }

    ContextUtil.UserCanSubmitVideo = function() {

        if (ContextUtil.IsAuthenticated()) {

            var result = Doritos.AgilityData.UserCanSubmitVideo(ContextUtil.GetCurrentUserID());

            if (result == "True") {
                return true;
            }

        }
        return false;
    };



    ContextUtil.GetCurrentUserID = function() {

        if (ContextUtil.CurrentUserID > 0) {
            return ContextUtil.CurrentUserID;
        } else {

            var userid = Doritos.AgilityData.GetCurrentUserID();

            if (userid > 0) {
                ContextUtil.CurrentUserID = userid;
                return ContextUtil.CurrentUserID;
            }
            return -1;
        }
    };

    ContextUtil.GetCurrentDisplayName = function() {

        if (ContextUtil.CurrentDisplayName.length > 0) {
            return ContextUtil.CurrentDisplayName;
        } else {

            var uname = Doritos.AgilityData.GetCurrentDisplayName();

            if (uname != null && uname.length > 0) {
                ContextUtil.CurrentDisplayName = uname;
                return ContextUtil.CurrentDisplayName;
            }
            return "N/A";
        }
    };

    ContextUtil.ClearJustOneTimeUser = function() {
        $.cookie("JustOneTimeLastName", null);
        $.cookie("JustOneTimeFirstName", null);
        $.cookie("JustOneTimeEmail", null);
    };

    ContextUtil.RemoveSpecialCharacters = function(str) {

        return str.replace(/[^a-zA-Z 0-9]+/g, '');

        
    }


    ContextUtil.SetJustOneTimeUser = function(FirstName, LastName, Email) {



        $.cookie("JustOneTimeLastName", FirstName);
        $.cookie("JustOneTimeFirstName", LastName);
        $.cookie("JustOneTimeEmail", Email);
    };

    ContextUtil.JustOneTimeAuthenticated = function() {
        try {
            if ($.cookie("JustOneTimeEmail").length > 0 && $.cookie("JustOneTimeLastName").length > 0 && $.cookie("JustOneTimeFirstName").length > 0) {
                return true;
            }
            else {
                return false;
            }
        }
        catch (Err) {

        }

        return false;
    };

    ContextUtil.GetJustOneTimeUserID = function() {
        try {
            if (ContextUtil.JustOneTimeAuthenticated()) {
                var key = $.cookie("JustOneTimeLastName").toLowerCase() + "-" + $.cookie("JustOneTimeFirstName").toLowerCase() + "-" + $.cookie("JustOneTimeEmail").toLowerCase();
                return key;
            }
        }
        catch (Err) {

        }

        return "";
    };

    ContextUtil.GetJustOneTimeUserDisplayName = function() {
        try {
            if (ContextUtil.JustOneTimeAuthenticated()) {
                var username = $.cookie("JustOneTimeFirstName").toLowerCase() + " " + $.cookie("JustOneTimeLastName").toString().substring(0, 1);
                return username;
            }
        } catch (err) {
        }

        return "";
    };

})(Doritos.ContextUtil);