class Application < Sinatra::Base

  #Helpers 

  def generate_response_badges badges
      badges.map { |each|  
        {  id_badge_class:each["slug"], 
           name:each["name"],
           imageUrl: each["imageUrl"],
           description: each["earnerDescription"],
           criteriaUrl: each["criteriaUrl"],
           criteria: each["criteria"]} 
       }
  end  


  #List all Cientificos Ciudadanos Badge Classes. 
  get '/badges' do
    response = signed_get_request @@API_ROOT+"/badges"
    badges = []
    if ! response["badges"].nil?
      badges = generate_response_badges response["badges"]
    end
    JSON.pretty_generate badges
  end

  #List all <Application> Badge Classes (achievements).
  get '/issuers/:id_app/badges' do
    status 200
    response = signed_get_request @@API_ROOT+"/issuers/#{params[:id_app]}/badges"
    badges = []
    if ! response["badges"].nil?
      badges = generate_response_badges response["badges"]
    end
  
    JSON.pretty_generate badges
  end

  #Create Badge Class
  post '/badges' do
    #'https://www.dropbox.com/s/nvukekjbuql19jd/badge1.png?dl=1'     
    description = params["description"]
    body = {
        name:params["name"],
        imageUrl:params["imageUrl"],
        unique: true,
        criteriaUrl: params["criteriaUrl"],
        earnerDescription: description,
        consumerDescription: description ,
        type: 'Badge'
      }
    status 201
    
    response = signed_post_request @@API_ROOT+"/badges", body
    JSON.pretty_generate response
  end

  #Create Achievement Badge Class
  post '/issuers/:id_app/badges' do
    #'https://www.dropbox.com/s/nvukekjbuql19jd/badge1.png?dl=1'     
    description = params["description"]
    body = {
        name:params["name"],
        imageUrl:params["imageUrl"],
        unique: true,
        criteriaUrl: params["criteriaUrl"],
        earnerDescription: description,
        consumerDescription: description ,
        type: 'Badge'
      }
    status 201
    
    response = signed_post_request @@API_ROOT+"/issuers/#{params[:id_app]}/badges", body
    JSON.pretty_generate response
  end
end