(function(window) {
  'use strict';

  // ensure Kalamba global is available
  window.Kalamba = window.Kalamba || {};

  var QueryString = (function() {
    var query_string = {};
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split('=');
      pair[0] = decodeURIComponent((pair[0] + '').replace(/\+/g, '%20'));
      pair[1] = decodeURIComponent((pair[1] + '').replace(/\+/g, '%20'));
      if (typeof query_string[pair[0]] === 'undefined') {
        query_string[pair[0]] = pair[1];
      } else if (typeof query_string[pair[0]] === 'string') {
        var arr = [query_string[pair[0]], pair[1]];
        query_string[pair[0]] = arr;
      } else {
        query_string[pair[0]].push(pair[1]);
      }
    }
    return query_string;
  })();

  // Configuration implementation
  var Configuration = function(appVersion, serverUrl, staticUrl, buildUrl) {
    var badgeParameters = {
      autoplayType: 1,
      cageCode: 'OD1',
      cutTrailingZeros: false,
      fullscreenOverlay: false,
      minimumSpinDuration: 0,
      numberAbbreviations: [],
      quickDepositBalanceThresholds: [3, 1],
      realityCheckPeriodFormat: 'minutes',
      realityCheckShowCloseGame: true,
      realityCheckShowHistoryLink: true,
      realityCheckShowNetGamingActivity: true,
      realityCheckShowSessionDuration: true,
      realityCheckShowSumBets: true,
      realityCheckShowSumWins: true,
      showClock: true,
      showCloseGameButton: true,
      showFunBanner: true,
      showGameHistory: true,
      showGameInfoBar: true,
      showGameRules: true,
      showGameUi: true,
      showHyperBonus: true,
      showLoaderContinueButton: true,
      showMobileSoundPrompt: true,
      showQuickDeposit: false,
      showRoundId: false,
      showTurboButton: true,
      hideDeclineFreeRoundsButton: true,
      skipInsufficientFundsCheck: false,
      incompleteGamesResolutionType: 1,
      daysToAutoResolution: 90
    };
    if (QueryString.lobbyUrl === 'OFF') {
      badgeParameters.showCloseGameButton = false;
    }

    if (QueryString.gameCode === 'KLM_JOJ') {
      badgeParameters.showHyperBonus = false;
    }

    
    var resourcesPath = staticUrl.replace(/{CLIENT_VERSION}/, appVersion);

    // these parematers are read from URL (GET)
    var gameCode = QueryString.gameCode;
    var token = QueryString.token;
    var username = QueryString.username;
    var languageCode = typeof QueryString.languageCode !== 'undefined' ? QueryString.languageCode : 'ENG';
    var playMode = typeof QueryString.playMode !== 'undefined' ? QueryString.playMode : 'REAL';

    if (playMode == 'FUN') {
	badgeParameters.showGameHistory = false;
	badgeParameters.realityCheckShowHistoryLink = false;
    }

    var fullscreen = typeof QueryString.fullscreen !== 'undefined' ? QueryString.fullscreen === 'true' : true;
    
    return {
      cli: {
        cageCode: badgeParameters.cageCode,
        clientType: 3,
        gameCode: gameCode,
        languageCode: languageCode,
        playMode: playMode,
        skinCode: badgeParameters.cageCode,
        token: token,
        username: username,
        version: appVersion
      },
      ui: {
        // The type of autoplay to use with the game:
        // - 1: predefined options with loss and win limits,
        // - 2: predefined options, no limits,
        // - 3: no predefined options, plays until user presses stop button;
        // - 4: predefined options with loss and win limits, without "no limit" in loss limit
	// - 5: autoplay off
        // - null, undefined, or out of scope, default to 1.
        autoPlayType: badgeParameters.autoplayType,
        buildPath: buildUrl,
        feature: {
          fullscreen: fullscreen,
          fullscreenOverlay: badgeParameters.fullscreenOverlay,
          quickDepositOptions: {
            // Shows the maximum balance remaining as a multiple of current selected bet before quick deposit prompt appears, as a series of values.
            balanceThresholds: badgeParameters.quickDepositBalanceThresholds,
            // Whether or not to show quick deposit at all
            showQuickDeposit: badgeParameters.showQuickDeposit,
          },
          showClock: badgeParameters.showClock,
          showCloseGameButton: badgeParameters.showCloseGameButton,
          showFunBanner: badgeParameters.showFunBanner,
          showGameHistory: badgeParameters.showGameHistory,
          showRoundId: badgeParameters.showRoundId,
          showGameInfoBar: badgeParameters.showGameInfoBar,
          showGameUi: badgeParameters.showGameUi,
          showHyperBonus: badgeParameters.showHyperBonus,
          showLoaderContinueButton: badgeParameters.showLoaderContinueButton,
          showMobileSoundPrompt: badgeParameters.showMobileSoundPrompt,
          showTurboButton: badgeParameters.showTurboButton,
          skipInsufficientFundsCheck: badgeParameters.skipInsufficientFundsCheck,
          hideDeclineFreeRoundsButton: badgeParameters.hideDeclineFreeRoundsButton
        },
        incompleteGameResolution: {
          // In case of automatic incomplete game resolution it defines days to auto resolution
          // - null, undefined, or out of scope, default to 90.
          daysToAutoResolution: badgeParameters.daysToAutoResolution,
          // The type of incomplete game resolution:
          // - 1: none
          // - 2: manual
          // - 3: automatic
          // - null, undefined, or out of scope, default to 1.
          incompleteGameResolutionType: badgeParameters.incompleteGamesResolutionType
        },
        // Minimum spin duration:
        // - if this is null, absent or <= 0, there is no minimum duration.
        // - if this is defined and positive, then this is the minimum interval between base game spins, in seconds.
        minimumSpinDuration: badgeParameters.minimumSpinDuration,
        // Number formatting
        numberFormat: {
          cutTrailingZeros: badgeParameters.cutTrailingZeros,
          decimalSeparator: badgeParameters.decimalSeparator,
          numberAbbreviations: badgeParameters.numberAbbreviations,
        },
        // Properties to define how reality check message is displayed, in order to comply with the deployed jurisdiction.
        // The game code is not meant to know the rules for each jurisdiction so this is included here.
        realityCheckType: {
          sessionDurationPeriodFormat: badgeParameters.realityCheckPeriodFormat,
          showCloseGame: badgeParameters.realityCheckShowCloseGame,
          showHistoryLink: badgeParameters.realityCheckShowHistoryLink,
          showNetGamingActivity: badgeParameters.realityCheckShowNetGamingActivity,
          showSessionDuration: badgeParameters.realityCheckShowSessionDuration,
          showSumBets: badgeParameters.realityCheckShowSumBets,
          showSumWins: badgeParameters.realityCheckShowSumWins,
        },
        resourcesPath: resourcesPath,
      },
      url: serverUrl,
    };
  };

  window.Kalamba.Configuration = Configuration;
})(window);
