# The purpose of this 'test' is to render emails to disk,
# So that a developer can look in tmp/emails and open them up and verify that they look OK
# Also, to have Jenkins archive them to make it easier to check if a build look OK

require "spec_helper"

describe "RenderMailers", :slow => true do

  let(:user) { FactoryGirl.create(:user) }
  let(:school) {FactoryGirl.create(:school, education:true)}

  before(:each) do
    @filename = nil # set this on your test to pin the filename; i just make it the name of the mailer method responsible for sending the mail
  end

  describe "UserMailer emails" do

    before(:each) do
      user.update_email = "my_new_email@jamkazam.com"
      UserMailer.deliveries.clear
    end

    after(:each) do
      UserMailer.deliveries.length.should == 1
      mail = UserMailer.deliveries[0]
      save_emails_to_disk(mail, @filename)
    end

    it { @filename="welcome_message"; UserMailer.welcome_message(user).deliver_now }
    it { @filename="student_welcome_message"; UserMailer.student_welcome_message(user).deliver_now }
    it { @filename="student_welcome_message_education"; user.school = school; user.save!; UserMailer.student_welcome_message(user).deliver_now }
    it { @filename="school_owner_welcome_message"; UserMailer.school_owner_welcome_message(user).deliver_now }
    it { @filename="education_owner_welcome_message"; UserMailer.education_owner_welcome_message(user).deliver_now }
    it { @filename="confirm_email"; UserMailer.confirm_email(user, "/signup").deliver_now }
    it { @filename="password_reset"; UserMailer.password_reset(user, '/reset_password').deliver_now }
    it { @filename="password_changed"; UserMailer.password_changed(user).deliver_now }
    it { @filename="updated_email"; UserMailer.updated_email(user).deliver_now }
    it { @filename="updating_email"; UserMailer.updating_email(user).deliver_now }

    describe "amazon_first_lesson_scheduled" do
      it {@filename="amazon_first_lesson_scheduled"; UserMailer.amazon_first_lesson_scheduled(user).deliver_now}
    end

    describe "amazon_first_lesson_instructions" do
      it {@filename="amazon_first_lesson_instructions"; UserMailer.amazon_first_lesson_instructions(user).deliver_now}
    end

    describe "has sending user" do
      let(:user2) { FactoryGirl.create(:user) }
      let(:friend_request) { FactoryGirl.create(:friend_request, user: user, friend: user2) }

      it { @filename="text_message"; UserMailer.text_message(user, user2.id, user2.name, user2.resolved_photo_url, 'Get online!!').deliver_now }
      it { @filename="friend_request"; UserMailer.friend_request(user, 'So and so has sent you a friend request.', friend_request.id).deliver_now }
    end

=begin
    describe "student/teacher" do
      let(:teacher) { u = FactoryGirl.create(:teacher); u.user }
      let(:user) { FactoryGirl.create(:user) }

      it "teacher_welcome_message" do
        @filename = "teacher_welcome_message"
        UserMailer.teacher_welcome_message(teacher).deliver_now
      end

      it "teacher_counter_reminder" do
        @filename = "teacher_counter_reminder"

        lesson_session = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.teacher_counter_reminder(lesson_session).deliver_now
      end

      it "teacher_lesson_request" do
        @filename = "teacher_lesson_request"

        lesson_booking = testdrive_lesson(user, teacher).lesson_booking

        UserMailer.deliveries.clear
        UserMailer.teacher_lesson_request(lesson_booking).deliver_now
      end

      it "student_lesson_request" do
        @filename = "student_lesson_request"

        lesson_booking = testdrive_lesson(user, teacher).lesson_booking
        UserMailer.deliveries.clear
        UserMailer.student_lesson_request(lesson_booking).deliver_now
      end

      it "teacher_lesson_accepted" do
        @filename = "teacher_lesson_accepted"

        lesson_session = testdrive_lesson(user, teacher)
        UserMailer.deliveries.clear
        UserMailer.teacher_lesson_accepted(lesson_session, "custom message", lesson_session.lesson_booking.default_slot).deliver_now
      end

      it "student_lesson_accepted" do
        @filename = "student_lesson_accepted"

        lesson_session = testdrive_lesson(user, teacher)
        UserMailer.deliveries.clear
        UserMailer.student_lesson_accepted(lesson_session, "custom message", lesson_session.lesson_booking.default_slot).deliver_now
      end

      it "teacher_scheduled_jamclass_invitation" do
        @filename = "teacher_scheduled_jamclass_invitation"
        lesson_session = testdrive_lesson(user, teacher)
        UserMailer.deliveries.clear
        UserMailer.teacher_scheduled_jamclass_invitation(lesson_session.teacher, "custom message", lesson_session.music_session).deliver_now
      end

      it "student_scheduled_jamclass_invitation" do
        @filename = "student_scheduled_jamclass_invitation"
        lesson_session = testdrive_lesson(user, teacher)
        UserMailer.deliveries.clear
        UserMailer.student_scheduled_jamclass_invitation(lesson_session.student, "custom message", lesson_session.music_session).deliver_now
      end

      it "student_test_drive_no_bill" do
        @filename = "student_test_drive_no_bill"
        lesson = testdrive_lesson(user, teacher, {accept: true, miss: true})

        UserMailer.deliveries.clear
        UserMailer.student_test_drive_no_bill(lesson).deliver_now
      end

      it "teacher_test_drive_no_bill" do
        @filename = "teacher_test_drive_no_bill"
        lesson = testdrive_lesson(user, teacher, {accept: true, miss: true})

        UserMailer.deliveries.clear
        UserMailer.teacher_test_drive_no_bill(lesson).deliver_now
      end
      it "student_test_drive_lesson_completed" do
        @filename = "student_test_drive_lesson_completed"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.student_test_drive_lesson_completed(lesson).deliver_now
      end
      it "student_test_drive_done" do
        @filename = "student_test_drive_lesson_done"
        lesson = testdrive_lesson(user, teacher)
        lesson = testdrive_lesson(user, FactoryGirl.create(:teacher_user))
        lesson = testdrive_lesson(user, FactoryGirl.create(:teacher_user))
        lesson = testdrive_lesson(user, FactoryGirl.create(:teacher_user))

        UserMailer.deliveries.clear
        UserMailer.student_test_drive_lesson_done(lesson).deliver_now
      end
      it "teacher_lesson_completed" do
        @filename = "teacher_lesson_completed"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.teacher_lesson_completed(lesson).deliver_now
      end

      it "lesson_starting_soon_teacher" do
        @filename = "lesson_starting_soon_teacher"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.lesson_starting_soon_teacher(lesson, false, '6:36 am').deliver_now
      end

      it "lesson_starting_soon_student" do
        @filename = "lesson_starting_soon_student"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.lesson_starting_soon_student(lesson, false, '6:36 am').deliver_now
      end


      it "lesson_starting_soon_teacher_early" do
        @filename = "lesson_starting_soon_teacher_early"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.lesson_starting_soon_teacher(lesson, true, '6:36 am').deliver_now
      end

      it "lesson_starting_soon_student_early" do
        @filename = "lesson_starting_soon_student_early"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.lesson_starting_soon_student(lesson, true, '6:36 am').deliver_now
      end

      it "music_notation_attachment" do
        @filename = "music_notation_attachment"

        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        notation = FactoryGirl.create(:music_notation, user: user)
        UserMailer.lesson_attachment(user, teacher, lesson, notation).deliver_now
      end

      it "recording_attachment" do
        @filename = "recording_attachment"

        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        claim = FactoryGirl.create(:claimed_recording, user: user)
        UserMailer.lesson_attachment(user, teacher, lesson, claim).deliver_now
      end

      it "teacher_no_comm_other" do
        @filename = "teacher_no_comm_other"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.teacher_no_comm_other(lesson.lesson_booking).deliver_now
      end

      it "student_no_comm_other (late)" do
        @filename = "student_no_comm_other"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.student_no_comm_other(lesson.lesson_booking, false).deliver_now
      end

      it "student_no_comm_other (really late)" do
        @filename = "student_no_comm_other_late"
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.student_no_comm_other(lesson.lesson_booking, true).deliver_now
      end

      it "take_first_free_lesson 1" do
        @filename = "take_first_free_lesson_1"

        user.sent_take_flesson_email_times = 0
        user.save!

        UserMailer.deliveries.clear
        UserMailer.take_first_free_lesson(user).deliver_now
      end

      it "take_first_free_lesson 2" do
        @filename = "take_first_free_lesson_2"

        user.sent_take_flesson_email_times = 1
        user.save!

        UserMailer.deliveries.clear
        UserMailer.take_first_free_lesson(user).deliver_now
      end

      it "take_first_free_lesson 3" do
        @filename = "take_first_free_lesson_3"

        user.sent_take_flesson_email_times = 2
        user.save!

        UserMailer.deliveries.clear
        UserMailer.take_first_free_lesson(user).deliver_now
      end

      it "take_second_free_lesson 1" do
        @filename = "take_second_free_lesson_1"

        user.sent_take_2nd_flesson_email_times = 0
        user.save!

        lesson = testdrive_lesson(user, teacher)


        UserMailer.deliveries.clear
        UserMailer.take_second_free_lesson(user).deliver_now
      end

      it "take_second_free_lesson 2" do
        @filename = "take_second_free_lesson_2"

        user.sent_take_2nd_flesson_email_times = 1
        user.save!

        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.take_second_free_lesson(user).deliver_now
      end

      it "take_second_free_lesson 3" do
        @filename = "take_second_free_lesson_3"

        user.sent_take_2nd_flesson_email_times = 2
        user.save!

        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.take_second_free_lesson(user).deliver_now
      end

      it "take_paid_lesson 1" do
        @filename = "take_paid_lesson_1"

        user.sent_take_plesson_email_times = 0
        user.save!

        lesson = testdrive_lesson(user, teacher)
        lesson = testdrive_lesson(user, teacher)


        UserMailer.deliveries.clear
        UserMailer.take_paid_lesson(user).deliver_now
      end

      it "take_paid_lesson 2" do
        @filename = "take_paid_lesson_2"

        user.sent_take_plesson_email_times = 1
        user.save!

        lesson = testdrive_lesson(user, teacher)
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.take_paid_lesson(user).deliver_now
      end

      it "take_paid_lesson 3" do
        @filename = "take_paid_lesson_3"

        user.sent_take_plesson_email_times = 2
        user.save!

        lesson = testdrive_lesson(user, teacher)
        lesson = testdrive_lesson(user, teacher)

        UserMailer.deliveries.clear
        UserMailer.take_paid_lesson(user).deliver_now
      end

      it "onboarding_survey" do
        @filename = "onboarding_survey"
        UserMailer.deliveries.clear
        UserMailer.onboarding_survey(user).deliver_now
      end
    end
=end
  end

  describe "InvitedSchool emails" do
    before(:each) do
      UserMailer.deliveries.clear
    end

    after(:each) do
      UserMailer.deliveries.length.should == 1
      mail = UserMailer.deliveries[0]
      save_emails_to_disk(mail, @filename)
    end

    it "invite_school_teacher" do
      @filename = "invite_school_teacher"
      UserMailer.invite_school_teacher(FactoryGirl.create(:school_invitation, as_teacher: true)).deliver_now
    end

    it "invite_school_student" do
      @filename = "invite_school_student"
      UserMailer.invite_school_student(FactoryGirl.create(:school_invitation, as_teacher: false)).deliver_now
    end
  end

  describe "Retailer emails" do
    let(:retailer) {FactoryGirl.create(:retailer)}

    before(:each) do
      UserMailer.deliveries.clear
    end
    after(:each) do
      UserMailer.deliveries.length.should == 1
      # NOTE! we take the second email, because the act of creating the InvitedUser model
      # sends an email too, before our it {} block runs.  This is because we have an InvitedUserObserver
      mail = UserMailer.deliveries[0]
      save_emails_to_disk(mail, @filename)
    end
    it {@filename="retailer_customer_blast"; UserMailer.retailer_customer_blast('seth@jamkazam.com', retailer).deliver_now}
  end

  describe "InvitedUserMailer emails" do

    let(:user2) { FactoryGirl.create(:user) }
    let(:invited_user) { FactoryGirl.create(:invited_user, :sender => user2) }
    let(:admin_invited_user) { FactoryGirl.create(:invited_user) }

    before(:each) do
      InvitedUserMailer.deliveries.clear
    end

    after(:each) do
      UserMailer.deliveries.length.should == 2
      # NOTE! we take the second email, because the act of creating the InvitedUser model
      # sends an email too, before our it {} block runs.  This is because we have an InvitedUserObserver
      mail = InvitedUserMailer.deliveries[1]
      save_emails_to_disk(mail, @filename)
    end

    it { @filename="friend_invitation"; InvitedUserMailer.deliveries.clear; InvitedUserMailer.friend_invitation(invited_user).deliver_now }
    it { @filename="welcome_betauser"; InvitedUserMailer.welcome_betauser(admin_invited_user).deliver_now }
  end

  describe "Poke Amazon Users" do
    let(:teacher) { u = FactoryGirl.create(:teacher, biography: 'This is a really long biography for a really worthless human that does not deserve the first bit of trash. What? What was I saying. Oh yes, right.  For years me takey-takey lessons and givey-givey lessons.  But so what you say?  beat it'); u.user }
    let(:user) { FactoryGirl.create(:user) }

    before(:each) do
      UserMailer.deliveries.clear
    end
    after(:each) do
      UserMailer.deliveries.length.should == 1
      # NOTE! we take the second email, because the act of creating the InvitedUser model
      # sends an email too, before our it {} block runs.  This is because we have an InvitedUserObserver
      mail = UserMailer.deliveries[0]
      save_emails_to_disk(mail, @filename)
    end
    it {@filename="amazon_prompt_take_lesson_1"; UserMailer.amazon_prompt_take_lesson(user, teacher, 0).deliver_now}
    it {@filename="amazon_prompt_take_lesson_2"; UserMailer.amazon_prompt_take_lesson(user, teacher, 1).deliver_now}
    it {@filename="amazon_prompt_take_lesson_3"; UserMailer.amazon_prompt_take_lesson(user, teacher, 2).deliver_now}

  end

  describe "Daily Scheduled Session emails" do
    let (:scheduled_batch) { FactoryGirl.create(:email_batch_scheduled_session) }
    let(:music_session) { FactoryGirl.create(:music_session) }
    let (:drums) { FactoryGirl.create(:instrument, :description => 'drums') }
    let (:guitar) { FactoryGirl.create(:instrument, :description => 'guitar') }
    let (:bass) { FactoryGirl.create(:instrument, :description => 'bass') }
    let (:vocals) { FactoryGirl.create(:instrument, :description => 'vocal') }

    let (:drummer) { FactoryGirl.create(:user, :last_jam_locidispid => 1, :last_jam_addr => 1) }
    let (:guitarist) { FactoryGirl.create(:user, :last_jam_locidispid => 1, :last_jam_addr => 1) }
    let (:bassist) { FactoryGirl.create(:user, :last_jam_locidispid => 1, :last_jam_addr => 1) }
    let (:vocalist) { FactoryGirl.create(:user, :last_jam_locidispid => 1, :last_jam_addr => 1) }

    let (:session1) do
      FactoryGirl.create(:music_session,
                         :creator => drummer,
                         :scheduled_start => Time.now() + 2.days,
                         :musician_access => true,
                         :approval_required => false,
                         :created_at => Time.now - 1.hour)
    end

    let (:session2) do
      FactoryGirl.create(:music_session,
                         :creator => drummer,
                         :scheduled_start => Time.now() + 2.days,
                         :musician_access => true,
                         :approval_required => false,
                         :created_at => Time.now - 1.hour)
    end

    before(:each) do
      BatchMailer.deliveries.clear
      scheduled_batch.reset!

      drummer.musician_instruments << FactoryGirl.build(:musician_instrument, player: drummer, instrument: drums, proficiency_level: 2)
      drummer.musician_instruments << FactoryGirl.build(:musician_instrument, player: drummer, instrument: guitar, proficiency_level: 2)

      guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, player: guitarist, instrument: guitar, proficiency_level: 2)
      guitarist.musician_instruments << FactoryGirl.build(:musician_instrument, player: guitarist, instrument: bass, proficiency_level: 2)

      vocalist.musician_instruments << FactoryGirl.build(:musician_instrument, player: vocalist, instrument: vocals, proficiency_level: 2)

      FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session1)
      FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session1)
      FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session1)

      FactoryGirl.create(:rsvp_slot, :instrument => drums, :music_session => session2)
      FactoryGirl.create(:rsvp_slot, :instrument => guitar, :music_session => session2)
      FactoryGirl.create(:rsvp_slot, :instrument => bass, :music_session => session2)

      JamRuby::Score.createx(1, 'a', 1, 1, 'a', 1, 10)
      JamRuby::Score.createx(1, 'a', 1, 2, 'a', 2, Score::MAX_YELLOW_LATENCY + 1)
    end

    after(:each) do
      BatchMailer.deliveries.length.should == 1
      mail = BatchMailer.deliveries[0]
      save_emails_to_disk(mail, @filename)
    end

    it "daily sessions" do
      @filename="daily_sessions"; scheduled_batch.deliver_batch
    end
  end
end

def save_emails_to_disk(mail, filename)
  # taken from: https://github.com/originalpete/actionmailer_extensions/blob/master/lib/actionmailer_extensions.rb
  # this extension does not work with ActionMailer 3.x, but this method is all we need

  if filename.nil?
    filename = mail.subject
  end

  email_output_dir = 'tmp/emails'
  FileUtils.mkdir_p(email_output_dir) unless File.directory?(email_output_dir)
  filename = "#{filename}.eml"
  File.open(File.join(email_output_dir, filename), "w+") { |f|
    f << mail.encoded
  }
end