  module JamRuby
    # UserMailer must be configured to work
    # Some common configs occur in jam_ruby/init.rb
    # Environment specific configs occur in spec_helper.rb in jam-ruby and jam-web (to put it into test mode),
    # and in config/initializers/email.rb in rails to configure sendmail account settings
    # If UserMailer were to be used in another project, it would need to be configured there, as well.

    # Templates for UserMailer can be found in jam_ruby/app/views/jam_ruby/user_mailer
    class UserMailer < ActionMailer::Base
      include SendGrid

      layout "user_mailer"

      DEFAULT_SENDER = "JamKazam <noreply@jamkazam.com>"

      default :from => DEFAULT_SENDER

      sendgrid_category :use_subject_lines
      #sendgrid_enable   :opentrack, :clicktrack  # this makes our emails creepy, imo (seth)
      sendgrid_unique_args :env => Environment.mode

      def confirm_email(user, signup_confirm_url)
        @user = user
        @signup_confirm_url = signup_confirm_url
        sendgrid_category "Confirm Email"
        sendgrid_unique_args :type => "confirm_email"

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => user.email, :subject => "Please confirm your JamKazam email") do |format|
          format.text
          format.html
        end
      end

      def welcome_message(user)
        @user = user
        sendgrid_category "Welcome"
        sendgrid_unique_args :type => "welcome_message"

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])
        sendgrid_substitute(EmailBatchProgression::VAR_FIRST_NAME, [user.first_name])

        mail(:to => user.email, :subject => "Welcome to JamKazam") do |format|
          format.text
          format.html
         end
      end

      def password_changed(user)
        @user = user

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])

        sendgrid_unique_args :type => "password_changed"
        mail(:to => user.email, :subject => "JamKazam Password Changed") do |format|
          format.text
          format.html
         end
      end

      def password_reset(user, password_reset_url)
        @user = user

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])

        @password_reset_url = password_reset_url
        sendgrid_unique_args :type => "password_reset"
        mail(:to => user.email, :subject => "JamKazam Password Reset") do |format|
          format.text
          format.html
        end
      end

      def updating_email(user)
        @user = user

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])

        sendgrid_unique_args :type => "updating_email"
        mail(:to => user.update_email, :subject => "JamKazam Email Change Confirmation") do |format|
          format.text
          format.html
        end
      end

      def updated_email(user)
        @user = user

        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])

        sendgrid_unique_args :type => "updated_email"
        mail(:to => user.email, :subject => "JamKazam Email Changed") do |format|
          format.text
          format.html
        end
      end

      def new_musicians(user, new_musicians, host='www.jamkazam.com')
        @user, @new_musicians, @host = user, new_musicians, host
        sendgrid_recipients([user.email])
        sendgrid_substitute('@USERID', [user.id])
        sendgrid_unique_args :type => "new_musicians"

        mail(:to => user.email, :subject => EmailBatchNewMusician.subject) do |format|
          format.text
          format.html
        end
      end

      #################################### NOTIFICATION EMAILS ####################################
      def friend_request(user, msg, friend_request_id)
        return if !user.subscribe_email

        email = user.email
        subject = "You have a new friend request on JamKazam"
        unique_args = {:type => "friend_request"}

        @url = Nav.accept_friend_request_dialog(friend_request_id)
        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def friend_request_accepted(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "You have a new friend on JamKazam"
        unique_args = {:type => "friend_request_accepted"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def new_user_follower(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "You have a new follower on JamKazam"
        unique_args = {:type => "new_user_follower"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def new_band_follower(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "Your band has a new follower on JamKazam"
        unique_args = {:type => "new_band_follower"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def session_invitation(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "You have been invited to a session on JamKazam"
        unique_args = {:type => "session_invitation"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def musician_session_join(user, msg, session_id)
        return if !user.subscribe_email

        email = user.email
        subject = "Someone you know is in a session on JamKazam"
        unique_args = {:type => "musician_session_join"}
        @body = msg
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_invitation(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session Invitation"
        unique_args = {:type => "scheduled_session_invitation"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_rsvp(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session RSVP"
        unique_args = {:type => "scheduled_session_rsvp"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_rsvp_approved(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session RSVP Approved"
        unique_args = {:type => "scheduled_session_rsvp_approved"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_rsvp_cancelled(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session RSVP Cancelled"
        unique_args = {:type => "scheduled_session_rsvp_cancelled"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_rsvp_cancelled_org(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Your Session RSVP Cancelled"
        unique_args = {:type => "scheduled_session_rsvp_cancelled_org"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_cancelled(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session Cancelled"
        unique_args = {:type => "scheduled_session_cancelled"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_rescheduled(user, msg, session)
        return if !user.subscribe_email

        email = user.email
        subject = "Session Rescheduled"
        unique_args = {:type => "scheduled_session_rescheduled"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_reminder_upcoming(user, session)
        subject = "Your JamKazam session starts in 1 hour!"
        unique_args = {:type => "scheduled_session_reminder_upcoming"}
        send_scheduled_session_reminder(user, session, subject, unique_args)
      end

      def scheduled_session_reminder_day(user, session)
        subject = "JamKazam Session Reminder"
        unique_args = {:type => "scheduled_session_reminder_day"}
        send_scheduled_session_reminder(user, session, subject, unique_args)
      end

      def send_scheduled_session_reminder(user, session, subject, unique_args)
        return if !user.subscribe_email

        email = user.email
        @user = user
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def scheduled_session_comment(target_user, sender, msg, comment, session)
        return if !target_user.subscribe_email

        email = target_user.email
        subject = "New Session Comment"
        unique_args = {:type => "scheduled_session_comment"}
        @body = msg
        @session_name = session.name
        @session_date = session.pretty_scheduled_start(true)
        @comment = comment
        @sender = sender
        @suppress_user_has_account_footer = true
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session.id}/details"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [target_user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html { render :layout => "from_user_mailer" }
        end
      end

      def scheduled_session_daily(receiver, sessions_and_latency)
        sendgrid_category "Notification"
        sendgrid_unique_args :type => "scheduled_session_daily"

        sendgrid_recipients([receiver.email])
        sendgrid_substitute('@USERID', [receiver.id])

        @user = receiver
        @sessions_and_latency = sessions_and_latency

        @title = 'New Scheduled Sessions Matched to You'
        mail(:to => receiver.email,
             :subject => EmailBatchScheduledSessions.subject) do |format|
          format.text
          format.html
        end
      end

      def band_session_join(user, msg, session_id)
        return if !user.subscribe_email

        email = user.email
        subject = "A band that you follow has joined a session"
        unique_args = {:type => "band_session_join"}

        @body = msg
        @session_url = "#{APP_CONFIG.external_root_url}/sessions/#{session_id}"
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def musician_recording_saved(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "A musician has saved a new recording on JamKazam"
        unique_args = {:type => "musician_recording_saved"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def band_recording_saved(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "A band has saved a new recording on JamKazam"
        unique_args = {:type => "band_recording_saved"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def band_invitation(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "You have been invited to join a band on JamKazam"
        unique_args = {:type => "band_invitation"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end

      def band_invitation_accepted(user, msg)
        return if !user.subscribe_email

        email = user.email
        subject = "Your band invitation was accepted"
        unique_args = {:type => "band_invitation_accepted"}

        @body = msg
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html
        end
      end


      def text_message(user, sender_id, sender_name, sender_photo_url, message)
        return if !user.subscribe_email

        email = user.email
        subject = "Message from #{sender_name}"
        unique_args = {:type => "text_message"}

        @note = message
        @url = Nav.home(dialog: 'text-message', dialog_opts: {d1: sender_id})
        @sender_id = sender_id
        @sender_name = sender_name
        @sender_photo_url = sender_photo_url
        sendgrid_category "Notification"
        sendgrid_unique_args :type => unique_args[:type]

        sendgrid_recipients([email])
        sendgrid_substitute('@USERID', [user.id])

        mail(:to => email, :subject => subject) do |format|
          format.text
          format.html { render :layout => "from_user_mailer" }
        end
      end

      # def send_notification(email, subject, msg, unique_args)
      #   @body = msg
      #   sendgrid_category "Notification"
      #   sendgrid_unique_args :type => unique_args[:type]
      #   mail(:bcc => email, :subject => subject) do |format|
      #     format.text
      #     format.html
      #   end
      # end
      #############################################################################################

    end
  end
