require 'mandrill'

class Mailer
  class << self
    def send_message(email, template_name, vars)
        mandrill = Mandrill::API.new ENV['MANDRILL_API_KEY']
        merge_vars = [
            {:name => 'firstname', :content => vars[:firstname] },
            {:name => 'appliance_url', :content => vars[:appliance_url] },
            {:name => 'ssh_password', :content => vars[:ssh_password] },
            {:name => 'demo_page_url', :content => render_demopage_url(vars[:demo_name]) }
        ]
        message = {
            :subject => render_subject(vars[:demo_name], vars[:firstname]),
            :from_email => 'robot@conjur.net',
            :from_name => 'Conjur, Inc.',
            :to => [{
                  :name => "#{vars[:firstname]} #{vars[:lastname]}",
                  :type => 'to',
                  :email => email
            }],
            :global_merge_vars => merge_vars
        }
        mandrill.messages.send_template template_name, [], message
    end

    private

    def render_subject(demo_name, firstname)
      default = "#{firstname}, Here is your Conjur Secrets Demo!"
      case demo_name
        when 'secrets'
          default
        when 'ssh'
          "#{firstname}, Here is your Conjur SSH Demo!"
        else
          default
      end
    end

    def render_demopage_url(demo_name)
      "http://developer.conjur.net/product_demo/#{demo_name}.html"
    end
  end
end