﻿"use strict";

var app =
    angular.module("judgeapp", ["ngRoute",
        "ngTouch",
        "ngAnimate",
        "ngResource",
        "ngSanitize",
        "LocalStorageModule",
        "ui.bootstrap",
        "ngCacheBuster",
        "ui.bootstrap",
        "angular-loading-bar",
        "infinite-scroll",
        "ngCookies"])
        .config(function (httpRequestInterceptorCacheBusterProvider) {
            httpRequestInterceptorCacheBusterProvider.setMatchlist([/.*api.*/], true);
        });




// App init
console.i("Initializing app ...");

app.run(["$rootScope", "$location", "configDataService", function ($rootScope, $location, configDataService) {
    // This data is only configured on app startup ...
    $rootScope.awardsIdentifier = "";
    $rootScope.secureApiBaseUri = "";
    $rootScope.clientId = "";
    $rootScope.streamingUri = "";
    $rootScope.videoStreamingUri = "";
    $rootScope.appVersion = "";
    $rootScope.llnwWebCdnEnabled = false;
    $rootScope.llnwWebCdnHost = "";
    $rootScope.llnwWebCdnHost2 = "";
    $rootScope.enableLLNWStreaming = false;
    $rootScope.llnwHashSecret = "";

    $rootScope.require2FA = false;

    var urlPath = window.location; // can't use $location here, as you can only inject providers into the config
    var locale = urlPath.pathname;

    $rootScope.logoLocale = locale.replace("/", ".");

    // Global vars, this data is resetable data
    $rootScope.showMenuHint = false;
    $rootScope.firstLogin = false;
    $rootScope.showVotingControls = true;
    $rootScope.isDetailsView = false;
    $rootScope.detailsCategory = null;

    // about video player
    $rootScope.isVideoPlayer = false;
    $rootScope.judgeVideoPollingSeconds = 60;

    $rootScope.applyVersionedUrl = function (url) {
        return (url + "?v=" + $rootScope.appVersion);
    };
    $rootScope.getStreamingLink = function (mediaId) {
        return ($rootScope.streamingUri.format(mediaId));
    };
    $rootScope.getVideoStreamingLink = function (mediaId) {
        return $rootScope.videoStreamingUri.format(mediaId);
    };
    $rootScope.getVideoStreamingLinkRtmp = function (mediaId) {
        return $rootScope.videoStreamingUriRtmp.format(mediaId);
    };
    $rootScope.getVideoStreamingLinkOgg = function (mediaId) {
        return $rootScope.videoStreamingUriOgg.format(mediaId);
    };
    $rootScope.getVideoStreamingLinkOgv = function (mediaId) {
        return $rootScope.videoStreamingUriOgv.format(mediaId);
    };
    $rootScope.getImageUrl = function (mediaId) {
        var baseUrl = "/spa-app/media/image/png/";
        if ($rootScope.judgeLLNWWebCdnEnabled)
            baseUrl = "http://" + $rootScope.judgeLLNWWebCdnHost + baseUrl;
        var url = baseUrl + mediaId;
        if (!$.awards.is.nullOrEmpty(mediaId))
            return ($rootScope.applyVersionedUrl(url));
        return (url + "0"); // This will force the placeholder image to display
    };
    $rootScope.localizeUrl = function (url) {
        var urlPath = window.location;
        var locale = urlPath.pathname;
        return locale + url;
    };
    $rootScope.resetRootData = function () {
        console.i("Resetting rootScope data ...");

        $rootScope.showMenuHint = false;
        $rootScope.firstLogin = false;
        $rootScope.showVotingControls = true;
        $rootScope.isDetailsView = false;
        $rootScope.detailsCategory = null;
    };
    $rootScope.configApp = function () {
        configDataService.getAppConfig().then(function (response) {
            var data = response.data;
            $rootScope.clientId = data.clientId;
            $rootScope.secureApiBaseUri = data.secureApiBaseUri;
            $rootScope.streamingUri = data.streamingUri;
            $rootScope.videoStreamingUri = data.videoStreamingUri;
            $rootScope.videoStreamingUriOgg = data.videoStreamingUriOgg;
            $rootScope.videoStreamingUriOgv = data.videoStreamingUriOgv;
            $rootScope.videoStreamingUriRtmp = data.videoStreamingUriRtmp;
            $rootScope.appVersion = data.appVersion;
            $rootScope.awardsIdentifier = data.awardsIdentifier;
            $rootScope.llnwWebCdnEnabled = data.llnwWebCdnEnabled;
            $rootScope.llnwWebCdnHost = data.llnwWebCdnHost;
            $rootScope.llnwWebCdnHost2 = data.llnwWebCdnHost2;
            $rootScope.judgeLLNWWebCdnEnabled = data.judgeLLNWWebCdnEnabled;
            $rootScope.judgeLLNWWebCdnHost = data.judgeLLNWWebCdnHost;
            $rootScope.judgeLLNWWebCdnHost2 = data.judgeLLNWWebCdnHost2;
            $rootScope.enableLLNWStreaming = data.enableLLNWStreaming;
            $rootScope.llnwHashSecret = data.llnwHashSecret;
            $rootScope.judgeVideoPollingSeconds = data.judgeVideoPollingSeconds;
            $rootScope.requireResetForgotPassword = data.requireResetForgotPassword;
            $rootScope.enableHighwindsURLSigning = data.enableHighwindsURLSigning;
            $rootScope.highwindsURLSigningStreamingHashSecret = data.highwindsURLSigningStreamingHashSecret;
            $rootScope.highwindsURLSigningTTLInMinutes = data.highwindsURLSigningTTLInMinutes;
            $rootScope.judgeSiteCloseDate = data.judgeSiteCloseDate;
            $rootScope.require2FA = data.judgeRequire2FA;
            $rootScope.blockSiteAccessAfterSubmittingVotes = data.blockSiteAccessAfterSubmittingVotes;
            $rootScope.forceProfilePage = data.forceProfilePage;
            $rootScope.hideSignUpPanel = data.hideSignUpPanel;
            $rootScope.enableMembershipSignup = data.enableMembershipSignup;
            $rootScope.membershipTypeID = data.membershipTypeID;
            $rootScope.forceMemberManagerPage = data.forceMemberManagerPage;
            $rootScope.memberManagerEnabled = data.memberManagerEnabled;
        },
            function (error) {
                console.error('error loading config data!');
            });
    };

    $rootScope.validateRounds = function (roundsOpen) {
        if (!roundsOpen) {
            $location.path("/roundClosed");
        }
    };

    // Load the config for the app
    console.i("Loading config data ...");
    $rootScope.configApp();
}]);

app.config(["$httpProvider", function ($httpProvider) {
    $httpProvider.interceptors.push("authInterceptorService");
    $httpProvider.interceptors.push("localeInterceptorService");

    // Remove loading graphic stuff since it shows up on video playing
    //$httpProvider.responseInterceptors.push("httpInterceptorService");
    // var spinnerFunction = function spinnerFunction(data, headersGetter) {
    //    $("#loaderContainer").addClass("loading");
    //    return data;
    //};

    //$httpProvider.defaults.transformRequest.push(spinnerFunction);
}]);

app.run(["authService", function (authService) {
    authService.fillAuthData();
}]);