class CycleEntry
  attr_accessor :weight, :back_arcs, :patients, :id

  def initialize(id, data)
    @weight = 'N/A'
    @back_arcs = 'N/A'
    @patients = []
    @any_back_arcs = false
    @altruistic = false
    @alternative = []
	  @id = id

    if(data.blank?)
      return
    end

    process_cycles(data)
  end

  def process_cycles(data)
    data['cycle'].each do |element|
      altruistic = element.key?('a') ? element['a'] : false
	  backarc = element.key?('b') ? element['b'] : -1
      @patients << Patient.new(element['p'], element['d'], altruistic, backarc)
    end

    @weight = data['weight']
    @back_arcs = data['backarcs']  
  end

  def patients_as_string
    patients_out = ''
    @patients.each do |patient|
      id_out = "#{patient.id.to_s}, "
      if(patient.altruistic)
        id_out = '<span class="altruistic_id">' + patient.donor_id.to_s + '</span>, '
      end
      patients_out += id_out
    end
    patients_out.chop!.chop!
    patients_out
  end

  def patients_with_donors_as_string
    patient_donor_out = ''
    @patients.each do |patient|
      patient_donor_out += "#{patient.id.to_s}(#{patient.donor_id.to_s}), "
    end
    patient_donor_out.chop!.chop!
    patient_donor_out    
  end

  def patient_back_arcs_as_string(all_cycles)
    arcs_out = ""
    @patients.length.times do |i|
      if(@patients[i].backarc_with_next?)
		arcs_out += "[" + all_cycles[@patients[i].backarc_id].patients_as_string + "] "
      end
    end
    if(arcs_out.blank?)
      arcs_out = "&nbsp;"
    end
    arcs_out
  end

  def any_back_arcs?
    if(@any_back_arcs)
      return @arcs
    end

    @arcs = false
    @patients.each do |patient|
       @arcs = @arcs || patient.backarc_with_next?
    end

    @any_back_arcs = true
    @arcs
  end
end