     url: 'https://api-v2launch.trakt.tv/sync/history'
     method: 'POST'
     data: request
-    (response, status, req) -> console.log "Check SUCCESS"
+    (response, status, req) ->
+      console.log "Check SUCCESS"
+      # console.log "#{index}: #{key}: #{value}" for key, value of index for index in model.shows
+      itemToMark = item for item in model.shows when item.show.ids.trakt == episode.show.ids.trakt
+      seasonToMark = season for season in itemToMark.seasons when season.number == episode.season
+      console.log "season to mark: #{JSON.stringify seasonToMark}"
+      episodeToMark = ep for ep in seasonToMark.episodes when ep.number == episode.episode
+      console.log "ep to mark: #{JSON.stringify episodeToMark}"
+      episodeToMark.completed = true
+      # console.log "found show. #{key}, #{value}" for key, value of show
+      # console.log "I must update loca show #{show.title}"
+      success()
+    (response, status, req) ->
+      console.log "Check FAILURE"
+      failure(response, status, req)
+
+
+compareByKey = (key) ->
+  (a, b) ->
+    -1 if a[key] < b[key]
+    0 if a[key] == b[key]
+    1 if a[key] > b[key]
+
+# {
+#   episode: 12
+#   season: 2
+# } or undefined
+firstUnwatchedEpisode = (show) ->
+  seasons = show.seasons[..]
+  seasons.sort compareByKey('number')
+  for season in seasons
+    # console.log "considering #{JSON.stringify season}"
+    episodes = season.episodes[..]
+    episodes.sort(compareByKey('number'))
+    for episode in episodes
+      unless episode.completed
+        return {
+          episode: episode.number
+          season: season.number
+        }
+
 
 displayToWatchMenu = ->
-  unless model.toWatchList?
+  unless model.shows?
     observer = (changes) ->
-      unless 'toWatchList' in (change.name for change in changes)
+      unless 'shows' in (change.name for change in changes)
         return
 
       console.log 'called observe. this is ', JSON.stringify(this)
       displayToWatchMenu()
-      Object.unobserve model.toWatchList, observer
+      Object.unobserve model.shows, observer
 
     Object.observe model, observer
     return;
-  items = []
-  console.log 'displayToWatchMenu: toWatchList: ', JSON.stringify(model.toWatchList)
-  for ep in model.toWatchList
-    console.log "considering: " + JSON.stringify(ep)
-    items.push
-      title: ep.show.title
-      subtitle: 'Season ' + ep.season + ' Ep. ' + ep.episode
-      episode: ep
-
-  console.log 'obtained items: ', JSON.stringify(items)
-  toWatchMenu = new (UI.Menu)(sections: [ { 'items': items } ])
+
+  getToWatchMenuItems = ->
+    toWatch = []
+    for item in model.shows
+      # console.log "Title: #{item.show.title}"
+      ep = firstUnwatchedEpisode(item)
+      # console.log "firstUnwatchedEpisode: #{JSON.stringify ep}}"
+      if ep?
+        toWatch.push
+          title: item.show.title
+          subtitle: "Season #{ep.season} Ep. #{ep.episode}"
+          data:
+            episode: ep.episode
+            season: ep.season
+            show: item.show
+    toWatch
+
+  toWatchMenu = new UI.Menu(
+    sections: [{
+      items: getToWatchMenuItems()
+      }]
+    )
+  checkHandler = (e) ->
+    console.log "Deleting menu item [#{e.sectionIndex}, #{e.itemIndex}]"
+    toWatchMenu.items(0, getToWatchMenuItems())
+
   toWatchMenu.on 'longSelect', (e) ->
-    checkWatched(e.item.episode)
+    e.item.subtitle = "Checking in..."
+    toWatchMenu.item(e.sectionIndex, e.itemIndex, e.item)
+    checkWatched e.item.data, ->
+      console.log "End of checkWatched. needt to remove"
+      console.log "#{key}: #{value}" for key, value of e
+      checkHandler(e)
+
   toWatchMenu.on 'select', (e) ->
-    episode = e.item.episode
+    episode = e.item.data
     traktvRequest(
-      "https://api-v2launch.trakt.tv/shows/#{episode.show.id}/seasons/#{episode.season}/episodes/#{episode.episode}",
+      "https://api-v2launch.trakt.tv/shows/#{episode.show.ids.trakt}/seasons/#{episode.season}/episodes/#{episode.episode}",
       (response, status, req) ->
-        # response = {
-        #   "season": 1,
-        #   "number": 1,
-        #   "title": "Winter Is Coming",
-        #   "ids": {
-        #     "trakt": 36440,
-        #     "tvdb": 3254641,
-        #     "imdb": "tt1480055",
-        #     "tmdb": 63056,
-        #     "tvrage": null
-        #   }
-        # }
-        detailedItem = new UI.Card(
+        detailedItemCard = new UI.Card(
           title: episode.show.title
           subtitle: "Season #{episode.season} Ep. #{episode.episode}"
           body: "Title: #{response.title}"
           style: 'small'
         )
-        detailedItem.on('longClick', 'select', ->
-          detailedItem.hide()
-          checkWatched(episode)
+        detailedItemCard.on('longClick', 'select', ->
+          e.item.subtitle = "Checking in..."
+          toWatchMenu.item(e.sectionIndex, e.itemIndex, e.item)
+          detailedItemCard.hide()
+          checkWatched episode, -> checkHandler(e)
         )
-        detailedItem.show()
+        detailedItemCard.show()
     )
   toWatchMenu.show()
   return
 
-CONFIG_BASE_URL = 'http://bobby.alessiobogon.com:8020/'
-WATCHED_URL = 'https://api-v2launch.trakt.tv/users/me/watched/shows'
-signInWindow = undefined
-mainMenu = undefined
-
-###
-[{
-  title: "abc",
-  year: 2014,
-  id: 1234
-}, ...]
-###
-
 traktvRequest = (opt, success, failure) ->
   console.log "traktvRequest: opt: #{JSON.stringify opt}"
+  # console.log success
   if typeof opt == 'string'
     opt = if opt.indexOf('http') == 0
       url: opt

    },{
      title: 'Calendar'
      id: 'calendar'
    }]
  ]

mainMenu.on 'select', (e) ->
  switch e.item.id
    when 'toWatch'
        displayToWatchMenu()



if PEBBLE_DEVELOPER?
  newSectionIndex = mainMenu.state.sections.length
  mainMenu.items newSectionIndex, [
    title: 'Developer tools'
    id: 'developer'
  ]
  mainMenu.on 'select', (e) ->
    switch e.item.id
      when 'developer'
        devMenu = new UI.Menu
          sections: [
            items: [{
              title: 'Reset localStorage'
              action: localStorage.clear()
            }, {
              title: 'SAY MY NAME!'
              action: -> console.log 'YOU\'RE HEISENBERG'
            }]
          ]

        devMenu.on 'select', (e) -> e.item.action()
        devMenu.show()

mainMenu.show()
refreshModels()

console.log "PEBBLE_DEVELOPER: " + typeof PEBBLE_DEVELOPER


    headers:
      'trakt-api-version': 2
      'trakt-api-key': '16fc8c04f10ebdf6074611891c7ce2727b4fcae3d2ab2df177625989543085e9'
      Authorization: "Bearer #{accessToken}"
    method: method
    data: data
    success
    (response, status, req) ->
      if status == 401
        console.log "Server says that authorization is required"
        displaySignInWindow()
      console.log "Request failure (#{status} #{method} #{url})"
      failure(response, status, req)


# Set a configurable with the open callback
