import logging
import sys

from PyQt6.QtWidgets import QApplication

from app import constants as c
from app.gui.controller import DialogController, ExceptionHandler
from app.utils.ffmpeg import check_ffmpeg

logging_level = logging.getLevelName(c.LOGGING_LEVEL)
logging.basicConfig(level=logging_level)


def _configure_exception_handler(controller: DialogController) -> None:
    """
    Configure exception handler for application.
    Now all exceptions will be handled by application and shown in error dialog.
    :param controller: dialog controller
    :return:
    """
    exception_handler = ExceptionHandler()
    exception_handler.errorSignal.connect(controller.show_error)
    sys._excepthook = sys.excepthook
    sys.excepthook = exception_handler.handler


def _start_app() -> None:
    """
    Entry point for application execution.
    :return:
    """
    app = QApplication(sys.argv)
    controller = DialogController()
    _configure_exception_handler(controller)
    check_ffmpeg()
    controller.start()
    return_code = app.exec()
    sys.exit(return_code)


if __name__ == "__main__":
    _start_app()
