class Admission::ApplicantDecorator < ApplicationDecorator
  delegate :enrollment, to: :application

  def age
    months = time_in_months(Time.current) - time_in_months(date_of_birth)
    months -= 1 if Time.current.day < date_of_birth.day
    age = months.divmod(12)
    "#{age.first} years, #{age.second} months"
  end

  def grade_level
    grade_levels[grade]
  end

  def current_grade_level
    return nil if student.nil?

    grade_levels[student.grade]
  end

  def gender_label
    { 'male' => 'Male', 'female' => 'Female' }[gender]
  end

  def status_name
    status.nil? ? nil : status.name
  end

  def system_status
    return status.decorate.title if status
    return 'Not Submitted' if unsubmitted?

    state.humanize
  end

  def status_options
    active_statuses = application.school.admission_statuses.active.send(state)
    options = active_statuses.map { |s| [s.decorate.title, s.id] }.to_h
    return options unless status&.archived

    options.merge(status.decorate.title => status.id)
  end

  def application_type
    application.enrollment ? 'Enrollment' : 'Entrance'
  end

  def birth_date
    l(date_of_birth)
  end

  def submission_date
    submitted_at.nil? ? nil : l(submitted_at.to_date)
  end

  def submission_datetime
    submitted_at.nil? ? nil : submitted_at.strftime('%m/%d/%Y %l:%M%P')
  end

  def created_date
    l(created_at.to_date)
  end

  def created_datetime
    created_at.strftime('%m/%d/%Y %l:%M%P')
  end

  def status_datetime
    unsubmitted? ? created_datetime : submission_datetime
  end

  def index_path
    if application.enrollment
      h.enrollment_admission_applicants_url
    else
      h.entrance_admission_applicants_url
    end
  end

  def race_list
    list = races.map(&:name)
    list.join(', ')
  end

  def ethnic_label
    Admission::Applicant.ethnicity_labels[ethnicity&.to_sym]
  end

  def payment_description
    object.payment_description || application.payment_description
  end

  def payment_amount
    object.payment_amount || application.payment_amount
  end

  private
    def time_in_months(time)
      time.year * 12 + time.month
    end

    def grade_levels
      @grade_levels ||= application.school.grades_hash
    end
end
