class ContactMailer < BaseMailer

  # Send notification email to user when someone send message to user if user is offline.
  def offline_chat(message_id, receipt_id)
    offline_reply_to = Configurations::General.reply_to unless Configurations::General.reply_to.blank?# Staging Email Precessor ID
#  offline_reply_to = "inbound@tutor1.bymail.in" # Local Email Precessor ID
    message = Message.find(message_id)
    receipt = MessageReceipt.find(receipt_id)
    receiver = User.find(receipt.user_id)
    begin
      @name = message.sender.name
      @message = message.message
      @time = message.created_at.strftime('%d-%b-%Y %I:%M%p')
      @image = message.sender.image_url
      @recipient_email = receiver.email
      @from_id = message.sender.id
      @to_id = receiver.id
      unless offline_reply_to.blank?
        domain = Configurations::General.reply_to.split('@').last
        headers "Message-ID" => "<identifier_#{message.university_id}_conversation_#{message.chat_conversation_id}_#{message.id}_#{receipt.id}@#{domain}>", "In-Reply-To" => "<Conversation##{message.chat_conversation_id}@#{domain}>"
        mail(
          :from => message.sender.email,
          :to => @recipient_email,
          :subject => "New chat message from #{@name}",
          :reply_to => offline_reply_to) do |format|
            format.html {render layout: 'mailer'}
          end
      else
        mail(
          :from => message.sender.email,
          :to => @recipient_email,
          :subject => "New chat message from #{@name}"
        )
      end
    rescue => e
      Rails.logger.debug("Exceptions #{e}")
    end
  end

  # Visitors of app send message to provided emails
  def new_contact(email, message)
    @email = email
    @message = message
    mail(:subject => "Message from a #{Configurations::General.application_name} user") do |format|
      format.html { render :layout => 'mailer' }
    end
  end

  # send email with the search result of Tutor, subject, Course
  def search_for_tutor(details)
    @details = details
    mail(:subject => "#{Tutor.model_name.human} search request. subject:#{details[:subject]}, course:#{details[:course]} - #{Configurations::General.application_name}") do |format|
      format.html { render :layout => 'mailer' }
    end
  end

  # send email if not able to search the requested keyword from visitor
  def no_result_found(keyword, user)
    @keyword = keyword
    mail(:subject => "No result found - #{Configurations::General.application_name} search Engine") do |format|
      format.html { render :layout => 'mailer' }
    end
  end

  # To send message over the email.
  def send_message(name,email,subject,message,rcpt_email,sender_type)
    @name = name
    @sender_email = email
    @recipient_email = rcpt_email
    @subject = subject
    @message = message
    @sender_type = sender_type
    mail(:to => @recipient_email,:subject => "#{@subject} - #{Configurations::General.application_name}") do |format|
      format.html { render :layout => 'mailer' }
    end
  end

end
