FactoryGirl.define do
  factory :user, :class => JamRuby::User do
    sequence(:email) { |n| "person_#{n}@example.com"}
    sequence(:first_name)  { |n| "Person" }
    sequence(:last_name)  { |n| "#{n}" }
    password "foobar"
    password_confirmation "foobar"
    email_confirmed true
    musician true
    city "Apex"
    state "NC"
    country "US"
    terms_of_service true
    reuse_card true


    factory :admin do
      admin true
    end

    before(:create) do |user|
      user.musician_instruments << FactoryGirl.build(:musician_instrument, player: user)
    end
  end


  factory :active_music_session_no_user_history, :class => JamRuby::ActiveMusicSession do

    association :creator, factory: :user

    transient do
      name "My Music Session"
      description "Come Music Session"
      fan_chat true
      fan_access true
      approval_required false
      musician_access true
      legal_terms true
      genre JamRuby::Genre.first
      band nil
    end


    before(:create) do |session, evaluator|
      music_session = FactoryGirl.create(:music_session, name: evaluator.name, description: evaluator.description, fan_chat: evaluator.fan_chat,
                                         fan_access: evaluator.fan_access, approval_required: evaluator.approval_required, musician_access: evaluator.musician_access,
                                         genre: evaluator.genre, creator: evaluator.creator, band: evaluator.band)
      session.id = music_session.id
    end

    factory :active_music_session do
      after(:create) { |session|
        FactoryGirl.create(:music_session_user_history, :history => session.music_session, :user => session.creator)
      }
    end
  end


  factory :music_session, :class => JamRuby::MusicSession do
    sequence(:name) { |n| "Music Session #{n}" }
    sequence(:description) { |n| "Music Session Description #{n}" }
    fan_chat true
    fan_access true
    approval_required false
    musician_access true
    legal_terms true
    language 'eng'
    legal_policy 'standard'
    genre JamRuby::Genre.first
    timezone 'UTC,Etc/UTC'
    open_rsvps true
    scheduled_start Time.now
    scheduled_duration 3600
    association :creator, :factory => :user
  end

  factory :music_session_user_history, :class => JamRuby::MusicSessionUserHistory do
    ignore do
      history nil
      user nil
    end

    instruments 'guitar'
    music_session_id { history.id }
    user_id { user.id }
    sequence(:client_id) { |n| "Connection #{n}" }
  end

  factory :connection, :class => JamRuby::Connection do
    addr 0
    locidispid 0
    ip_address '1.1.1.1'
    as_musician true
    client_type 'client'
    gateway 'gateway1'
    scoring_timeout Time.now
    sequence(:channel_id) { |n| "Channel#{n}"}
  end

  factory :instrument, :class => JamRuby::Instrument do
    description { |n| "Instrument #{n}" }
  end

  factory :musician_instrument, :class=> JamRuby::MusicianInstrument do
    instrument { Instrument.find('electric guitar') }
    proficiency_level 1
    priority 0
  end

  factory :latency_tester, :class => JamRuby::LatencyTester do
    ignore do
      connection nil
      make_connection  true
    end

    sequence(:client_id) { |n| "LatencyTesterClientId-#{n}" }

    after(:create) do |latency_tester, evaluator|
      latency_tester.connection = evaluator.connection if evaluator.connection
      latency_tester.connection = FactoryGirl.create(:connection, client_type: Connection::TYPE_LATENCY_TESTER, client_id: latency_tester.client_id) if evaluator.make_connection
      latency_tester.save
    end
  end

  factory :jam_track_licensor, :class => JamRuby::JamTrackLicensor do
    sequence(:name) { |n| "licensor-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    sequence(:attention) { |n| "attention-#{n}" }
    sequence(:address_line_1) { |n| "address1-#{n}" }
    sequence(:address_line_2) { |n| "address2-#{n}" }
    sequence(:city) { |n| "city-#{n}" }
    sequence(:state) { |n| "state-#{n}" }
    sequence(:zip_code) { |n| "zipcode-#{n}" }
    sequence(:contact) { |n| "contact-#{n}" }
    sequence(:email) { |n| "email-#{n}" }
    sequence(:phone) { |n| "phone-#{n}" }
  end

  factory :jam_track, :class => JamRuby::JamTrack do
    sequence(:name) { |n| "jam-track-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    bpm               100.1
    time_signature    '4/4'
    status            'Production'
    recording_type    'Cover'
    sequence(:original_artist) { |n| "original-artist-#{n}" }
    sequence(:songwriter) { |n| "songwriter-#{n}" }
    sequence(:publisher) { |n| "publisher-#{n}" }
    pro               'ASCAP'
    sales_region      'United States'
    price             1.99
    reproduction_royalty  true
    public_performance_royalty true
    reproduction_royalty_amount 0.999
    licensor_royalty_amount 0.999
    pro_royalty_amount  0.999

    genre             JamRuby::Genre.first
    association       :licensor, factory: :jam_track_licensor
  end

  factory :jam_track_track, :class => JamRuby::JamTrackTrack do
    position          1
    part              'lead guitar'
    track_type        'Track'
    instrument        JamRuby::Instrument.find('electric guitar')
    association       :jam_track, factory: :jam_track
  end

  factory :jam_track_right, :class => JamRuby::JamTrackRight do
    association       :jam_track, factory: :jam_track
    association       :user,      factory: :user
  end

  factory :notification, :class => JamRuby::Notification do

    factory :notification_text_message do
      description 'TEXT_MESSAGE'
      message "chocolate"
    end
  end

  factory :friendship, :class => JamRuby::Friendship do

  end
end
