#!/usr/bin/python3

# SPDX-FileCopyrightText: 2024 European Southern Observatory (ESO)
#
# SPDX-License-Identifier: LGPL-3.0-only

"""
@file
@ingroup trex_gui
@copyright ESO - European Southern Observatory

@brief trexenggui `main` source file

trex_engineering_gui GUI (or trexenggui), is part the main
GUI interface to the TREx system.
"""
import signal
import sys
import time
from taurus.core.util import argparse
from taurus.qt.qtgui.application import TaurusApplication
from trex_engineering_gui.applicationwindow import ApplicationWindow

int_signal_times: int = 0


def int_signal_handler(signum, frame):
    global int_signal_times
    print("\nSIGINT signal detected!")
    qApp.exit(signum)
    int_signal_times += 1
    if int_signal_times > 1:
        print("\nSIGINT signal detected more than once, forcing exit!")
        os._exit(signum)


if __name__ == "__main__":

    description = (
        "CUT RAD Hello GUI (or cutRadHello), is an \n"
        + "application developed for the E-ELT project."
    )

    parser = argparse.get_taurus_parser()
    parser.set_description(description)
    parser.add_option(
        "-p",
        "--port",
        dest="port",
        help="Changes the network port used to connect to the RAD Hello Application",
        default=14001,
    )
    parser.add_option(
        "-H",
        "--host",
        dest="host",
        default="localhost",
        help="Changes the network host used to connect to the RAD Hello Application",
    )
    parser.add_option(
        "-v",
        "--verbose",
        dest="verbose",
        action="store_true",
        default=False,
        help="Changes the LogLevel of the application to Debug. Output more information on application procedures.",
    )
    parser.add_option(
        "-V",
        "--extra-verbose",
        dest="extraverbose",
        action="store_true",
        default=False,
        help="Changes the LogLevel of the application to Trace. Outputs everything.",
    )

    parser, options, args = argparse.init_taurus_args(parser=parser)

    app = TaurusApplication(
        app_name="TREx Engineering GUI",
        app_version="0.9.0",
        org_name="ESO",
        org_domain="eso.org",
        cmd_line_parser=parser,
    )

    # taurus.setLogLevel(getattr(taurus, log_level))
    window = ApplicationWindow()

    window.show()

    # Handle SIGINT - this code ensures
    # exiting of the application on SIGINT
    signal.signal(signal.SIGINT, signal.SIG_DFL)
    # Start and Exit Taurus Application properly
    app.exec_()
    sys.exit(app.exit())

if __name__ == "__main__":
    trexenggui()
