module AdminHelper

  def administration_steps
    {
      info: [:administrators, :dates, :description, :poster],
      payment: [:payment_message, :suggested_amounts, :paypal],
      registration: [:registration_status, :stats, :registrations, :broadcast],
      housing: [:providers, :housing],
      events: [:locations, :meals, :events],
      schedule: [:workshop_times, :schedule, :publish_schedule]
    }
  end

  def administration_sub_steps
    {
      location_edit: :locations,
      event_edit: :events
    }
  end

  def get_administration_group(administration_step)
    admin_step = administration_step.to_sym
    admin_step = administration_sub_steps[admin_step] if administration_sub_steps[admin_step].present?
    administration_steps.each do | group, steps |
      steps.each do | step |
        return group if step == admin_step
      end
    end

    return nil
  end

  def broadcast_to(to, conference = nil)
    conference ||= @this_conference || @conference

    users = []
    
    case to.to_sym
    when :registered
      ConferenceRegistration.where(conference_id: conference.id).each do | r |
        users << r.user if ((r.steps_completed || []).include? 'questions') && r.user.present? && r.is_attending != 'n'
      end
    when :pre_registered
      ConferenceRegistration.where(conference_id: conference.id).each do | r |
        users << r.user if registration_status(r) == :preregistered && r.is_attending != 'n'
      end
    when :workshop_facilitators
      user_hash = {}
      Workshop.where(conference_id: conference.id).each do | w |
        w.active_facilitators.each do | u |
          user_hash[u.id] ||= u if u.present?
        end
      end
      users = user_hash.values
    when :unregistered
      ConferenceRegistration.where(conference_id: conference.id).each do | r |
        users << r.user if registration_status(r) == :unregistered && r.is_attending != 'n'
      end
    when :housing_providers
      ConferenceRegistration.where(conference_id: conference.id, can_provide_housing: true).each do | r |
        users << r.user if r.user.present?
      end
    when :guests
      ConferenceRegistration.where(conference_id: conference.id, housing: 'house').each do | r |
        users << r.user if r.user.present? && r.is_attending != 'n'
      end
    when :all
      User.all.each do | u |
        users << u if u.present? && (u.is_subscribed.nil? || u.is_subscribed)
      end
    end
    
    return users
  end

  def get_housing_match(host, guest, space)
    housing_data = guest.housing_data || []
    
    if housing_data['host'].present?
      if housing_data['host'] == host.id
        return space == housing_data['space'] ? :selected_space : :other_space
      end

      return :other_host
    end

    if space_matches?(space, guest.housing) && available_dates_match?(host, guest)
      return :good_match
    end

    return :bad_match
  end

  def get_workshop_match(workshop, day, block, location)
    if workshop.event_location_id.present? && workshop.present?
      if (Date.parse params[:day]).wday == workshop.block['day'] && block == workshop.block['block'].to_i
        return :selected_space
      end

      if location.present? && location.id == workshop.event_location_id
        return :other_space
      end

      return :other_host
    end

    if location.present?
      needs = JSON.parse(workshop.needs || '[]').map &:to_sym
      amenities = JSON.parse(location.amenities || '[]').map &:to_sym

      if (needs & amenities).length < needs.length
        return :bad_match
      end
    end

    (((((@schedule[@day] || {})[:times] || {})[@time] || {})[:item] || {})[:workshops] || {}).each do | l, w |
      if w[:workshop].id != workshop.id
        f_a = w[:workshop].active_facilitators.map { | f | f.id }
        f_b = workshop.active_facilitators.map { | f | f.id }
        if (f_a & f_b).present?
          return :bad_match
        end
      end
    end

    # housing_data = guest.housing_data || []
    
    # if housing_data['host'].present?
    #   if housing_data['host'] == host.id
    #     return space == housing_data['space'] ? :selected_space : :other_space
    #   end

    #   return :other_host
    # end

    # if space_matches?(space, guest.housing) && available_dates_match?(host, guest)
    #   return :good_match
    # end

    # return :bad_match
    return :good_match
  end

  def space_matches?(host_space, guest_space)
    return false unless host_space.present? && guest_space.present?

    if host_space.to_s == 'bed_space' || host_space.to_s == 'floor_space'
      return guest_space.to_s == 'house'
    end

    return host_space.to_s == 'tent_space' && guest_space.to_s == 'tent'
  end

  def available_dates_match?(host, guest)
    return false unless host.housing_data['availability'].present? && host.housing_data['availability'][1].present?
    if host.housing_data['availability'][0] <= guest.arrival &&
      host.housing_data['availability'][1] >= guest.departure
      return true
     end

     return false
  end
end
