
class KidneyController < ApplicationController

  def find
    @data = params[:data] || ""
    @operation = params[:operation] || "optimal"
    @altruistic_chain_length = params[:altruistic_chain_length] || '1'
    @altruistic_chain_length = Integer(@altruistic_chain_length)

    @pairwise = "lemon"

    respond_to do |format|
          format.html {
            start_time = Time.now
            @exchanges = KidneyExchange::KidneyAllocator.new(KIDNEY_APP_CONFIG, KIDNEY_WEIGHT_CONFIG).find_json(@data,
																											    @operation,
																											    @pairwise,
																											    @altruistic_chain_length)
            @elapsed = Time.now - start_time
            @json = JSON.parse(@exchanges)

            if(@json.key?('error'))
              @error = @json['error']
              render :action => 'error'
              return
            end

            @exchange_data = ExchangeData.new(@json, @operation)
          
            render
          }
          
          format.json { render :json => KidneyExchange::KidneyAllocator.new(KIDNEY_APP_CONFIG, KIDNEY_WEIGHT_CONFIG).find_json(@data,
																															   @operation,
																															   @pairwise,
																															   @altruistic_chain_length)}
	                                                                       
          format.xml  { render :xml => KidneyExchange::KidneyAllocator.new(KIDNEY_APP_CONFIG, KIDNEY_WEIGHT_CONFIG).find_xml(@data,
																															 @operation,
																															 @pairwise,
																															 @altruistic_chain_length)}
      end
  end

  def index
    @operations = [['optimal', 'optimal'], ['maxcard','maxcard'], ['pairs', 'pairs']]
    @chain_lengths = [['1', '1'],['2','2']]
  end
end
