'use strict';
import React, { PropTypes as T } from 'react';
import c from 'classnames';
import ReactTooltip from 'react-tooltip';

import Dropdown from '../dropdown';
import { t } from '../../utils/i18n';

import ScenarioDeleteAction from './scenario-delete-action';

const ScenarioHeaderActions = React.createClass({

  propTypes: {
    onAction: T.func,
    scenario: T.object
  },

  onGenerateClick: function (isActive, e) {
    isActive && this.props.onAction('generate', e);
  },

  onEditClick: function (isActive, e) {
    isActive && this.props.onAction('edit-network', e);
  },

  renderDisableReatonTip: function (isGenerating, isPending) {
    let disable = !isGenerating && !isPending;
    let txt;

    if (isGenerating) {
      txt = t('Generation is in progress.');
    } else if (isPending) {
      txt = t('Scenario still being created.');
    }

    return (
      <ReactTooltip id='tip-disable-reason' effect='solid' disable={disable}>
        {txt}
      </ReactTooltip>
    );
  },

  render: function () {
    let isGenerating = this.props.scenario.gen_analysis && this.props.scenario.gen_analysis.status === 'running';
    let isMaster = this.props.scenario.master;
    let isPending = this.props.scenario.status === 'pending';

    return (
      <div className='inpage__actions'>
        <Dropdown
          onChange={(open) => open ? ReactTooltip.rebuild() : ReactTooltip.hide()}
          className='scenario-meta-menu'
          triggerClassName='ipa-ellipsis'
          triggerActiveClassName='button--active'
          triggerText='Action'
          triggerTitle='Action'
          direction='down'
          alignment='center' >
            <ul className='drop__menu drop__menu--iconified' role='menu'>
              <li><a href='#' title={t('Edit metadata')} className='drop__menu-item dmi-pencil' data-hook='dropdown:close' onClick={this.props.onAction.bind(null, 'edit-metadata')}>{t('Edit metadata')}</a></li>
              <li><a href='#' title={t('Duplicate scenario')} className='drop__menu-item dmi-copy' data-hook='dropdown:close' onClick={this.props.onAction.bind(null, 'duplicate')}>{t('Duplicate scenario')}</a></li>
            </ul>
            <ul className='drop__menu drop__menu--iconified' role='menu'>
              <li><ScenarioDeleteAction isMaster={isMaster} name={this.props.scenario.name} onDeleteConfirm={this.props.onAction.bind(null, 'delete')}/></li>
            </ul>
        </Dropdown>
        <button data-tip data-for='tip-disable-reason' title={t('Edit network')} className={c('ipa-pencil', {'visually-disabled': isGenerating || isPending})} type='button' onClick={this.onEditClick.bind(null, !isGenerating)}><span>{t('Edit')}</span></button>
        <button data-tip data-for='tip-soon' title={t('Download results')} className='ipa-download visually-disabled' type='button' onClick={this.props.onAction.bind(null, 'download-results')}><span>{t('Download')}</span></button>

        <button data-tip data-for='tip-disable-reason' title={t('Generate results')} className={c('ipa-arrow-loop', {'visually-disabled': isGenerating || isPending})} type='button' onClick={this.onGenerateClick.bind(null, !isGenerating)}><span>{t('Generate')}</span></button>

        {this.renderDisableReatonTip(isGenerating, isPending)}

        <ReactTooltip id='tip-soon' effect='solid'>
          {t('Coming soon...')}
        </ReactTooltip>

      </div>
    );
  }
});

export default ScenarioHeaderActions;

@@ -121,24 +121,59 @@
 -tooltip';%0A%0A
+import config from '../../config';%0A
 import Dropd
@@ -632,25 +632,25 @@
 erDisableRea
-t
+s
 onTip: funct
@@ -1282,16 +1282,236 @@
 ding';%0A%0A
+    let hasResults = this.props.scenario.files.some(f =%3E f.type === 'results');%0A    let resultsUrl = %60$%7Bconfig.api%7D/projects/$%7Bthis.props.scenario.project_id%7D/scenarios/$%7Bthis.props.scenario.id%7D/results?download=true%60;%0A%0A
     retu
@@ -2959,38 +2959,51 @@
 utton%3E%0A        %3C
-button
+a href=%7BresultsUrl%7D
  data-tip data-f
@@ -3010,20 +3010,26 @@
 or='tip-
-soon
+no-results
 ' title=
@@ -3058,24 +3058,27 @@
 %7D className=
+%7Bc(
 'ipa-downloa
@@ -3078,17 +3078,21 @@
 download
- 
+', %7B'
 visually
@@ -3105,81 +3105,73 @@
 led'
- type='button' onClick=%7Bthis.props.onAction.bind(null, 'download-results'
+: !hasResults%7D)%7D onClick=%7B(e) =%3E !hasResults && e.preventDefault(
 )%7D%3E%3C
@@ -3191,38 +3191,33 @@
 load')%7D%3C/span%3E%3C/
-button
+a
 %3E%0A%0A        %3Cbutt
@@ -3510,17 +3510,17 @@
 sableRea
-t
+s
 onTip(is
@@ -3574,20 +3574,26 @@
 id='tip-
-soon
+no-results
 ' effect
@@ -3604,38 +3604,74 @@
 lid'
-%3E%0A          %7Bt('Coming soon...
+ disable=%7BhasResults%7D%3E%0A          %7Bt('No results were generated yet
 ')%7D%0A

Add result download. Fix #73