import DS from 'ember-data'
import { modelAction } from 'ember-custom-actions'
export default DS.Model.extend
  state:            DS.attr('string', defaultValue: 'pending') # pending, planned, (active), overdue, rejected, completed
  pending_at:       DS.attr('moment')
  planned_at:       DS.attr('moment')
  overdue_at:       DS.attr('moment')
  inquiry_at:       DS.attr('moment')
  rejected_at:      DS.attr('moment')
  completed_at:     DS.attr('moment')
  target_date:      DS.attr('moment')
  target_date_lock: DS.attr('boolean', defaultValue: false)

  string_value:     DS.attr('string', defaultValue: '')
  text_value:       DS.attr('string', defaultValue: '')
  date_value:       DS.attr('moment')
  timestamp_value:  DS.attr('moment')
  number_value:     DS.attr('number')
  boolean_value:    DS.attr('boolean', defaultValue: false)
  #date,           type:    Date
  notes:            DS.attr('string')

  completed: Ember.computed.equal 'state', 'completed'

  triggerEvent: modelAction('trigger_event', pushToStore: true)

  value_display: Ember.computed 'workflow_action_definition.value_type', ->
    return '' unless value_type = @get('workflow_action_definition.value_type')
    switch value_type
      when 'string', 'select'
        @get('string_value')
      when 'number'
        @get('number_value')
      when 'text'
        @get('text_value')
      when 'date'
        @get('date_value')?.format('LL')
      when 'timestamp'
        @get('timestamp_value')?.format('LLL')
      when 'boolean'
        cls = if @get('boolean_value') then 'check square outline' else 'square outline'
        "<i class='#{cls} icon'></i>".htmlSafe()

  string_comparison_value: Ember.computed 'workflow_action_definition.value_type', '{string_value,number_value,text_value,date_value,timestamp_value,boolean_value}', ->
    return '' unless value_type = @get('workflow_action_definition.value_type')
    switch value_type
      when 'string', 'select'
        @get('string_value')
      when 'number'
        if number = @get('number_value') then String(number) else ''
      when 'text'
        @get('text_value')
      when 'date'
        if date = @get('date_value') then date.format('YYYY-MM-DD') else ''
      when 'timestamp'
        if timestamp = @get('timestamp_value') then timestamp.format('YYYY-MM-DDThh:mm:ss') else ''
      when 'boolean'
        String(@get('boolean_value'))

  project:                    DS.belongsTo('panda/project',                    inverse: 'project_instance_actions')
  workflow_action_definition: DS.belongsTo('panda/workflow-action-definition', inverse: 'project_instance_actions')
  project_instance_group:     DS.belongsTo('panda/project-instance-group',     inverse: 'project_instance_actions')
  project_instance:           DS.belongsTo('panda/project-instance',           inverse: 'project_instance_actions')

  #project_instance_action_connections: DS.hasMany('panda/project-instance-action-connection') # use for workflow, not actual instance handling
  pointing_project_instance_action_connections: DS.hasMany('panda/project-instance-action-connection', inverse: 'base_project_instance_action')

  events:                     DS.hasMany('panda/action-event', inverse: 'project_instance_action')
