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

    factory :single_user_session do
      after(:create) do |user, evaluator|
        music_session = FactoryGirl.create(:active_music_session, :creator => user)
        connection = FactoryGirl.create(:connection, :user => user, :music_session => music_session)
      end
    end
  end

  factory :connection, :class => JamRuby::Connection do
    sequence(:client_id) { |n| "Client#{n}" }
    ip_address "1.1.1.1"
    as_musician true
    addr 0
    locidispid 0
    client_type 'client'
    gateway 'gateway1'
    scoring_timeout Time.now
    sequence(:channel_id) { |n| "Channel#{n}"}
    association :user, factory: :user
    metronome_open false
  end
  
  factory :artifact_update, :class => JamRuby::ArtifactUpdate do
    sequence(:version) { |n| "0.1.#{n}" }
    uri { "http://somewhere/jkclient.msi" }
    product { "JamClient/Win32" }
    environment { "public" }
    sha1 { "blurp" }
  end

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

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

  factory :genre, :class => JamRuby::Genre do
    description { |n| "Genre #{n}" }
  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'
    timezone 'UTC,Etc/UTC'
    open_rsvps true
    scheduled_start Time.now
    scheduled_duration 3600
    genre JamRuby::Genre.first
    association :creator, :factory => :user
  end

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

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

  factory :recorded_track, :class => JamRuby::RecordedTrack do
    instrument JamRuby::Instrument.first
    sound 'stereo'
    sequence(:client_id) { |n| "client_id-#{n}"}
    sequence(:track_id) { |n| "track_id-#{n}"}
    sequence(:client_track_id) { |n| "client_track_id-#{n}"}
    md5 'abc'
    length 1
    fully_uploaded true
    association :user,          factory: :user
    association :recording,     factory: :recording
  end

  factory :recording, :class => JamRuby::Recording do

    association :owner,           factory: :user
    association :music_session,   factory: :active_music_session

    factory :recording_with_track do
      before(:create) { |recording, evaluator|
        recording.recorded_tracks << FactoryGirl.create(:recorded_track, recording: recording, user: evaluator.owner)
      }
    end
  end

  factory :claimed_recording, :class => JamRuby::ClaimedRecording do
    sequence(:name) { |n| "name-#{n}" }
    sequence(:description) { |n| "description-#{n}" }
    is_public true
    association :genre,      factory: :genre
    association :user,       factory: :user

    before(:create) { |claimed_recording|
      claimed_recording.recording = FactoryGirl.create(:recording_with_track, owner: claimed_recording.user)
    }

  end

  factory :mix, :class => JamRuby::Mix do
    started_at Time.now
    completed_at Time.now
    ogg_md5 'abc'
    ogg_length 1
    sequence(:ogg_url) { |n| "recordings/ogg/#{n}" }
    mp3_md5 'abc'
    mp3_length 1
    sequence(:mp3_url) { |n| "recordings/mp3/#{n}" }
    completed true

    before(:create) {|mix|
      user = FactoryGirl.create(:user)
      mix.recording = FactoryGirl.create(:recording_with_track, owner: user)
      mix.recording.claimed_recordings << FactoryGirl.create(:claimed_recording, user: user, recording: mix.recording)
    }
  end

  factory :diagnostic, :class => JamRuby::Diagnostic do
    type JamRuby::Diagnostic::NO_HEARTBEAT_ACK
    creator JamRuby::Diagnostic::CLIENT
    data Faker::Lorem.sentence
    association :user,       factory: :user
  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)
      }

      factory :music_session_with_mount do
        association :mount, :factory => :icecast_mount
      end
    end
  end

  factory :latency_tester, :class => JamRuby::LatencyTester do
    transient 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}" }
    sequence(:slug) { |n| "slug-#{n}" }
    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
end
