<commit_before>Dialog = require 'zooniverse/controllers/dialog'
loginDialog = require 'zooniverse/controllers/login-dialog'
signupDialog = require 'zooniverse/controllers/signup-dialog'
Classifier = require './classifier'
MiniTutorial = require './mini-tutorial'
SubjectViewer = require './subject-viewer'
DecisionTree = require 'zooniverse-decision-tree'
ClassificationSummary = require './classification-summary'
DrawingTask = require './tasks/drawing'
FieldGuide = require './field-guide'
currentConfig = require 'zooniverse-readymade/current-configuration'
User = require 'zooniverse/models/user'
$ = window.jQuery
StackOfPages = require 'stack-of-pages'
Api = require 'zooniverse/lib/api'

class ClassifyPage extends Classifier
  START_TUTORIAL: "zooniverse-readymade:classifier:start_tutorial"

  targetSubjectID: ''

  workflow: 'untitled_workflow'
  tasks: null
  firstTask: ''

  tutorial: null
  tutorialSteps: null

  examples: null

  classificationsSubmitted: 0
  loginPromptEvery: 5 # Or use 0 to disable.

  className: "#{Classifier::className} readymade-classify-page"
  template: require './templates/classify-page'

  elements:
    '.readymade-no-more-subjects-message': 'noMoreSubjectsMessage'
    '.readymade-subject-viewer-container': 'subjectViewerContainer'
    '.readymade-decision-tree-container': 'decisionTreeContainer'
    '.readymade-summary-container': 'summaryContainer'
    '.readymade-field-guide-container': 'fieldGuideContainer'

  constructor: ->
    super

    if @tutorialSteps?
      @tutorial = new MiniTutorial steps: @tutorialSteps
      $(@tutorial.el).on @tutorial.CLOSE_EVENT, ->
        User.current?.setPreference 'tutorial_done', true

      @el.append @tutorial.el

    @subjectViewer = new SubjectViewer
    @subjectViewerContainer.append @subjectViewer.el

    @decisionTree = new DecisionTree
      taskTypes:
        radio: require './tasks/radio'
        checkbox: require './tasks/checkbox'
        button: require './tasks/button'
        filter: require './tasks/filter'
        drawing: DrawingTask
      tasks: @tasks
      firstTask: @firstTask || Object.keys(@tasks)[0]

    @listenTo @decisionTree.el, @decisionTree.LOAD_TASK, (e) =>
      @subjectViewer.setTool null, null
      @subjectViewer.setTaskIndex e.detail.index

    @listenTo @decisionTree.el, DrawingTask::SELECT_TOOL, (e) =>
      # Delay so the default is preserved instead of cleared with the LOAD_TASK event.
      setTimeout =>
        {tool, choice} = e.detail
        @subjectViewer.setTool tool, choice

    @listenTo @decisionTree.el, @decisionTree.COMPLETE, =>
      @finishSubject()

    @decisionTreeContainer.append @decisionTree.el

    @summary = new ClassificationSummary

    @summary.on @summary.DISMISS, =>
      @getNextSubject()

    @summaryContainer.append @summary.el

    if @examples?
      @fieldGuide = new FieldGuide {@examples}
      @fieldGuideContainer.append @fieldGuide.el

    @el.on StackOfPages::activateEvent, => @onActivate arguments...

  isUserScientist: ->
    result = new $.Deferred
    if User.current?
      # TODO: Cache some of this? Pretty nasty.
      project = Api.current.get "/projects/#{Api.current.project}"
      talkUser = Api.current.get "/projects/penguin/talk/users/#{User.current.name}"
      $.when(project, talkUser).then (project, talkUser) =>
        projectRoles = talkUser.talk?.roles?[project.id] ? []
        details =
          project: project.id
          roles: projectRoles
          scientist: 'scientist' in projectRoles
          admin: 'admin' in projectRoles
          'brian-c': talkUser.name is 'brian-c'
        console?.log 'Can you pick your own subject?', JSON.stringify details, null, 2
        result.resolve 'scientist' in projectRoles or 'admin' in projectRoles or talkUser.name is 'brian-c'
    else
      result.resolve false
    result.promise()

  onActivate: (e) ->
    @targetSubjectID = e.originalEvent.detail.subjectID
    if @classification?
      unless @targetSubjectID is @classification?.subject.zooniverse_id
        @getNextSubject()

  onUserChange: (user) ->
    super

    @classificationsSubmitted = 0

    if @tutorial?
      tutorialDone = user?.project?.tutorial_done
      tutorialDone ?= user?.preferences?[currentConfig.id]?.tutorial_done

      if tutorialDone
        if @tutorial.el.hasAttribute 'data-open'
          @tutorial.close()
      else
        @startTutorial()

  getNextSubject: ->
    if @targetSubjectID
      @isUserScientist().then (theyAre) =>
        if theyAre
          request = Api.current.get "/projects/#{Api.current.project}/subjects/#{@targetSubjectID}"
          request.then (data) =>
            subject = new @Subject data
            subject.select()
          request.fail =>
            alert "There's no subject with the ID #{@targetSubjectID}."
        else
          alert 'Sorry, only science team members can choose the subjects they classify.'
          super
    else
      super

  startTutorial: ->
    @tutorial.goTo 0
    @tutorial.open()
    @trigger @START_TUTORIAL, this, @tutorial

  onNoMoreSubjects: ->
    @noMoreSubjectsMessage.show()
    @subjectViewerContainer.hide()
    @decisionTreeContainer.hide()
    @summaryContainer.hide()

  createClassification: (subject) ->
    super
    if subject.zooniverse_id is @targetSubjectID
      @classification.set 'chosen_subject', true

  loadSubject: (subject, callback) ->
    args = arguments

    @noMoreSubjectsMessage.hide()
    @subjectViewerContainer.show()
    @decisionTreeContainer.show()
    @summaryContainer.hide()

    @subjectViewer.loadSubject subject, =>
      super args...

    @summary.loadSubject subject

  loadClassification: (classification, callback) ->
    args = arguments<commit_msg>Default value of targetSubjectID Set targetSubjectID to current subject ID for new classifications. Override it if a subjectID param is set in the URL.<commit_after>Dialog = require 'zooniverse/controllers/dialog'
loginDialog = require 'zooniverse/controllers/login-dialog'
signupDialog = require 'zooniverse/controllers/signup-dialog'
Classifier = require './classifier'
MiniTutorial = require './mini-tutorial'
SubjectViewer = require './subject-viewer'
DecisionTree = require 'zooniverse-decision-tree'
ClassificationSummary = require './classification-summary'
DrawingTask = require './tasks/drawing'
FieldGuide = require './field-guide'
currentConfig = require 'zooniverse-readymade/current-configuration'
User = require 'zooniverse/models/user'
$ = window.jQuery
StackOfPages = require 'stack-of-pages'
Api = require 'zooniverse/lib/api'

class ClassifyPage extends Classifier
  START_TUTORIAL: "zooniverse-readymade:classifier:start_tutorial"

  targetSubjectID: ''

  workflow: 'untitled_workflow'
  tasks: null
  firstTask: ''

  tutorial: null
  tutorialSteps: null

  examples: null

  classificationsSubmitted: 0
  loginPromptEvery: 5 # Or use 0 to disable.

  className: "#{Classifier::className} readymade-classify-page"
  template: require './templates/classify-page'

  elements:
    '.readymade-no-more-subjects-message': 'noMoreSubjectsMessage'
    '.readymade-subject-viewer-container': 'subjectViewerContainer'
    '.readymade-decision-tree-container': 'decisionTreeContainer'
    '.readymade-summary-container': 'summaryContainer'
    '.readymade-field-guide-container': 'fieldGuideContainer'

  constructor: ->
    super

    if @tutorialSteps?
      @tutorial = new MiniTutorial steps: @tutorialSteps
      $(@tutorial.el).on @tutorial.CLOSE_EVENT, ->
        User.current?.setPreference 'tutorial_done', true

      @el.append @tutorial.el

    @subjectViewer = new SubjectViewer
    @subjectViewerContainer.append @subjectViewer.el

    @decisionTree = new DecisionTree
      taskTypes:
        radio: require './tasks/radio'
        checkbox: require './tasks/checkbox'
        button: require './tasks/button'
        filter: require './tasks/filter'
        drawing: DrawingTask
      tasks: @tasks
      firstTask: @firstTask || Object.keys(@tasks)[0]

    @listenTo @decisionTree.el, @decisionTree.LOAD_TASK, (e) =>
      @subjectViewer.setTool null, null
      @subjectViewer.setTaskIndex e.detail.index

    @listenTo @decisionTree.el, DrawingTask::SELECT_TOOL, (e) =>
      # Delay so the default is preserved instead of cleared with the LOAD_TASK event.
      setTimeout =>
        {tool, choice} = e.detail
        @subjectViewer.setTool tool, choice

    @listenTo @decisionTree.el, @decisionTree.COMPLETE, =>
      @finishSubject()

    @decisionTreeContainer.append @decisionTree.el

    @summary = new ClassificationSummary

    @summary.on @summary.DISMISS, =>
      @getNextSubject()

    @summaryContainer.append @summary.el

    if @examples?
      @fieldGuide = new FieldGuide {@examples}
      @fieldGuideContainer.append @fieldGuide.el

    @el.on StackOfPages::activateEvent, => @onActivate arguments...

  isUserScientist: ->
    result = new $.Deferred
    if User.current?
      # TODO: Cache some of this? Pretty nasty.
      project = Api.current.get "/projects/#{Api.current.project}"
      talkUser = Api.current.get "/projects/penguin/talk/users/#{User.current.name}"
      $.when(project, talkUser).then (project, talkUser) =>
        projectRoles = talkUser.talk?.roles?[project.id] ? []
        details =
          project: project.id
          roles: projectRoles
          scientist: 'scientist' in projectRoles
          admin: 'admin' in projectRoles
          'brian-c': talkUser.name is 'brian-c'
        console?.log 'Can you pick your own subject?', JSON.stringify details, null, 2
        result.resolve 'scientist' in projectRoles or 'admin' in projectRoles or talkUser.name is 'brian-c'
    else
      result.resolve false
    result.promise()

  onActivate: (e) ->
    @targetSubjectID = e.originalEvent.detail.subjectID if e.originalEvent.detail.subjectID.length
    if @classification?
      unless @targetSubjectID is @classification?.subject.zooniverse_id
        @getNextSubject()

  onUserChange: (user) ->
    super

    @classificationsSubmitted = 0

    if @tutorial?
      tutorialDone = user?.project?.tutorial_done
      tutorialDone ?= user?.preferences?[currentConfig.id]?.tutorial_done

      if tutorialDone
        if @tutorial.el.hasAttribute 'data-open'
          @tutorial.close()
      else
        @startTutorial()

  getNextSubject: ->
    if @targetSubjectID
      @isUserScientist().then (theyAre) =>
        if theyAre
          request = Api.current.get "/projects/#{Api.current.project}/subjects/#{@targetSubjectID}"
          request.then (data) =>
            subject = new @Subject data
            subject.select()
          request.fail =>
            alert "There's no subject with the ID #{@targetSubjectID}."
        else
          alert 'Sorry, only science team members can choose the subjects they classify.'
          super
    else
      super

  startTutorial: ->
    @tutorial.goTo 0
    @tutorial.open()
    @trigger @START_TUTORIAL, this, @tutorial

  onNoMoreSubjects: ->
    @noMoreSubjectsMessage.show()
    @subjectViewerContainer.hide()
    @decisionTreeContainer.hide()
    @summaryContainer.hide()

  createClassification: (subject) ->
    super
    if subject.zooniverse_id is @targetSubjectID
      @classification.set 'chosen_subject', true
    
    @targetSubjectID = subject.zooniverse_id

  loadSubject: (subject, callback) ->
    args = arguments

    @noMoreSubjectsMessage.hide()
    @subjectViewerContainer.show()
    @decisionTreeContainer.show()
    @summaryContainer.hide()

    @subjectViewer.loadSubject subject, =>
      super args...

    @summary.loadSubject subject

  loadClassification: (classification, callback) ->
    args = arguments
{'fraction_non_alphanumeric': 0.10972802455452298, 'fraction_numerical': 0.0011936226447267456, 'mean_word_length': 3.2423146473779387, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1970319', 'n_tokens_mistral': 3576, 'n_tokens_neox': 3386, 'n_words': 774}