import Ember from 'ember'
export default Ember.Component.extend
  classNames: ["workflow-action-definition-item", 'workflow-action-container']
  classNameBindings: ["workflow_action_definition.done:done", "isEditing:is-editing:is-display", "workflow_action_definition.completes_group:completes-group"]
  selectedValueType: Ember.computed.oneWay "workflow_action_definition.value_type"
  isEditing: false

  make_editable: (->
    @set 'isEditing', true
  ).on "doubleClick"

  didRender: ->
    @_super arguments...
    #@set 'isEditing', true if @get('workflow_action_definition.isNew')

  actions:
    saveWorkflowActionDefinition: ->
      record = @get("workflow_action_definition")
      first_save = record.get('isNew')
      record.setProperties
        value_type: @get("selectedValueType")
      record.save().then =>
        @set 'isEditing', false
        if first_save
          @send('editWorkflowActionDefinition', record)
        else
          # fist save has no graph impact, so only overhere
          @incrementProperty 'model.project.update_revision'

    destroyWorkflowActionDefinition: (workflow_action_definition)->
      @modal "confirm",
        title_path: "general.destroy.are_you_sure"
        model: @get("workflow_action_definition")
        ok: ->
          @set 'isEditing', false
          workflow_action_definition = @get('model')
          if workflow_action_definition.get('isNew')
            workflow_action_definition.deleteRecord()
          else
            workflow_action_definition.rollbackAttributes()
            workflow_action_definition.destroyRecord()
          @incrementProperty 'model.project.update_revision'
    rollbackWorkflowActionDefinition: ->
      @set 'isEditing', false
      @get('workflow_action_definition').rollbackAttributes()

    positionInProject: ->
      @modal 'project/position-workflow-action-definition-in-project',
        model: @get('workflow_action_definition')
      @set 'isEditing', false

    authorize_users: ->
      @modal 'workflow-action-definition/authorize-users',
        title_path: 'workflow_action_definition.authorize_users.title'
        model: @get('workflow_action_definition')

    editWorkflowActionDefinition: ->
      @modal 'workflow-action-definition/edit-workflow-action-definition',
        model: @get('workflow_action_definition')
      @set 'isEditing', false # usability

  #select_on_edit: Ember.computed "workflow_action_definition.isEditing", ->
  #  @$('.workflow-action-definition-title-edit').select()

  updateWorkflowActionDefinitionIfNeeded: (->
    workflow_action_definition = @get('workflow_action_definition')
    if workflow_action_definition.changedAttributes().done
      workflow_action_definition.save()
  ).observes('workflow_action_definition.done')
