# == Schema Information
#
# Table name: topics
#
#  id          :integer          not null, primary key
#  type        :string(255)
#  title       :string(255)
#  uuid        :integer
#  url         :string(255)
#  order_index :integer
#  location    :integer          default("0")
#

class Topic < ApplicationRecord

  enum location: { home: 0, competition: 1 }

  default_scope { order(order_index: :desc)}

  scope :with_single_type, ->(type){where(type: trans_simpletype_to_classtype(type))}

  def image
    image_url('image')
  end

  def self.trans_simpletype_to_classtype(type)
    case type 
    when 'activity_forum'
      'Topic::ActivityForum'
    when 'banner'
      'Topic::Banner'
    when 'card'
      'Topic::Card'
    when 'cooperator'
      'Topic::Cooperator'
    when 'excellent_project'
      'Topic::ExcellentProject'
    when 'experience_forum'
      'Topic::ExperienceForum'
    when "glcc_news"
      'Topic::GlccNews'
    when 'pinned_forum'
      'Topic::PinnedForum'
    when 'applet_banner'
      'Topic::AppletBanner'
    end
  end

  private

  def image_url(type)
    return nil unless Util::FileManage.exists?(self, type)
    Util::FileManage.source_disk_file_url(self, type)
  end

end
