class UserMailer < ActionMailer::Base
  default from: APPLICATION['defaults']['from_email']
  helper :application

  def get_from_email_address(user_id)
    @user = User.where(id: user_id).last 
    if @user.organisation.present? && @user.organisation.branding
      @user.organisation.organisation_support_email
    else
      APPLICATION['defaults']['from_email']
    end
  end

  def pdf_email(report_id,user_id,organisation_id)
    @report = Report.find(report_id)
    attachments['report.pdf'] = File.read(@report.path)
    subject_text = "PCC - Report for #{@report.location_network.network_name}"
    body_text = "Dear, Please find the attachment report for Network #{@report.location_network.network_name}"
    organisation =Organisation.find(organisation_id)
    config = organisation.email_config
    if user_id.class == Array
      if config.blank?
        mail(from: get_from_email_address(organisation.users.first.id), bcc: user_id.join(','), subject: subject_text, body: body_text)
      else
        custom_gateway(body_text,subject_text,user_id,config.config_hash,true)
      end
    else
      @user = User.where(:id => user_id).first
      if @user.present?
        if config.blank?
          mail(from: get_from_email_address(user_id), to: @user.user_id, subject: subject_text, body: body_text)
        else
          custom_gateway(body_text,subject_text,@user.email,config.config_hash,true)
        end
      end  
    end
  end

  def notification_email(user_id, notification_id,network_name,organisation_id)
    @notification = Notification.where(:id => notification_id).last
    return unless @notification.present?
    condition = @notification.extra_data["condition"]
    organisation =Organisation.find(organisation_id)
    config = organisation.email_config
    subject_text = @notification.subject.blank? ? "Alert on AP #{@notification.ap_mac_id} in network #{network_name} #{(condition == 'up' || condition == 'down') ? 'has gone ' + condition : ''}" : @notification.subject
    if user_id.class == Array
        if config.blank?
          mail(from: get_from_email_address(organisation.users.first.id), bcc: user_id.join(','), subject: subject_text)
        else
          custom_gateway('notification_email',subject_text,user_id.join(','),config.config_hash)
        end
    else
        @user = User.where(:id => user_id).first
        if @user.present?
          if config.blank?
            mail(from: get_from_email_address(user_id), to: @user.email, subject: subject_text)
          else
            custom_gateway('notification_email',subject_text, @user.email,config.config_hash)
          end
        end  
    end
  end
  
  def user_approved(user_id,url)
    @user = User.find(user_id)
    if @user.present?
       @url= url
      config = @user.organisation.email_config
      if config.blank?
        mail(to: @user.email, subject: 'Account Activated')
      else
        custom_gateway('user_approved','Account Activated',@user.email,config.config_hash)
      end
    end  
  end

  def notify_admin(url,user_id)
    @user = User.find(user_id)
    if @user.present?
      @url = url
        mail(to: 'techsupport@prontonetworks.com', subject: 'New Account is Waiting for Approval in PCC')
    end    
  end

  def password_changed(user_id)
    @user = User.find(user_id)
    if @user.present?
      config = @user.organisation.email_config
      if config.blank?
        mail(from: get_from_email_address(user_id), to: @user.email, subject: 'Password Changed')
      else
        custom_gateway('password_changed','Password Changed',@user.email,config.config_hash)
      end
    end  
  end

  def custom_gateway html_file,subject,senders,config,attachment=false
   options=
    # {address:  'smtp.gmail.com',port: 587, domain:  'gmail.com',authentication: :plain,user_name: 'pradeep@wavespot.net',password: 'comakeit@123456'}
    {:address=>config["address"], :domain=>config["domain"], :port=>config["port"], :authentication=>config["auth_type"], :user_name=>config["u_name"], :password=>config["pwd"],:content_type =>"multipart/alternative",:enable_starttls_auto => false}
    options[:tls] = true if config["port"] == "465"
    options[:enable_starttls_auto] = true if config["port"] == "587"
    puts options
    Mail.defaults do
      delivery_method :smtp, options
    end
    html_body = attachment ? html_file : render_to_string(html_file,:locals => { :@user => @user },layout: false)
    mail = Mail.new
    mail.from = options[:user_name]
    if senders.class == Array
      mail.bcc = senders.join(',')
    else
      mail.to = senders
    end
    mail.subject = subject
    if attachment
      mail.attachments['report.pdf'] = {:mime_type => 'application/pdf', :content => File.read(@report.path,mode: "rb")}
    else
      mail.content_type 'text/html; charset=UTF-8'
    end 
    mail.body html_body 
    mail.deliver!
  end
end

