<commit_before>kd                  = require 'kd'

JView               = require 'app/jview'
curryIn             = require 'app/util/curryIn'
showError           = require 'app/util/showError'

{yamlToJson}        = require './yamlutils'
StackEditorView     = require './stackeditorview'
updateStackTemplate = require './updatestacktemplate'
CredentialStatusView= require './credentialstatusview'


module.exports = class DefineStackView extends kd.View

  JView.mixin @prototype

  constructor: (options = {}, data) ->

    curryIn options, cssClass: 'step-define-stack'

    super options, data ? {}

    {credential, stackTemplate, template} = @getData()

    title   = stackTemplate?.title or 'Default stack template'
    content = stackTemplate?.template?.content

    @inputTitle            = new kd.FormViewWithFields fields:
      title                :
        cssClass           : 'template-title'
        label              : 'Stack Template Title'
        defaultValue       : title
        nextElement        :
          credentialStatus :
            cssClass       : 'credential-status'
            itemClass      : CredentialStatusView

    { @credentialStatus } = @inputTitle.inputs

    @editorView      = new StackEditorView {delegate: this, content}

    @cancelButton    = new kd.ButtonView
      title          : 'Cancel'
      cssClass       : 'solid compact light-gray nav cancel'
      callback       : => @emit 'Cancel'

    @saveButton      = new kd.ButtonView
      title          : 'Save & Test'
      cssClass       : 'solid compact green nav next'
      callback       : =>
        @saveTemplate (err, stackTemplate) =>
          return  if showError err
          @emit 'Completed', stackTemplate


  saveTemplate: (callback) ->

    {credential, stackTemplate} = @getData()

    {title}         = @inputTitle.getData()
    templateContent = @editorView.getValue()

    # TODO this needs to be filled in when we implement
    # Github flow for new stack editor
    templateDetails = null

    if 'yaml' is @editorView.getOption 'contentType'
      templateContent = (yamlToJson templateContent).content

    updateStackTemplate {
      template: templateContent, templateDetails
      credential, stackTemplate, title
    }, callback


  pistachio: ->
    """
      <div class='text header'>Create new Stack</div>
      {{> @inputTitle}}
      {{> @editorView}}
      {{> @cancelButton}}
      {{> @saveButton}}
    """<commit_msg>DefineStackView: use CredentialStatusView for both checking credential status and the credential data for active stack template<commit_after>kd                  = require 'kd'

JView               = require 'app/jview'
curryIn             = require 'app/util/curryIn'
showError           = require 'app/util/showError'

{yamlToJson}        = require './yamlutils'
StackEditorView     = require './stackeditorview'
updateStackTemplate = require './updatestacktemplate'
CredentialStatusView= require './credentialstatusview'


module.exports = class DefineStackView extends kd.View

  JView.mixin @prototype

  constructor: (options = {}, data) ->

    curryIn options, cssClass: 'step-define-stack'

    super options, data ? {}

    {credential, stackTemplate, template} = @getData()

    title   = stackTemplate?.title or 'Default stack template'
    content = stackTemplate?.template?.content

    @inputTitle            = new kd.FormViewWithFields fields:
      title                :
        cssClass           : 'template-title'
        label              : 'Stack Template Title'
        defaultValue       : title
        nextElement        :
          credentialStatus :
            cssClass       : 'credential-status'
            itemClass      : CredentialStatusView

    { @credentialStatus } = @inputTitle.inputs

    @editorView      = new StackEditorView {delegate: this, content}

    @cancelButton    = new kd.ButtonView
      title          : 'Cancel'
      cssClass       : 'solid compact light-gray nav cancel'
      callback       : => @emit 'Cancel'

    @saveButton      = new kd.ButtonView
      title          : 'Save & Test'
      cssClass       : 'solid compact green nav next'
      disabled       : yes
      callback       : =>
        @saveTemplate (err, stackTemplate) =>
          return  if showError err
          @emit 'Completed', stackTemplate

    @credentialStatus.on 'StatusChanged', (status) =>
      if status is 'verified'
      then @saveButton.enable()
      else @saveButton.disable()

    kd.utils.defer =>
      { credential } = @getData()
      @credentialStatus.setCredential credential


  saveTemplate: (callback) ->

    {stackTemplate} = @getData()

    {title}         = @inputTitle.getData()
    templateContent = @editorView.getValue()

    # TODO this needs to be filled in when we implement
    # Github flow for new stack editor
    templateDetails = null

    # TODO Make this to support multiple credentials
    credential      = @credentialStatus.credentials.first

    if 'yaml' is @editorView.getOption 'contentType'
      templateContent = (yamlToJson templateContent).content

    updateStackTemplate {
      template: templateContent, templateDetails
      credential, stackTemplate, title
    }, callback


  pistachio: ->
    """
      <div class='text header'>Create new Stack</div>
      {{> @inputTitle}}
      {{> @editorView}}
      {{> @cancelButton}}
      {{> @saveButton}}
    """
{'fraction_non_alphanumeric': 0.09533468559837728, 'fraction_numerical': 0.0, 'mean_word_length': 2.2753623188405796, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, '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': '4819328', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1412, 'n_words': 381}