Rails.logger = Logger.new(STDOUT)
require Rails.root.join('lib', 'google_client')

namespace :google do
  task create_broadcast: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')

    broadcast = music_session.create_stream(user, nil)

    puts broadcast.inspect
  end

  task get_livestream: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')

    livestream = music_session.get_livestream(google_client, user)

    puts livestream.inspect

    # example:
    #{"kind"=>"youtube#liveStream",
    # "etag"=>"\"m2yskBQFythfE4irbTIeOgYYfBU/fq4yPJsfCN91Qi2Dd9VuLZ3sm9U\"",
    # "id"=>"lgXe2spB8uVk-U6H_b8aSQ1495983496676422",
    # "snippet"=>{"publishedAt"=>"2017-05-28T14:58:16.000Z",
    # "channelId"=>"UClgXe2spB8uVk-U6H_b8aSQ",
    # "title"=>"Private Test Session", "description"=>"", "isDefaultStream"=>false},
    # "cdn"=>{"format"=>"360p", "ingestionType"=>"rtmp", "ingestionInfo"=>{"streamName"=>"6axs-3zpj-0esg-d7uv", "ingestionAddress"=>"rtmp://a.rtmp.youtube.com/live2", "backupIngestionAddress"=>"rtmp://b.rtmp.youtube.com/live2?backup=1"}, "resolution"=>"360p", "frameRate"=>"30fps"},
    # "status"=>{"streamStatus"=>"active", "healthStatus"=>{"status"=>"good", "configurationIssues"=>[{"type"=>"audioBitrateHigh", "severity"=>"info", "reason"=>"Check audio settings", "description"=>"The audio stream's current bitrate of 255.00 Kbps is higher than the recommended bitrate. We recommend that you use an audio stream bitrate of 128 Kbps."}, {"type"=>"bitrateHigh", "severity"=>"info", "reason"=>"Check video settings", "description"=>"The stream's current bitrate (1286.00 Kbps) is higher than the recommended bitrate. We recommend that you use a stream bitrate of 750 Kbps."}]}}}

  end

  task get_broadcast: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')

    broadcast = music_session.get_broadcast(google_client, user)

    # example:
    #{"kind"=>"youtube#liveBroadcast",
    # "etag"=>"\"m2yskBQFythfE4irbTIeOgYYfBU/7ARgOe0WJLQdOvjeISlnBnnK3L0\"",
    # "id"=>"4CLE1CXfKB8",
    # "snippet"=>{"publishedAt"=>"2017-05-28T14:58:16.000Z", "channelId"=>"UClgXe2spB8uVk-U6H_b8aSQ", "title"=>"Private Test Session", "description"=>"Private session set up just to test things out in the session interface by myself.", "thumbnails"=>{"default"=>{"url"=>"https://i9.ytimg.com/vi/4CLE1CXfKB8/default_live.jpg?sqp=CMzXq8kF&rs=AOn4CLCo_lxnPzD52m7nQgJ7xHsyRSBLkw", "width"=>120, "height"=>90}, "medium"=>{"url"=>"https://i9.ytimg.com/vi/4CLE1CXfKB8/mqdefault_live.jpg?sqp=CMzXq8kF&rs=AOn4CLDF5tDY7i0H__d27ZTdSfbvrpgc6w", "width"=>320, "height"=>180}, "high"=>{"url"=>"https://i9.ytimg.com/vi/4CLE1CXfKB8/hqdefault_live.jpg?sqp=CMzXq8kF&rs=AOn4CLAl1XUfz8faQDBXEydsDG9D5iRe6g", "width"=>480, "height"=>360}}, "scheduledStartTime"=>"2017-05-28T14:58:17.000Z", "isDefaultBroadcast"=>false, "liveChatId"=>"Cg0KCzRDTEUxQ1hmS0I4"},
    # "status"=>{"lifeCycleStatus"=>"testing", "privacyStatus"=>"private", "recordingStatus"=>"notRecording"},
    # "contentDetails"=>{"boundStreamId"=>"lgXe2spB8uVk-U6H_b8aSQ1495983496676422", "boundStreamLastUpdateTimeMs"=>"2017-05-28T14:58:16.695Z", "monitorStream"=>{"enableMonitorStream"=>true, "broadcastStreamDelayMs"=>0, "embedHtml"=>"<iframe width=\"425\" height=\"344\" src=\"https://www.youtube.com/embed/4CLE1CXfKB8?autoplay=1&livemonitor=1\" frameborder=\"0\" allowfullscreen></iframe>"}, "enableEmbed"=>true, "enableDvr"=>true, "enableContentEncryption"=>false, "startWithSlate"=>false, "recordFromStart"=>true, "enableClosedCaptions"=>false, "closedCaptionsType"=>"closedCaptionsDisabled", "enableLowLatency"=>false, "projection"=>"rectangular"}}

    puts broadcast.inspect
  end

  task transition_broadcast_live: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')


    livestream = music_session.transition_broadcast(google_client, user, music_session.current_broadcast, 'live')

    puts livestream.inspect
  end


  task transition_broadcast_testing: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')


    livestream = music_session.transition_broadcast(google_client, user, music_session.current_broadcast, 'testing')

    puts livestream.inspect
  end

  task transition_broadcast_complete: :environment do |task, args|
    google_client = JamRuby::GoogleClient.new
    music_session = MusicSession.first
    user = User.find_by_email('seth@jamkazam.com')

    livestream = music_session.transition_broadcast(google_client, user, music_session.current_broadcast, 'complete')

    puts livestream.inspect
  end
end
