# -*- coding: utf-8 -*-

# -----------------------------------------------------------------------------
# Copyright (c) 2016-2017 Anaconda, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------

"""Qt utilities."""

from __future__ import annotations

__all__ = ['qapplication']

import os

from qtpy.QtCore import QTimer
from qtpy.QtWidgets import QApplication

from navigator_updater.utils.linux_scaling import setup_scale_factor_for_linux


def qapplication(test_time: int = 60) -> QApplication:
    """Create QApplication instance."""
    result: QApplication | None = QApplication.instance()

    if result is None:
        setup_scale_factor_for_linux()
        result = QApplication(['Anaconda-Navigator'])
        result.setApplicationName('Anaconda-Navigator')

    if os.environ.get('TEST_CI', None) is not None:
        timer_shutdown: QTimer = QTimer(result)
        timer_shutdown.timeout.connect(result.quit)
        timer_shutdown.start(test_time * 1000)

    return result
