// Licensed under the Apache License. See footer for details.

var DefaultSearch = "Sad_Worker"

var app = angular.module("um-radio", ["ngResource"])

app.config(function($locationProvider) {
  $locationProvider.html5Mode({
    enabled:     true,
    requireBase: false
  })
})

app.controller("BodyController", BodyController)

//------------------------------------------------------------------------------
function BodyController($scope, $rootScope, $timeout, $sce, $location) {

  $scope.timeout           = $timeout
  $scope.sce               = $sce
  $scope.loc               = $location

  $scope.helpShown         = false
  $scope.message           = null

  $scope.twitterSearchText = ""
  $scope.mix               = null

  clearSearch($scope)

  var q = $scope.loc.search().q
  if (!q) {
    $scope.loc.search({q: DefaultSearch})
  }

  //-----------------------------------
  $rootScope.$on("$locationChangeSuccess", function(){
    LocationChanged($scope)
  })

  $scope.toggleHelp = function() {
    ToggleHelp($scope)
  }

  $scope.twitterSearchEntered = function() {
    TwitterSearchEntered($scope)
  }

  $scope.twitterSearchPerform = function(search) {
    TwitterSearchPerform($scope, search)
  }

  //-----------------------------------
  $scope.inAng = function(label, fn) {
    // console.log("@ ang block: " + label)
    $scope.timeout(function() {
      // console.log("-> ang block: " + label)
      fn()
      // console.log("<- ang block: " + label)
    },1)
  }
}

//------------------------------------------------------------------------------
function LocationChanged($scope) {
  var q = $scope.loc.search().q || DefaultSearch

  TwitterSearchPerform($scope, q)
}

//------------------------------------------------------------------------------
function ToggleHelp($scope) {
  $scope.helpShown = !$scope.helpShown
}

//------------------------------------------------------------------------------
function TwitterSearchEntered($scope) {
  clearSearch($scope)

  $scope.inAng("twitterSearchEntered", function(){
    $("#twitterSearchText").blur()
  })

  $scope.loc.search({q: $scope.twitterSearchText})
}

//------------------------------------------------------------------------------
function TwitterSearchPerform($scope, search) {
  search = search || DefaultSearch
  clearSearch($scope)

  $scope.twitterSearchText = search

  TwitterGetTweets($scope, search)
}

//------------------------------------------------------------------------------
function clearSearch($scope) {
  $scope.tweets = []
  $scope.scores = []
}

//------------------------------------------------------------------------------
// Copyright IBM Corp. 2014
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------

@@ -819,16 +819,19 @@
 rch(%7Bq: 
+get
 DefaultS
@@ -835,16 +835,18 @@
 ltSearch
+()
 %7D)%0A  %7D%0A%0A
@@ -1640,24 +1640,27 @@
 arch().q %7C%7C 
+get
 DefaultSearc
@@ -1656,24 +1656,26 @@
 efaultSearch
+()
 %0A%0A  TwitterS
@@ -2294,16 +2294,19 @@
 arch %7C%7C 
+get
 DefaultS
@@ -2310,16 +2310,46 @@
 ltSearch
+()%0A  setDefaultSearch(search)%0A
 %0A  clear
@@ -2572,16 +2572,16 @@
 ts = %5B%5D%0A
-
   $scope
@@ -2592,24 +2592,462 @@
 res = %5B%5D%0A%7D%0A%0A
+//------------------------------------------------------------------------------%0Afunction getDefaultSearch() %7B%0A  if (!window.localStorage) return DefaultSearch%0A%0A  return window.localStorage.umRadioDefaultSearch %7C%7C DefaultSearch%0A%7D%0A%0A//------------------------------------------------------------------------------%0Afunction setDefaultSearch(value) %7B%0A  if (!window.localStorage) return%0A%0A  window.localStorage.umRadioDefaultSearch = value%0A%0A%7D%0A%0A
 //----------

save last search in localStorage