module JamRuby
  class PerformanceSample < ActiveRecord::Base

    PERMISSION_MSG = "You do not have permission to perform this operation."

    attr_accessible :player_id, :service_type, :claimed_recording_id, :service_id, :url, :description

    belongs_to :player, polymorphic: true
    belongs_to :claimed_recording, :class_name => "JamRuby::ClaimedRecording", :foreign_key => "claimed_recording_id"

    validates :service_type, presence:true, length: {maximum: 100}

    # JamKazam validators
    validate :claimed_recording_id_present, :if => lambda { |p| p.service_type == "jamkazam" }
    validate :user_type_recording_unique, :if => lambda { |p| p.service_type == "jamkazam" }

    # Non-JamKazam validators
    validate :service_id_present, :if => lambda { |p| p.service_type != "jamkazam" }
    validate :user_type_service_unique, :if => lambda { |p| p.service_type != "jamkazam" }

    def claimed_recording_id_present
      raise StateError, "Claimed recording is required for JamKazam performance samples" if self.claimed_recording_id.blank?
    end

    def user_type_recording_unique
      match = PerformanceSample.exists?(:player_id => self.player_id, :claimed_recording_id => self.claimed_recording_id, :service_type => self.service_type)
      raise ConflictError, "You already have this JamKazam recording listed as a sample" if match
    end

    def service_id_present
      raise StateError, "Service ID is required for non-JamKazam performance samples" if self.service_id.blank?
    end

    def user_type_service_unique
      match = PerformanceSample.exists?(:player_id => self.player_id, :service_id => self.service_id, :service_type => self.service_type)
      raise ConflictError, "You already have this #{self.service_type} sample listed (#{self.service_id})." if match
    end

    def self.index(options = {})
      raise JamArgumentError, "The user is not specified." if options[:id].blank?
      PerformanceSample.where("player_id = ?", options[:id])
    end

    # Create with target_player (target_player is either user or band):
    def self.create(target_player, options = {}, save = true)
      auth_user(target_player, options)
      raise StateError, "Missing required information" if options[:service_type].blank?
      
      ps = PerformanceSample.new({
        :player_id => target_player.id, 
        :service_type => options[:service_type], 
        :claimed_recording_id => options[:claimed_recording_id],
        :service_id => options[:service_id],
        :url => options[:url],
        :description => options[:description]
      })

      ps.save! if save
      ps
    end

    def self.delete(target_player, options = {})
      raise JamPermissionError, "You do not have permission to perform this operation" if target_player.nil? || options[:player_id] != target_player.id
      raise StateError, "The performance sample ID is missing." if options[:id].blank?
      PerformanceSample.destroy(options[:id])
    end

    private
    def self.auth_user(target_player, options={})
      raise JamPermissionError, PERMISSION_MSG if target_player.nil? || options[:player_id] != target_player.id
    end
  end
end