<commit_before>React = require 'react'
_ = require 'underscore'

{TaskStepActions, TaskStepStore} = require '../../flux/task-step'
{TaskProgressActions, TaskProgressStore} = require '../../flux/task-progress'
{TaskStore} = require '../../flux/task'
{StepPanel} = require '../../helpers/policies'

BrowseTheBook = require '../buttons/browse-the-book'
RelatedContentLink = require '../related-content-link'
{ChapterSectionMixin, CardBody, ExerciseWithScroll, ExControlButtons} = require 'shared'

StepFooter = require './step-footer'

canOnlyContinue = (id) ->
  _.chain(StepPanel.getRemainingActions(id))
    .difference(['clickContinue'])
    .isEmpty()
    .value()

getWaitingText = (id) ->
  switch
    when TaskStepStore.isSaving(id)  then 'Saving…'
    when TaskStepStore.isLoading(id) then 'Loading…'
    else null

getReadingForStep = (id, taskId) ->
  TaskStore.getReadingForTaskId(taskId, id)

getCurrentPanel = (id) ->
  unless TaskStepStore.isSaving(id)
    currentPanel = StepPanel.getPanel(id)

module.exports = React.createClass
  displayName: 'ExerciseShell'
  propTypes:
    id: React.PropTypes.string.isRequired
    taskId: React.PropTypes.string.isRequired

  mixins: [ ChapterSectionMixin ]

  contextTypes:
    router: React.PropTypes.func

  getInitialState: ->
    partsInfo = @getPartsInfo()
    taskInfo = @getTaskInfo()

    _.extend({}, partsInfo, taskInfo)

  getPartsInfo: (props) ->
    props ?= @props

    {id, taskId, courseId, onNextStep} = props
    parts = TaskStore.getStepParts(taskId, id)

    lastPartId = _.last(parts).id
    isSinglePartExercise = @isSinglePart(parts)

    {parts, lastPartId, isSinglePartExercise}

  getTaskInfo: (props) ->
    props ?= @props
    {taskId} = props

    task = TaskStore.get(taskId)
    currentStep = TaskProgressStore.get(taskId)

    {task, currentStep}

  isSinglePart: (parts) ->
    parts.length is 1

  componentWillMount: ->
    @startListeningForProgress()

  componentWillUnmount: ->
    @stopListeningForProgress()

  componentWillReceiveProps: (nextProps) ->
    if nextProps.taskId isnt @props.taskId
      nextState = _.extend({}, @getTaskInfo(nextProps), @getPartsInfo(nextProps))
      @setState(nextState)
      @stopListeningForProgress()
      @startListeningForProgress(nextProps)
    else
      @setState(@getPartsInfo(nextProps))

  # renderHelpLink: (sections) ->
  #   return null unless sections? and not _.isEmpty(sections)
  #   {courseId} = @props

  #   sectionsLinks = _.chain sections
  #     .map (section) =>
  #       combined = @sectionFormat(section.chapter_section)
  #       <BrowseTheBook
  #         unstyled
  #         key={combined}
  #         section={combined}
  #         courseId={courseId}
  #         onlyShowBrowsable={false}>
  #         {combined} {section.title} <i className='fa fa-external-link' />
  #       </BrowseTheBook>
  #     .compact()
  #     .value()

  #   helpLink =
  #     <div key='task-help-links' className='task-help-links'>
  #       Comes from&nbsp&nbsp{sectionsLinks}
  #     </div>

  #   if sectionsLinks.length > 0 then helpLink

  stopListeningForProgress: (props) ->
    props ?= @props
    {taskId} = props

    TaskProgressStore.off("update.#{taskId}", @setCurrentStepFromProgress)

  startListeningForProgress: (props) ->
    props ?= @props
    {taskId} = props

    TaskProgressStore.on("update.#{taskId}", @setCurrentStepFromProgress)

  canAllContinue: ->
    {parts} = @state

    _.every parts, (part) ->
      canOnlyContinue(part.id)

  allCorrect: ->
    {parts} = @state

    _.every parts, (part) ->
      (part.correct_answer_id is part.answer_id)

  getReviewProps: ->
    {refreshStep, recoverFor} = @props
    {task, lastPartId} = @state<commit_msg>remove commented out block<commit_after>React = require 'react'
_ = require 'underscore'

{TaskStepActions, TaskStepStore} = require '../../flux/task-step'
{TaskProgressActions, TaskProgressStore} = require '../../flux/task-progress'
{TaskStore} = require '../../flux/task'
{StepPanel} = require '../../helpers/policies'

BrowseTheBook = require '../buttons/browse-the-book'
RelatedContentLink = require '../related-content-link'
{ChapterSectionMixin, CardBody, ExerciseWithScroll, ExControlButtons} = require 'shared'

StepFooter = require './step-footer'

canOnlyContinue = (id) ->
  _.chain(StepPanel.getRemainingActions(id))
    .difference(['clickContinue'])
    .isEmpty()
    .value()

getWaitingText = (id) ->
  switch
    when TaskStepStore.isSaving(id)  then 'Saving…'
    when TaskStepStore.isLoading(id) then 'Loading…'
    else null

getReadingForStep = (id, taskId) ->
  TaskStore.getReadingForTaskId(taskId, id)

getCurrentPanel = (id) ->
  unless TaskStepStore.isSaving(id)
    currentPanel = StepPanel.getPanel(id)

module.exports = React.createClass
  displayName: 'ExerciseShell'
  propTypes:
    id: React.PropTypes.string.isRequired
    taskId: React.PropTypes.string.isRequired

  mixins: [ ChapterSectionMixin ]

  contextTypes:
    router: React.PropTypes.func

  getInitialState: ->
    partsInfo = @getPartsInfo()
    taskInfo = @getTaskInfo()

    _.extend({}, partsInfo, taskInfo)

  getPartsInfo: (props) ->
    props ?= @props

    {id, taskId, courseId, onNextStep} = props
    parts = TaskStore.getStepParts(taskId, id)

    lastPartId = _.last(parts).id
    isSinglePartExercise = @isSinglePart(parts)

    {parts, lastPartId, isSinglePartExercise}

  getTaskInfo: (props) ->
    props ?= @props
    {taskId} = props

    task = TaskStore.get(taskId)
    currentStep = TaskProgressStore.get(taskId)

    {task, currentStep}

  isSinglePart: (parts) ->
    parts.length is 1

  componentWillMount: ->
    @startListeningForProgress()

  componentWillUnmount: ->
    @stopListeningForProgress()

  componentWillReceiveProps: (nextProps) ->
    if nextProps.taskId isnt @props.taskId
      nextState = _.extend({}, @getTaskInfo(nextProps), @getPartsInfo(nextProps))
      @setState(nextState)
      @stopListeningForProgress()
      @startListeningForProgress(nextProps)
    else
      @setState(@getPartsInfo(nextProps))

  stopListeningForProgress: (props) ->
    props ?= @props
    {taskId} = props

    TaskProgressStore.off("update.#{taskId}", @setCurrentStepFromProgress)

  startListeningForProgress: (props) ->
    props ?= @props
    {taskId} = props

    TaskProgressStore.on("update.#{taskId}", @setCurrentStepFromProgress)

  canAllContinue: ->
    {parts} = @state

    _.every parts, (part) ->
      canOnlyContinue(part.id)

  allCorrect: ->
    {parts} = @state

    _.every parts, (part) ->
      (part.correct_answer_id is part.answer_id)

  getReviewProps: ->
    {refreshStep, recoverFor} = @props
    {task, lastPartId} = @state
{'fraction_non_alphanumeric': 0.13303504847129008, 'fraction_numerical': 0.00044742729306487697, 'mean_word_length': 3.5464406779661015, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 47, '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': '3008073', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2142, 'n_words': 406}