context = window
logger = context.JK.logger
MixerActions = @MixerActions
MIX_MODES = context.JK.MIX_MODES

@SessionMetronome = React.createClass({

  mixins: [@MasterPersonalMixersMixin]

  handleMute: (e) ->
    e.preventDefault()

    muting = $(e.currentTarget).is('.enabled')

    # this button is annoying. the hover shows mute and it's good to use instead
    # MixerActions.mute([@mixer()], muting)

  render: () ->

    # today, all mixers are the same for a remote participant; so just grab the 1st
    mixers = @mixers()

    muteMixer = mixers.muteMixer
    vuMixer = mixers.vuMixer
    muteMixerId = muteMixer?.id

    classes = classNames({
      'track-icon-mute': true
      'enabled' : !muteMixer?.mute
      'muted' : muteMixer?.mute
    })

    componentClasses = classNames({
      "session-track" : true
      "metronome" : true
      "in-jam-track" : @props.location == 'jam-track'
      "no-mixer" : @props.mode == MIX_MODES.MASTER  # show it as disabled if in master mode
      "in-jam-track" : @props.location == 'jam-track'
    })

    pan = if mixers.mixer? then mixers.mixer?.pan else 0

    panStyle = {
      transform: "rotate(#{pan}deg)"
      WebkitTransform: "rotate(#{pan}deg)"
    }

    `<div className={componentClasses}>
      <div className="disabled-track-overlay" />
      <div className="session-track-contents">
        <div className="name">Metronome</div>
        <div className="track-controls">
          <SessionTrackVU orientation="horizontal" lightCount={4} lightWidth={17} lightHeight={3} side="best" mixers={mixers} />
          <div className="track-buttons">
            <div className="track-instrument"><img height="24" src={this.props.instrumentIcon} width="24" /></div>
            <div className="track-icon-pan" style={panStyle}/>
            <div className={classes} data-control="mute" data-mixer-id={muteMixerId} onClick={this.handleMute}/>
          </div>
          <br className="clearall"/>
        </div>

        <br className="clearall"/>
      </div>
    </div>`

  componentDidMount: () ->


    $root = $(this.getDOMNode())
    $topParent = $root.closest('.top-parent')
    $mute = $root.find('.track-icon-mute')
    $pan  = $root.find('.track-icon-pan')


    if @props.mode
      context.JK.helpBubble($root.find('.disabled-track-overlay'), 'master-metronome-notice', {}, {positions:['top'], offsetParent: $topParent})

    context.JK.interactReactBubble(
      $mute,
      'SessionTrackVolumeHover',
      () =>
        {mixers:@mixers(), trackType: 'SessionMetronome', mode: @props.mode}
      ,
      {width:235, positions:['right', 'left'], offsetParent:$topParent})

    context.JK.interactReactBubble(
      $pan,
      'SessionTrackPanHover',
      () =>
        {mixers:@mixers()}
      ,
      {width:331, positions:['right', 'left'], offsetParent:$topParent})

})
