<commit_before>class MainController extends KDController

  ###

  * EMITTED EVENTS
    - AppIsReady
    - FrameworkIsReady
    - AccountChanged                [account, firstLoad]
    - pageLoaded.as.loggedIn        [account, connectedState, firstLoad]
    - pageLoaded.as.loggedOut       [account, connectedState, firstLoad]
    - accountChanged.to.loggedIn    [account, connectedState, firstLoad]
    - accountChanged.to.loggedOut   [account, connectedState, firstLoad]
  ###


  connectedState =
    connected   : no

  constructor:(options = {}, data)->

    options.failWait  = 5000            # duration in miliseconds to show a connection failed modal

    super options, data

    # window.appManager is there for backwards compatibilty
    # will be deprecated soon.
    window.appManager = new ApplicationManager

    KD.registerSingleton "appManager", appManager
    KD.registerSingleton "mainController", @
    KD.registerSingleton "kiteController", new KiteController
    KD.registerSingleton "vmController", new VirtualizationController
    KD.registerSingleton "contentDisplayController", new ContentDisplayController
    KD.registerSingleton "notificationController", new NotificationController
    KD.registerSingleton "localStorageController", new LocalStorageController
    # KD.registerSingleton "fatih", new Fatih

    KD.registerSingleton "linkController", new LinkController

    router = new KodingRouter location.pathname
    KD.registerSingleton 'router', router

    appManager.create 'Groups', (groupsController)->
      KD.registerSingleton "groupsController", groupsController

    # appManager.create 'Chat', (chatController)->
    #   KD.registerSingleton "chatController", chatController

    @ready =>
      router.listen()
      KD.registerSingleton "activityController", new ActivityController
      KD.registerSingleton "kodingAppsController", new KodingAppsController
      #KD.registerSingleton "bottomPanelController", new BottomPanelController
      @emit 'AppIsReady'
      @emit 'FrameworkIsReady'

    @setFailTimer()
    @attachListeners()

    @appStorages = {}

    @introductionTooltipController = new IntroductionTooltipController

  # FIXME GG
  getAppStorageSingleton:(appName, version)->
    if @appStorages[appName]?
      storage = @appStorages[appName]
    else
      storage = @appStorages[appName] = new AppStorage appName, version

    storage.fetchStorage()
    return storage

  accountChanged:(account, firstLoad = no)->

    @userAccount             = account
    connectedState.connected = yes

    KD.whoami().fetchMyPermissionsAndRoles (err, permissions, roles)=>
      return warn err  if err
      KD.config.roles       = roles
      KD.config.permissions = permissions

      @ready @emit.bind @, "AccountChanged", account, firstLoad

      unless @mainViewController
        @loginScreen = new LoginView
        KDView.appendToDOMBody @loginScreen

        @mainViewController  = new MainViewController
          view    : mainView = new MainView
            domId : "kdmaincontainer"

        KDView.appendToDOMBody mainView

        @decorateBodyTag()
        @emit 'ready'

        eventPrefix = if firstLoad then "pageLoaded.as" else "accountChanged.to"
        eventSuffix = if @isUserLoggedIn() then "loggedIn" else "loggedOut"

        # this emits following events
        # -> "pageLoaded.as.loggedIn"
        # -> "pageLoaded.as.loggedOut"
        # -> "accountChanged.to.loggedIn"
        # -> "accountChanged.to.loggedOut"

        @emit "#{eventPrefix}.#{eventSuffix}", account, connectedState, firstLoad

  doLogout:->

    KD.logout()
    KD.remote.api.JUser.logout (err, account, replacementToken)=>
      $.cookie 'clientId', replacementToken if replacementToken
      @accountChanged account

    # fixme: make a old tv switch off animation and reload
    # $('body').addClass "turn-off"
    return location.reload()

  attachListeners:->

    # @on 'pageLoaded.as.(loggedIn|loggedOut)', (account)=>
    #   log "pageLoaded", @isUserLoggedIn()


  # some day we'll have this :)
  hashDidChange:(params,query)->

  setVisitor:(visitor)-> @visitor = visitor
  getVisitor: -> @visitor
  getAccount: -> KD.whoami()

  isUserLoggedIn: -> KD.whoami() instanceof KD.remote.api.JAccount

  unmarkUserAsTroll:(data)->

    kallback = (acc)=><commit_msg>sitewide: some mysterious indentation discovered and fixed<commit_after>class MainController extends KDController

  ###

  * EMITTED EVENTS
    - AppIsReady
    - FrameworkIsReady
    - AccountChanged                [account, firstLoad]
    - pageLoaded.as.loggedIn        [account, connectedState, firstLoad]
    - pageLoaded.as.loggedOut       [account, connectedState, firstLoad]
    - accountChanged.to.loggedIn    [account, connectedState, firstLoad]
    - accountChanged.to.loggedOut   [account, connectedState, firstLoad]
  ###


  connectedState =
    connected   : no

  constructor:(options = {}, data)->

    options.failWait  = 5000            # duration in miliseconds to show a connection failed modal

    super options, data

    # window.appManager is there for backwards compatibilty
    # will be deprecated soon.
    window.appManager = new ApplicationManager

    KD.registerSingleton "appManager", appManager
    KD.registerSingleton "mainController", @
    KD.registerSingleton "kiteController", new KiteController
    KD.registerSingleton "vmController", new VirtualizationController
    KD.registerSingleton "contentDisplayController", new ContentDisplayController
    KD.registerSingleton "notificationController", new NotificationController
    KD.registerSingleton "localStorageController", new LocalStorageController
    # KD.registerSingleton "fatih", new Fatih

    KD.registerSingleton "linkController", new LinkController

    router = new KodingRouter location.pathname
    KD.registerSingleton 'router', router

    appManager.create 'Groups', (groupsController)->
      KD.registerSingleton "groupsController", groupsController

    # appManager.create 'Chat', (chatController)->
    #   KD.registerSingleton "chatController", chatController

    @ready =>
      router.listen()
      KD.registerSingleton "activityController", new ActivityController
      KD.registerSingleton "kodingAppsController", new KodingAppsController
      #KD.registerSingleton "bottomPanelController", new BottomPanelController
      @emit 'AppIsReady'
      @emit 'FrameworkIsReady'

    @setFailTimer()
    @attachListeners()

    @appStorages = {}

    @introductionTooltipController = new IntroductionTooltipController

  # FIXME GG
  getAppStorageSingleton:(appName, version)->
    if @appStorages[appName]?
      storage = @appStorages[appName]
    else
      storage = @appStorages[appName] = new AppStorage appName, version

    storage.fetchStorage()
    return storage

  accountChanged:(account, firstLoad = no)->

    @userAccount             = account
    connectedState.connected = yes

    KD.whoami().fetchMyPermissionsAndRoles (err, permissions, roles)=>
      return warn err  if err
      KD.config.roles       = roles
      KD.config.permissions = permissions

      @ready @emit.bind @, "AccountChanged", account, firstLoad

      @createMainViewController()  unless @mainViewController

      @decorateBodyTag()
      @emit 'ready'
      # this emits following events
      # -> "pageLoaded.as.loggedIn"
      # -> "pageLoaded.as.loggedOut"
      # -> "accountChanged.to.loggedIn"
      # -> "accountChanged.to.loggedOut"
      eventPrefix = if firstLoad then "pageLoaded.as" else "accountChanged.to"
      eventSuffix = if @isUserLoggedIn() then "loggedIn" else "loggedOut"
      @emit "#{eventPrefix}.#{eventSuffix}", account, connectedState, firstLoad

  createMainViewController:->
    @loginScreen = new LoginView
    KDView.appendToDOMBody @loginScreen
    @mainViewController  = new MainViewController
      view    : mainView = new MainView
        domId : "kdmaincontainer"
    KDView.appendToDOMBody mainView

  doLogout:->

    KD.logout()
    KD.remote.api.JUser.logout (err, account, replacementToken)=>
      $.cookie 'clientId', replacementToken if replacementToken
      @accountChanged account

    # fixme: make a old tv switch off animation and reload
    # $('body').addClass "turn-off"
    return location.reload()

  attachListeners:->

    # @on 'pageLoaded.as.(loggedIn|loggedOut)', (account)=>
    #   log "pageLoaded", @isUserLoggedIn()


  # some day we'll have this :)
  hashDidChange:(params,query)->

  setVisitor:(visitor)-> @visitor = visitor
  getVisitor: -> @visitor
  getAccount: -> KD.whoami()

  isUserLoggedIn: -> KD.whoami() instanceof KD.remote.api.JAccount

  unmarkUserAsTroll:(data)->

    kallback = (acc)=>
{'fraction_non_alphanumeric': 0.0959862385321101, 'fraction_numerical': 0.0009174311926605505, 'mean_word_length': 3.4201723264064876, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956884', 'n_tokens_mistral': 2526, 'n_tokens_neox': 2357, 'n_words': 567}