from typing import Dict
from typing import Tuple
from typing import Literal
from typing import List


def place_colorstrip(
    anchor_coords: Dict[str, Tuple[float, float]],
    width: float,
    height: float,
    spacing: float,
    loc: Literal["left", "right", "up", "down", "polar"],
) -> Tuple[
    Dict[str, Tuple[List[float], List[float]]], Dict[str, Tuple[float, float]]
]:
    """Compute the coordinates of the boxes that represent a colorstrip.

    This function computes the x and y coordinates (or the angles and radii)
    of colored boxes, which together form a colorstrip used to annotate leaves
    of a tree.

    Args:
        anchor_coords: Dictionary of nodes-to-coordinate tuples that contain
            the "anchor" point to start the colorstrip. When `loc=left`, this
            is the center right of each box, when `loc=right`, this is the
            center left of each box, etc.
        width: Width of the box. The width is defined as the length of the
            box in the same direction as the leaves.
        height: Height of the box. The height is defined as the length of the
            box in the direction perpendicular to the leaves.
        spacing: Space between consecutive colorstrips. This value is used as a
            padding before placing the box.
        loc: Where to place each box relative to the anchors. Valid options are:
            `left`, `right`, `up`, `down`, `polar`.

    Returns:
        A dictionary of node-to-coordinate tuples for each box, and a dictionary
            of the next set of anchor coordinates (for placing more colorstrips)
    """
    next_anchor_coords = {}
    size_x, size_y = (
        (width, height) if loc in ("left", "right") else (height, width)
    )
    coef_x, coef_y = 0, 1  # default: up / polar
    if loc == "left":
        coef_x, coef_y = -1, 0
    elif loc == "right":
        coef_x, coef_y = 1, 0
    elif loc == "down":
        coef_x, coef_y = 0, -1

    boxes = {}
    for anchor, (x, y) in anchor_coords.items():
        next_anchor_coords[anchor] = (
            x + coef_x * (size_x + spacing),
            y + coef_y * (size_y + spacing),
        )
        center_x, center_y = (
            x + coef_x * ((size_x / 2) + spacing),
            y + coef_y * ((size_y / 2) + spacing),
        )
        xs = [
            center_x + size_x / 2,
            center_x - size_x / 2,
            center_x - size_x / 2,
            center_x + size_x / 2,
            center_x + size_x / 2,
        ]
        ys = [
            center_y + size_y / 2,
            center_y + size_y / 2,
            center_y - size_y / 2,
            center_y - size_y / 2,
            center_y + size_y / 2,
        ]
        boxes[anchor] = (xs, ys)
    return boxes, next_anchor_coords