module JamRuby
  class Instrument < ActiveRecord::Base

    MAP_ICON_NAME = {
        "accordion" => "accordion",
        "acoustic guitar" => "acoustic_guitar",
        "banjo" => "banjo",
        "bass guitar" => "bass_guitar",
        "cello" => "cello",
        "clarinet" => "clarinet",
        "computer" => "computer",
        "default" => "default",
        "drums" => "drums",
        "electric guitar" => "electric_guitar",
        "euphonium" => "euphonium",
        "flute" => "flute",
        "french horn" => "french_horn",
        "harmonica" => "harmonica",
        "keyboard" => "keyboard",
        "mandolin" => "mandolin",
        "oboe" => "oboe",
        "other" => "other",
        "piano" => "piano",
        "saxophone" => "saxophone",
        "trombone" => "trombone",
        "trumpet" => "trumpet",
        "tuba" => "tuba",
        "ukulele" => "ukelele",
        "upright bass" => "upright_bass",
        "double bass" => "double_bass",
        "viola" => "viola",
        "violin" => "violin",
        "voice" => "voice"
    }

    self.primary_key = 'id'

    # users
    has_many :musician_instruments, :class_name => "JamRuby::MusicianInstrument"
    has_many :players, :through => :musician_instruments
    has_many :tracks, :class_name => "JamRuby::Track", :inverse_of => :instrument
    has_many :recorded_tracks, :class_name => "JamRuby::RecordedTrack", :inverse_of => :instrument

    # music sessions
    has_and_belongs_to_many :music_sessions, :class_name => "JamRuby::ActiveMusicSession", :join_table => "genres_music_sessions"

    # teachers
    has_many :teachers, :class_name => "JamRuby::Teacher", through: :teachers_instruments
    has_many :teachers_instruments, class_name: "JamRuby::TeacherInstrument"

    def self.standard_list
      return Instrument.where('instruments.popularity > 0').order('instruments.description ASC')
    end

    def self.jam_track_list
      sql = "SELECT DISTINCT instrument_id FROM jam_track_tracks WHERE instrument_id IS NOT NULL"
      Instrument.where("instruments.id IN (#{sql})")
        .order('instruments.description ASC')
    end

    def icon_name
      MAP_ICON_NAME[self.id]
    end

    def to_s
      description
    end

  end
end
