# -*- coding: utf-8 -*-
module JamRuby
  class JamTrackSession < ActiveRecord::Base

    BROWSER = 'browser'
    SESSION = 'session'
    TYPES = [BROWSER, SESSION]

    @@log = Logging.logger[JamTrackSession]

    belongs_to :music_session, class_name: 'JamRuby::MusicSession'
    belongs_to :user, class_name: 'JamRuby::User'
    belongs_to :jam_track, class_name: 'JamRuby::JamTrack'

    def self.create_session(jam_track, user, music_session)
      create(jam_track, user, SESSION, music_session)
    end

    def self.create_browser(jam_track, user)
      create(jam_track, user, BROWSER, nil)
    end

    private
    def self.create(jam_track, user, type, music_session)
      jam_track_session = JamTrackSession.new
      jam_track_session.jam_track = jam_track
      jam_track_session.user = user
      jam_track_session.session_type = type
      jam_track_session.music_session = music_session
      jam_track_session.save
      jam_track_session
    end
  end
end