context = window

MixerActions = @MixerActions

@SessionMasterOtherTrack = React.createClass({

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

    unless @props.mixers.mixer?
      logger.debug("ignoring mute; no mixer")
      return

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

    MixerActions.mute([@props.mixers.mixer], muting)

  render: () ->

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

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

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

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

    # <div className="track-icon-equalizer" />

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


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

  componentDidMount: () ->

    $root = $(this.getDOMNode())
    $mute = $root.find('.track-icon-mute')
    $pan  = $root.find('.track-icon-pan')

    context.JK.interactReactBubble(
      $mute,
      'SessionTrackVolumeHover',
      () =>
        {mixers:this.props.mixers}
      ,
      {width:235, positions:['right', 'left'], offsetParent:$root.closest('.top-parent')})

    context.JK.interactReactBubble(
      $pan,
      'SessionTrackPanHover',
      () =>
        {mixers:this.props.mixers}
      ,
      {width:331, positions:['right', 'left'], offsetParent:$root.closest('.top-parent')})

  componentWillUpdate: (nextProps, nextState) ->
    $root = $(this.getDOMNode())
    $mute = $root.find('.track-icon-mute')
    $pan  = $root.find('.track-icon-pan')

    # disable hover effects if there is no mixer
    if nextProps.mixers.mixer?
      $mute.off("click", false)
      $pan.off("click", false)
    else
      $mute.on("click", false)
      $pan.on("click", false)
})
