class window.HomeCtrl extends BaseCtrl
  constructor: (@$scope, @$rootScope, @$http, @$state, @$injector, @Boards, @Dock, @Analytics, @Constants, @MixPanel) ->
    super()
    @nudgePreloader()
    @noBoards = false

    @getSite()
    @getBoards()
    @$rootScope.index = true

  gotoInfo: () =>
   @$state.go(@Constants.routes.siteinfo)

  goToEntry: (boardId, entryId, type) ->
    delete @Dock.data.entry
    if type == 'photo'
      @$state.go(@Constants.routes.viewPhoto, {boardId: boardId, entryId: entryId})
    if type == 'text'
      @$state.go(@Constants.routes.viewText, {boardId: boardId, entryId: entryId})
    if type == 'social_video'
      @$state.go(@Constants.routes.viewVideo, {boardId: boardId, entryId: entryId})

  goToBoard: (board) =>
    delete @Dock.data.board
    @$state.go(@Constants.routes.boardbase, {boardId: board.id})

  onSiteLoad: (data, headers) =>
    super(data, headers)
    @$rootScope.description = data.details.description

  getUser: =>
    console.log "getting user" if DEBUG
    #can't use $resource here, need a fresh instance in case token has changed
    @$http.get("#{apiPrefix}/v2/me.json", {headers: {'Authorization': @Dock.getUserToken()}})
    .success (data) =>
        @onGetUser(data)
    .error (error, status) =>
        @onGetUserError(error)

  onGetUser: (data) =>
    console.log "got user" if DEBUG
    @Dock.setUserInfo(data)
    @MixPanel.setUser(data)
    @getBoards()

  onGetUserError: (error) =>
    console.log error if DEBUG
    @showError()

  getBoards: =>
    if !@Dock.data.boardList
      @Boards.query(@onGetBoards, @onGetBoardsError)
    else
      console.log "got boards already" if DEBUG
      @progress()

  goToUpload: (board) =>
    @Board = @$injector.get("Board")
    @Board.get({id: board.id}, @onGetBoard, @onSiteError)

  onGetBoard: (data) =>
    @Dock.setBoard(data)
    @$rootScope.description = @Dock.data.board.description
    @$state.go(@Constants.routes.user, {boardId: @Dock.data.board.id})

  onGetBoards: (data) =>
    console.log "got boards" if DEBUG
    @Dock.setBoardList(data)
    @progress()

  onGetBoardsError: (error) =>
    console.log "error is #{error}" if DEBUG
    @showError()

  progress: =>
    console.log "home#progress"
    numBoards = @Dock.getNumberOfBoards()
    console.log numBoards if DEBUG

  showError: =>
    @$state.go(@Constants.routes.error)

HomeCtrl.$inject = ["$scope", "$rootScope", "$http", "$state", "$injector", "Boards", "Dock", "Analytics", "Constants", "MixPanel"]

angular.module("shuttlerockApp").controller("HomeCtrl", HomeCtrl)