class ContactController < ApplicationController
  
  layout "public"
  
  def new
    @lang = LangSite.first
  end

  def create
    @lang = LangSite.first
    
    if valid_captcha?(params['g-recaptcha-response'])
    
      if true
        @params = params
        DemandeMail.prise(@params).deliver
        DemandeMail.remerciement(@params).deliver
    
    
        api_url = "http://#{UAL_API_HOST}/admin/contacts/api"


        @c = Curl::Easy.new(api_url) do |curl|      
          curl.verbose = true
        end


        @c.http_post(
        Curl::PostField.content(:name, @params[:name]),
        Curl::PostField.content(:address, @params[:address]),
        Curl::PostField.content(:address2, @params[:address2]),
        Curl::PostField.content(:cp, @params[:cp]),
        Curl::PostField.content(:city, @params[:city]),
        Curl::PostField.content(:tel, @params[:tel]),
        Curl::PostField.content(:mail, @params[:mail]),
        Curl::PostField.content(:place, @params[:place]),
        Curl::PostField.content(:type, @params[:type].to_s),
        Curl::PostField.content(:owner, @params[:owner].to_s),
        Curl::PostField.content(:message, @params[:message])
    
        )

        @debug = @c.body_str
      else
        render :inline => "ok"
      end
    else
      redirect_to :back
    end
    
    
  end
  
  def valid_captcha?(recaptcha_response)
      return true if Rails.env.test?

      
      @c = Curl::Easy.new("https://www.google.com/recaptcha/api/siteverify") do |curl|      
        curl.verbose = true
      end


      @c.http_post(
      Curl::PostField.content(:secret, RECAPTCHA_SECRET_KEY),
      Curl::PostField.content(:response, recaptcha_response)
  
      )

      @debug = JSON.parse(@c.body_str) #["successe"]
      
      return @debug["success"]
      
      
  end
  
end
