from typing import List
from typing import Tuple

import matplotlib.pyplot as plt
import numpy
from matplotlib.figure import Figure


def plot_CDI_align(
    xs: List[numpy.ndarray],
    intensities: List[numpy.ndarray],
    intensity_fits: List[numpy.ndarray],
    labels: List[str],
    offset_x: numpy.ndarray,
    offset_y: numpy.ndarray,
    offset_fit: numpy.ndarray,
    axis_name: str,
    counter: str,
    norm: str = "log",
) -> Tuple[Figure, Figure]:
    """
    from a number of alignment scans at different points on a rocking curve
    determine the location of the COR relative to the current position
    """

    motor_pos_fig, ax1 = plt.subplots(figsize=(16, 8))
    for x, intensity, intensity_fit, label, x0 in zip(
        xs, intensities, intensity_fits, labels, offset_y
    ):
        ax1.plot(x, intensity, label=label)
        ax1.plot(x, intensity_fit, "--r", label=f"fit - {label}")
        ax1.vlines(x0, 0, intensity.max())

    if norm == "log":
        ax1.semilogy()
    ax1.legend(loc=1)
    ax1.set_xlabel(axis_name)
    ax1.set_ylabel(counter)

    offset_fig, ax2 = plt.subplots(figsize=(16, 8))
    ax2.plot(offset_x, offset_y)
    ax2.plot(offset_x, offset_y, "o", label="original data")
    ax2.plot(offset_x, offset_fit, "r", label="fitted line")
    ax2.legend()

    return motor_pos_fig, offset_fig
