<commit_before>kd             = require 'kd'
JView          = require 'app/jview'

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

CredentialSelectorModal = require './credentialselectormodal'

module.exports = class CredentialStatusView extends kd.View

  JView.mixin @prototype

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

    curryIn options, cssClass: 'credential-status'

    super options, data

    @credentials = []

    # Waiting state view
    @waitingView = new kd.View

    @waitingView.addSubView @loader  = new kd.LoaderView
      showLoader : yes
      size       : width : 16
    @waitingView.addSubView @message = new kd.CustomHTMLView
      cssClass   : 'message'
      partial    : 'Checking credentials...'

    # Stalled state view
    @stalledView = new kd.View
      cssClass   : 'hidden'

    @stalledView.addSubView @icon = new kd.CustomHTMLView
      cssClass   : 'icon not verified'

    @stalledView.addSubView @link = new kd.CustomHTMLView
      cssClass   : 'link'
      partial    : 'Credentials are not set'
      click      : =>

        modal = new CredentialSelectorModal {
          selectedCredentials: @getPublicKeys()
        }

        modal.on 'ItemSelected', (credential) =>
          modal.destroy()

          @setCredential credential


  setCredential: (credential) ->

    return @setNotVerified()  unless credential

    @credentials      = [credential]
    {provider, title} = credential
    @setVerified "
      A credential titled as '#{title}' for #{provider} provider is selected.
    "


  setVerified: (message) ->

    @waitingView.hide()

    @setInfo message
    @link.updatePartial 'Credentials are set'
    @icon.setClass 'verified'
    @emit 'StatusChanged', 'verified'

    @stalledView.show()


  setNotVerified: (message) ->

    @waitingView.hide()

    @setInfo()
    @link.updatePartial 'Credentials are not set'
    @icon.unsetClass 'verified'
    @emit 'StatusChanged', 'not-verified'

    @stalledView.show()


  setInfo: (message) ->

    unless message
      return @link.unsetTooltip()

    @link.setTooltip
      title     : message
      placement : 'below'


  getPublicKeys: ->
    (cred.publicKey for cred in @credentials)


  pistachio: ->
    """
      {{> @waitingView}}
      {{> @stalledView}}
    """<commit_msg>CredentialStatusView: check credential status inline, fetch credential if provided so we can show the short information about it<commit_after>kd             = require 'kd'
JView          = require 'app/jview'
remote         = require('app/remote').getInstance()

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

CredentialSelectorModal = require './credentialselectormodal'

module.exports = class CredentialStatusView extends kd.View

  JView.mixin @prototype

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

    curryIn options, cssClass: 'credential-status'

    super options, data

    { @credentials } = (@getOption 'stackTemplate') or {}
    @credentials   or= []

    # Waiting state view
    @waitingView = new kd.View

    @waitingView.addSubView @loader  = new kd.LoaderView
      showLoader : yes
      size       : width : 16
    @waitingView.addSubView @message = new kd.CustomHTMLView
      cssClass   : 'message'
      partial    : 'Checking credentials...'

    # Stalled state view
    @stalledView = new kd.View
      cssClass   : 'hidden'

    @stalledView.addSubView @icon = new kd.CustomHTMLView
      cssClass   : 'icon not verified'

    @stalledView.addSubView @link = new kd.CustomHTMLView
      cssClass   : 'link'
      partial    : 'Credentials are not set'
      click      : =>

        modal = new CredentialSelectorModal {
          selectedCredentials: @credentials
        }

        modal.on 'ItemSelected', (credential) =>
          modal.destroy()

          @setCredential credential


    if @credentials.length > 0
      [credential] = @credentials

      remote.api.JCredential.one credential, (err, credential) =>
        if err
        then @setNotVerified 'Credentials not valid'
        else @setCredential credential
    else
      @setNotVerified()


  setCredential: (credential) ->

    return @setNotVerified()  unless credential

    @credentials      = [credential.publicKey]
    {provider, title} = credential
    @setVerified "
      A credential titled as '#{title}' for #{provider} provider is selected.
    "


  setVerified: (message) ->

    @waitingView.hide()

    @setInfo message
    @link.updatePartial 'Credentials are set'
    @icon.setClass 'verified'
    @emit 'StatusChanged', 'verified'

    @stalledView.show()


  setNotVerified: (message) ->

    @waitingView.hide()

    @setInfo()
    @link.updatePartial message or 'Credentials are not set'
    @icon.unsetClass 'verified'
    @emit 'StatusChanged', 'not-verified'

    @stalledView.show()


  setInfo: (message) ->

    unless message
      return @link.unsetTooltip()

    @link.setTooltip
      title     : message
      placement : 'below'


  pistachio: ->
    """
      {{> @waitingView}}
      {{> @stalledView}}
    """
{'fraction_non_alphanumeric': 0.10599258247120828, 'fraction_numerical': 0.0009759906304899473, 'mean_word_length': 2.5682451253481893, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 26, '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': '4227897', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1530, 'n_words': 377}