<commit_before>class Kandan.Plugins.MusicPlayer

  @pluginNamespace: "Kandan.Plugins.MusicPlayer"
  @pluginId: ""
  @widgetTitle: "Player"
  @playRegex: /^&#x2F;play .+/
  @stopRegex: /^&#x2F;stop/
  @resumeRegex: /^&#x2F;resume/
  @localSongData: false
  @sounds: {
    channel: 'ding.wav'
    attention: 'threetone-alert.wav'
  }

  @playTemplate:   _.template('<strong><a class="audio-play">playing</a> <a target="_blank" href="<%- url %>"><%- url %></a></strong>')
  @stopTemplate:   _.template('<strong><a class="audio-play">stopping</a> the music.')
  @resumeTemplate: _.template('<strong><a class="audio-play">resuming</a> the music.')
  @songTemplate:   _.template('<li><%= song.split("/").pop() %></li>')


  @setError: (errorMessage)->
    console.log "music player error", errorMessage


  @createSongList: (songs)->
    $songs = $('<ul class="songs"></ul>')
    if songs.length == 0
      $songs = "No songs! Maybe add some?"
    else
      $songs.append(@songTemplate({song: song})) for song in songs
    return $songs


  @render: ($widgetEl)->
    $widgetElementClass = $widgetEl.attr('class')

    if @localSongData
      $songs = @createSongList(@localSongData)
    else
      @getSongs({
        success: (songs)=>
          $songs = @createSongList(songs)

        failure: ()->
          @setError("Could not load songs")
      })
    $widgetEl.html($songs)


  # TODO: Add support for sounds
  @init: (pluginId)->
    @pluginId = pluginId
    Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
    @registerPlayModifier()
    @registerStopModifier()
    @registerResumeModifier()
    # Disabled for now
    #@registerWidget()


  @registerWidget: ()->
    Kandan.Widgets.register @pluginNamespace


  @registerPlayModifier: ()->
    Kandan.Modifiers.register @playRegex, (message, activity) =>
      url = $.trim(message.substr(message.indexOf(" ") + 1));
      if true and Kandan.Data.Channels.activeChannelId()?
        rawInput  = Kandan.Helpers.Utils.unescape(url)
        soundUrl  = null
        soundUrl  = @localSounds(rawInput)
        soundUrl ?= rawInput

        @playUrl(activity.channel_id, soundUrl)
      else
        console.log "Not playing stale song"

      message.content = @playTemplate({url: url})
      return Kandan.Helpers.Activities.buildFromBaseTemplate message

  @registerStopModifier: ()->
    Kandan.Modifiers.register @stopRegex, (message, activity) =>
      url = $.trim(message.substr(message.indexOf(" ") + 1));
      if true and Kandan.Data.Channels.activeChannelId()?
        @stopSound(activity.channel_id)

      message = @stopTemplate()
      return message

  @registerResumeModifier: ()-><commit_msg>Fixed issues with not properly rendering played sounds/songs.<commit_after>class Kandan.Plugins.MusicPlayer

  @pluginNamespace: "Kandan.Plugins.MusicPlayer"
  @pluginId: ""
  @widgetTitle: "Player"
  @playRegex: /^&#x2F;play .+/
  @stopRegex: /^&#x2F;stop/
  @resumeRegex: /^&#x2F;resume/
  @localSongData: false
  @sounds: {
    channel: 'ding.wav'
    attention: 'threetone-alert.wav'
  }

  @playTemplate:   _.template('<i>play <a target="_blank" href="<%- soundUrl %>"><%- url %></a></i>')
  @stopTemplate:   _.template('<i>stop the music.</i>')
  @resumeTemplate: _.template('<i>resume the music.</i>')
  @songTemplate:   _.template('<li><%= song.split("/").pop() %></li>')


  @setError: (errorMessage)->
    console.log "music player error", errorMessage


  @createSongList: (songs)->
    $songs = $('<ul class="songs"></ul>')
    if songs.length == 0
      $songs = "No songs! Maybe add some?"
    else
      $songs.append(@songTemplate({song: song})) for song in songs
    return $songs


  @render: ($widgetEl)->
    $widgetElementClass = $widgetEl.attr('class')

    if @localSongData
      $songs = @createSongList(@localSongData)
    else
      @getSongs({
        success: (songs)=>
          $songs = @createSongList(songs)

        failure: ()->
          @setError("Could not load songs")
      })
    $widgetEl.html($songs)


  # TODO: Add support for sounds
  @init: (pluginId)->
    @pluginId = pluginId
    Kandan.Data.Channels.registerCallback("change", $.proxy(@onChannelChange, this))
    @registerPlayModifier()
    @registerStopModifier()
    @registerResumeModifier()
    # Disabled for now
    #@registerWidget()


  @registerWidget: ()->
    Kandan.Widgets.register @pluginNamespace


  @registerPlayModifier: ()->
    Kandan.Modifiers.register @playRegex, (message, activity) =>
      url = $.trim(message.substr(message.indexOf(" ") + 1));
      rawInput  = Kandan.Helpers.Utils.unescape(url)
      soundUrl  = null
      soundUrl  = @localSounds(rawInput)
      soundUrl ?= rawInput

      if true and Kandan.Data.Channels.activeChannelId()?
        @playUrl(activity.channel_id, soundUrl)
      else
        console.log "Not playing stale song"

      message = @playTemplate({url: url, soundUrl: soundUrl})
      return message #Kandan.Helpers.Activities.buildFromBaseTemplate message

  @registerStopModifier: ()->
    Kandan.Modifiers.register @stopRegex, (message, activity) =>
      url = $.trim(message.substr(message.indexOf(" ") + 1));
      if true and Kandan.Data.Channels.activeChannelId()?
        @stopSound(activity.channel_id)

      message = @stopTemplate()
      return message

  @registerResumeModifier: ()->
{'fraction_non_alphanumeric': 0.15879828326180256, 'fraction_numerical': 0.0022392237357715993, 'mean_word_length': 3.3683781581092096, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2890938', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1726, 'n_words': 341}