def draw_panel(self, surf):
    """Draw the unit selection or build queue."""

    left = -14  # How far from the right border
    line = itertools.count(3)

    def unit_name(unit_type):
      return self._static_data.units.get(unit_type, "<unknown>")

    def write(loc, text, color=colors.yellow):
      surf.write_screen(self._font_large, color, loc, text)
    def write_line(x, *args, **kwargs):
      write((left + x, next(line)), *args, **kwargs)

    def write_single(unit):
      """Write a description of a single selected unit."""
      write_line(1, unit_name(unit.unit_type), colors.cyan)
      write_line(1, "Health: %s / %s" % (unit.health, unit.max_health))
      if unit.max_shields:
        write_line(1, "Shields: %s / %s" % (unit.shields, unit.max_shields))
      if unit.max_energy:
        write_line(1, "Energy: %s / %s" % (unit.energy, unit.max_energy))
      if unit.build_progress > 0:
        write_line(1, "Progress: %d%%" % (unit.build_progress * 100))
      if unit.transport_slots_taken > 0:
        write_line(1, "Slots: %s" % unit.transport_slots_taken)

    def write_multi(units):
      """Write a description of multiple selected units."""
      counts = collections.defaultdict(int)
      for unit in units:
        counts[unit_name(unit.unit_type)] += 1
      for name, count in sorted(counts.items()):
        y = next(line)
        write((left + 1, y), count)
        write((left + 3, y), name)

    ui = self._obs.observation.ui_data

    if ui.groups:
      write_line(0, "Control Groups:", colors.green)
      for group in ui.groups:
        y = next(line)
        write((left + 1, y), "%s:" % group.control_group_index, colors.green)
        write((left + 3, y), "%s %s" % (group.count,
                                        unit_name(group.leader_unit_type)))
      next(line)

    if ui.HasField("single"):
      write_line(0, "Selection:", colors.green)
      write_single(ui.single.unit)
      if (ui.single.attack_upgrade_level or
          ui.single.armor_upgrade_level or
          ui.single.shield_upgrade_level):
        write_line(1, "Upgrades:")
        if ui.single.attack_upgrade_level:
          write_line(2, "Attack: %s" % ui.single.attack_upgrade_level)
        if ui.single.armor_upgrade_level:
          write_line(2, "Armor: %s" % ui.single.armor_upgrade_level)
        if ui.single.shield_upgrade_level:
          write_line(2, "Shield: %s" % ui.single.shield_upgrade_level)
      if ui.single.buffs:
        write_line(1, "Buffs:")
        for b in ui.single.buffs:
          write_line(2, buffs.Buffs(b).name)
    elif ui.HasField("multi"):
      write_line(0, "Selection:", colors.green)
      write_multi(ui.multi.units)
    elif ui.HasField("cargo"):
      write_line(0, "Selection:", colors.green)
      write_single(ui.cargo.unit)
      next(line)
      write_line(0, "Cargo:", colors.green)
      write_line(1, "Empty slots: %s" % ui.cargo.slots_available)
      write_multi(ui.cargo.passengers)
    elif ui.HasField("production"):
      write_line(0, "Selection:", colors.green)
      write_single(ui.production.unit)
      next(line)
      if ui.production.production_queue:
        write_line(0, "Production:", colors.green)
        for item in ui.production.production_queue:
          specific_data = self._static_data.abilities[item.ability_id]
          if specific_data.remaps_to_ability_id:
            general_data = self._static_data.abilities[
                specific_data.remaps_to_ability_id]
          else:
            general_data = specific_data
          s = (general_data.friendly_name or general_data.button_name or
               general_data.link_name)
          s = s.replace("Research ", "").replace("Train ", "")
          if item.build_progress > 0:
            s += ": %d%%" % (item.build_progress * 100)
          write_line(1, s)
      elif ui.production.build_queue:  # Handle old binaries, no research.
        write_line(0, "Build Queue:", colors.green)
        for unit in ui.production.build_queue:
          s = unit_name(unit.unit_type)
          if unit.build_progress > 0:
            s += ": %d%%" % (unit.build_progress * 100)
          write_line(1, s)