   return
 
 refreshModels = ->
-  getWatchedShows (_watchedShows) ->
-    console.log "Returned watchedShows: #{JSON.stringify _watchedShows}"
-    model.watchedShows = _watchedShows
-    getToWatchList (_toWatchList) ->
-      console.log 'toWatchList updated: ' + JSON.stringify _toWatchList
-      model.toWatchList = _toWatchList
-
-checkWatched = (episode) ->
-  console.log("Check watched! episode: #{JSON.stringify(episode)}")
+ traktvRequest 'sync/watched/shows', (response, status, req) ->
+    shows = response
+    # console.log "Returned shows: #{JSON.stringify shows.map (e)->e.show}"
+    getToWatchList shows, (toWatchList) ->
+      console.log 'toWatchList updated'
+      model.shows = toWatchList
+
+checkWatched = (episode, success, failure) ->
+  # console.log("Check watched! episode: #{JSON.stringify(episode)}")
   request =
     shows: [
-      episode.show
+      title: episode.show.title
+      year: episode.show.year
+      ids: episode.show.ids
       seasons: [
         number: episode.season
         episodes: [

    url: 'https://api-v2launch.trakt.tv/sync/history'
    method: 'POST'
    data: request
    (response, status, req) -> console.log "Check SUCCESS"

displayToWatchMenu = ->
  unless model.toWatchList?
    observer = (changes) ->
      unless 'toWatchList' in (change.name for change in changes)
        return

      console.log 'called observe. this is ', JSON.stringify(this)
      displayToWatchMenu()
      Object.unobserve model.toWatchList, 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 } ])
  toWatchMenu.on 'longSelect', (e) ->
    checkWatched(e.item.episode)
  toWatchMenu.on 'select', (e) ->
    episode = e.item.episode
    traktvRequest(
      "https://api-v2launch.trakt.tv/shows/#{episode.show.id}/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(
          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)
        )
        detailedItem.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}"
  if typeof opt == 'string'
    opt = if opt.indexOf('http') == 0
      url: opt

  url = if opt.url
    opt.url
  else
    if opt.action[0] == '/'
      opt.action = opt.action[1..]
    "https://api-v2launch.trakt.tv/#{opt.action}"

  # console.log "build url: " + JSON.stringify(url)

  data = opt.data

