class Api::InformationsController < ApiController
  respond_to :json

  def requests
    callback = params[:callback]
    @information_request = InformationRequest.new()
    @information_request.requested_information = params[:requested_information]
    @information_request.entity_type = params[:entity_type]
    @information_request.institution_id = params[:institution_id]
    @information_request.delivery_way = params[:delivery_way]    
    @information_request.firstname = params[:firstname]
    @information_request.lastname = params[:lastname]
    @information_request.age = params[:age]
    @information_request.gender = params[:gender]
    @information_request.nationality = params[:nationality]
    @information_request.email = params[:email]
    @information_request.phone = params[:phone]
    @information_request.cellular = params[:cellular]
    @information_request.fax = params[:fax]
    @information_request.address = params[:address]
    @information_request.state_id = params[:state_id]
    @information_request.city_id = params[:city_id]
    @information_request.occupation_id = params[:occupation_id]
    @information_request.educational_level = params[:educational_level]
    @information_request.document_type = params[:document_type]
    @information_request.document_number = params[:document_number]
    @information_request.textcaptcha
    @information_request.skip_textcaptcha = true
    @information_request.notification_mode = params[:notification_mode] || []
    session[:information_request_id] = @information_request.id

    @information_request.document_front_image = params[:file] if params[:file].present?

    if @information_request.save
      @information_request.delay.deliver_confirmation_email_request
      content = { :response  => 'success' }.to_json
    else      
      content = { :response  => 'failed' }.to_json
    end
    respond_to do |format|
      format.html {
        render json: "#{callback}(#{content})"
      }
      format.json { 
        render :json => "#{callback}(#{content})"
      }
      format.html
    end
  end

  def coords
    callback = params[:callback]
    gas_type = params[:coordinates][:fuel_type] rescue "GR"
    platitude = params[:coordinates][:latitude] rescue '13.706881'
    plongitude = params[:coordinates][:longitude] rescue '-89.20181'
    distance = params[:coordinates][:distance] rescue '1'
    @references = HydroReference.latest
    @establishments = HydroEstablishment.within(distance.to_i, :origin => [platitude, plongitude], units: :kms).uniq
    reference_fservice = @references.select{|p| p.product_code == gas_type and p.hydro_zone_id == @establishments.first.hydro_zone_id and p.service_code == 1}.first.price.round(2) rescue 0
    references_details = @references.map do |p|
      {
        start_at: l(p.start_at, format: :saving_short).upcase,
        end_at: l(p.end_at, format: :saving_short).upcase,
        product_code: p.product_code,
        service_code: p.service_code,
        hydro_zone_id: p.hydro_zone_id,
        price: p.price,
      }
    end

    @establishments_detail = @establishments.map do |e|
      full_service = HydroPrice.select("hydro_prices.price").where("hydro_establishment_id = ? AND product_code = ? AND service_code = ?", e.id, gas_type, 1).order("updated_at DESC").first.price.to_f.round(2) rescue 0
      autoservice = HydroPrice.select("hydro_prices.price").where("hydro_establishment_id = ? AND product_code = ? AND service_code = ?", e.id, gas_type, 2).order("updated_at DESC").first.price.to_f.round(2) rescue 0
      { id: e.id, 
        reference_fservice: reference_fservice,
        name: e.name, 
        address: e.address,
        brand: e.brand,
        latitude: e.latitude, 
        longitude: e.longitude, 
        price_full_service: full_service,
        price_autoservice: autoservice,
      }
    end

    @list = { :establishments => @establishments_detail, :prices_references => references_details }.to_json
    respond_to do |format|
      format.json { 
        render :json => "#{callback}(#{@list})"  #"#{@list}"
      }
    end
  end

  def gas_establishments
    callback = params[:callback]
    @establishment = HydroEstablishment.find(params[:id].to_i) rescue nil
    content = { :response  => 'success', :e_name =>@establishment.name.to_s, :e_address => @establishment.address.to_s, :elatitude => @establishment.latitude, :elongitude => @establishment.longitude}.to_json
    respond_to do |format|
      format.json { 
        render :json => "#{callback}(#{content})"
      }
    end
  end

  def contacts
    callback = params[:callback]
    @contact_message = ContactMessage.new(params[:contact_message])
    @contact_message.textcaptcha
    @contact_message.skip_textcaptcha = true
    if @contact_message.save
      content = { :response  => 'success' }.to_json
    else      
      content = { :response  => 'failed' }.to_json
    end
    respond_to do |format|
      format.json { 
        render :json => "#{callback}(#{content})"
      }
    end
  end

end