import typing

import PIL.Image

from toolri.toolri.controller import ToolRIController, ToolRIInstance, ToolRILabel


def toolri(
    image: PIL.Image.Image,
    data: typing.Union[None, typing.Union[None, list[dict]]] = None,
    labels: typing.Union[None, list[ToolRILabel]] = None,
) -> list[dict]:
    """
    ToolRI main function to call the GUI interface.

    Args:
        image (PIL.Image.Image): Image converted from the document to be labelled.
        data (typing.Union[None, typing.Union[None, list[dict]]], optional): Data generated by labeling with ToolRI. Empty data will be created if the parameter is `None`. Defaults to None.
        labels (typing.Union[None, list[ToolRILabel]], optional): List of `ToolRILabel`. Default labels will be created if the parameter is `None`. Defaults to None.

    Returns:
        list[dict]: Data generated by labeling with ToolRI.
    """

    # data is empty if not passed
    if data is None:
        data = []

    # init ToolRI GUI
    data = ToolRIController(
        image=image, data=data, toolri_instance=ToolRIInstance.PACKAGE, labels=labels
    ).run()
    return data


if __name__ == "__main__":
    """
    Run ToolRI GUI as a standalone application.
    """

    data = ToolRIController(
        image=None, data=None, toolri_instance=ToolRIInstance.BINARY, labels=None
    ).run()
