class AttributeField
  
  include Mongoid::Document
  include Mongoid::Timestamps
  include AttributeFieldHelper

  field :key
  field :markup #[select,text_field,email,date,addr]
  field :markup_value ,:type => Hash
  field :locale, :type => Boolean, :default => true
  field :list_options, :type => Array
  field :built_in, :type => Boolean, :default => false
  field :disabled, :type => Boolean, :default => false
  #field :title, localize: true

  field :locale_title, localize: true
  field :neutral_title

  belongs_to :attribute
  # belongs_to :role
  has_many :attribute_values
  
  # validates_uniqueness_of :key
  def role
    self.attribute.role
  end

  def title_translations
    if locale
      return locale_title_translations
    else
      return Hash[VALID_LOCALES.map{|d| [d,neutral_title]}]
    end
  end

  def title_translations=(var)
     if locale
      self.locale_title_translations = var
    end
  end

  def title
    if locale
      return self.locale_title
    else
      return self.neutral_title
    end
  end
  
  def title=(var)
    # binding.pry
     if locale 
      self.locale_title = var
    else
      self.neutral_title = var
    end
  end

  # Convert the string list_options into an array
  def select_list_options=(var)
    self.list_options = var.gsub(' ', '').split(',')
  end
  
  # Convert the array list_options into a string
  def select_list_options
    self.list_options.to_a.join(', ')
  end
  
  def is_built_in?
    self.built_in
  end
  
  def is_disabled?
    self.disabled
  end

end
