class StudentDecorator < ApplicationDecorator
  delegate :address, :address_ext, :city, :state, :zip, :county, :country, :home_phone, to: :family
  delegate :asthma?, :diabetes?, :seizures?, :deafness?, :sight_impairment?, :tylenol?, :ibuprofen?,
    :add?, :bladder?, :sicklecell?, :hemophiliac?, :healthy?, :medication, :allergies,
    to: :student_medical, allow_nil: true
  delegate :comments, :alerts, to: :student_medical, prefix: :medical, allow_nil: true
  delegate :exempt, :not_exempt?, to: :student_medical, prefix: :vaccine, allow_nil: true
  delegate :physician, :physician_phone, :physician_address, :physician_address_ext,
    :physician_city, :physician_state, :physician_zip, :dentist, :dentist_phone, :dentist_address,
    :dentist_address_ext, :dentist_city, :dentist_state, :dentist_zip, :insurance_company,
    :insurance_plan, :insurance_group, :hospital, to: :family_medical, allow_nil: true
  delegate :comments, to: :family_medical, prefix: true, allow_nil: true
  delegate :id, :username, :active, to: :user, prefix: true, allow_nil: true
  delegate :id, :birth_city, :birth_country_id, :birth_state_id, :multi_birth_status,
    to: :detail, prefix: true, allow_nil: true
  delegate :full_name, to: :homeroom_teacher, prefix: true, allow_nil: true
  delegate :name, to: :location, prefix: true, allow_nil: true
  delegate :address, to: :primary_email_address, prefix: true, allow_nil: true

  def birth_date
    l(date_of_birth) if date_of_birth
  end

  def age
    return if date_of_birth.nil?

    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 gender_label
    { 'male' => 'Male', 'female' => 'Female', 'non_binary' => 'Non-Binary' }[gender]
  end

  def grade_level
    grade_levels[grade]
  end

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

  def graduation_date
    return if graduation_year.zero?

    date = graduation_year.to_s
    return date if graduation_month.zero?

    date += "-#{graduation_month}"
    return date if graduation_day.zero?

    date += "-#{graduation_day}"
    date.to_date
  end

  def health_information
    StudentMedical::AILMENTS.map { |k, v| v if public_send("#{k}?") }.compact.join(', ')
  end

  def permitted_medications
    data = []
    data << 'Tylenol' if tylenol?
    data << 'Ibuprofen' if ibuprofen?
    data.join(', ')
  end

  def formatted_home_phone
    phone = home_phone.tr('^0-9', '')
    area_code = phone.length == 10
    h.number_to_phone(phone, area_code: area_code)
  end

  def formatted_physician_address
    html = span_tag(physician)
    html += span_tag(physician_address)
    html += span_tag("#{physician_city} #{physician_state} #{physician_zip}")
    html += span_tag(physician_phone)
    html.html_safe
  end

  def formatted_dentist_address
    html = span_tag(dentist)
    html += span_tag(dentist_address)
    html += span_tag("#{dentist_city} #{dentist_state} #{dentist_zip}")
    html += span_tag(dentist_phone)
    html.html_safe
  end

  def formatted_insurance_info
    html = ''
    html += span_tag("Company: #{insurance_company}") if insurance_company.present?
    html += span_tag("Plan: #{insurance_plan}") if insurance_plan.present?
    html += span_tag("Group: #{insurance_group}") if insurance_group.present?
    html += span_tag("Hospital: #{hospital}") if hospital.present?
    html.html_safe
  end

  def state_id_number
    state_number&.number
  end

  def external_id_number
    school_number&.number
  end

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

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

    def span_tag(str)
      "<span class='d-block'>#{str}</span>"
    end
end
