"""Portrait FacilityUI911 queue bounds from the generated facility slot count."""
from __future__ import annotations

from .ui_layout import LayoutError, Rect


def source_queue_hint(context, *, move_visible=False, upgrade_visible=False,
                      description_height=None, maximum=7, queue_count=None):
    """Return the existing ring observer's source_grid contract for portrait.

    ``queue_count`` comes from the facility's current level and source slot
    table, not the number of circles a frame happened to detect. ``maximum``
    is only a bootstrap search envelope before that level is available.
    Landscape keeps its existing rule pack.
    Description height and optional buttons share the source life parent rule.
    """
    if context.orientation != "portrait":
        return None
    if type(maximum) is not int or not 1 <= maximum <= 7:
        raise LayoutError("portrait queue search maximum must be1..7")
    if queue_count is not None and (type(queue_count) is not int or not 1 <= queue_count <= maximum):
        raise LayoutError("portrait facility queue_count must be1..maximum")
    # Import at call time: the shared owner also uses these queue measurements
    # when placing portrait recipe lists and the GetAll button.
    from .source_life_routes import _portrait_progress
    count = maximum if queue_count is None else queue_count
    state = dict(queue_count=count, move_visible=move_visible,
                 upgrade_visible=upgrade_visible)
    if description_height is not None:
        state["description_height"] = description_height
    safe, scale = context.safe_rect, context.scale
    top, height, parent_metadata = _portrait_progress(context, state, safe, "processing.queue.observe")
    # 911 PC1670..1756: progress padding0/32; button120; hgap16.
    # PC336/2061: queue width=progress.width-progressBtn.width-80.
    # PC2089..2107: nested flow bottom-aligns equal-height cells, gap10.
    # Outer progress valign=top (PC1707), so its first row stays at this top
    # when fewer than maximum rings are generated. 912 cells are100x100.
    # The description is content-height, source-bounded34..48, not always48.
    # Observe that finite vertical envelope without calculating input points.
    earliest_top = top - 14*scale if description_height is None else top
    bounds = Rect(safe.left + (32 + 120 + 16) * scale, earliest_top,
                  safe.width - (120 + 80) * scale, height+top-earliest_top)
    if not safe.contains(bounds):
        raise LayoutError("portrait queue search envelope is outside the safe area")
    metadata = dict(parent_metadata)
    metadata["queue_count"] = queue_count
    metadata.update(source_functions=(911, 912),
        source_body_sha256="0818fabfbf22ce08bbe5be17bd33b44ffe2c29a1812b4dd3654c851157d98a51",
        source_cell_sha256="aae4e067ea2544e22c8b43fa94c6fa87230ad5184da4d857bd12496b59c11442",
        maximum_meaning="bootstrap search bound only; current facility level supplies actual slot count",
        candidate_assumptions=parent_metadata["anchor_candidate_assumptions"],
        runtime_click_approved=False, role_scope="read-only queue ring observation")
    return dict(bounds=bounds.bounds, cell_size=(100 * scale, 100 * scale),
                stride=(110 * scale, 110 * scale), maximum=maximum,
                client_size=(context.viewport.width, context.viewport.height), queue_count=queue_count,
                row_anchor="top", row_top_bounds=(earliest_top, top), metadata=metadata)
