#!/usr/bin/python3
# @file
# @ingroup ifw-ccfui_gui
# @copyright
#   SPDX-FileCopyrightText: 2026 European Southern Observatory (ESO)
#   SPDX-License-Identifier: LGPL-3.0-only
#
# @brief ccfGui `main` source file

"""
@author Diego del Valle <ddelvall@eso.org>


The gui is a dummy program whose only purpose is to show a reference implementation of
the ESO ELT ICS style and guidelines.
"""
import sys


from taurus.core.util.log import Logger
from taurus.external.qt.Qt import QFile, QTextStream
from taurus.qt.qtgui.application import TaurusApplication
import click

from ifw.ccf.gui.ccfguimainwindow import CcfGuiMainWindow
import ifw.core.stooUtils.consul as consul_utils
from ifw.core.utils.utils import find_file


@click.command()
@click.option('-n', '--name', default='ccf-req', help='Registered CCF name in Consul')
@click.option('-u', '--uri', default='none', help='Service URI. E.g. zpb.rr://127.0.0.1:12091')
@click.option('-l', '--log-level', default='ERROR', help='Debugging level',
              type=click.Choice(['ERROR', 'INFO', 'DEBUG'], case_sensitive=False))
@click.option('-r','--style_resource', default='Default', help='Set built-in stylesheet resource',
                type=click.Choice(['Default', 'Combinear', 'Diffnes','Takezo'],
                case_sensitive=True))
@click.option('-d', '--remote-ddt-uri', required=False, multiple=True,
              help='Use remote DDT (uri), can be used multiple times. \nE.g. --remote-ddt-uri zpb.rr://192.168.0.100:12091/broker/Broker1')
@click.option('-p', '--publisher-ddt', required=False, multiple=True,
              help='DDT Publisher name to assign the remote ddt, can be used multiple times\n E.g.  --publisher-ddt pub_1_1_ddt')
def main(name, uri, log_level, remote_ddt_uri, publisher_ddt, style_resource):
    """
    ccfGui is a generic solution to connect to any CCF based camera. 
    It allows to be connected via URI (--uri) or by NOMAD service 
    request reply name (--name).

    The usual way of using this GUI would be:

    e.g. 1:

        ccfGui --uri zpb.rr://127.0.0.1:12091

    e.g. 2:

        ccfGui --name ccfNomadTask-req

    Remote publisher DDT are supported, and can be used by giving the
    DDT ID (--publisher-ddt) together with the remote broker uri 
    (--remote-ddt-uri). Be aware that both parameters can be repeated 
    as much as needed, but the user needs to make sure that both
    parameters appear the same ammount of time. The assigment is based
    on the order of the parameters.

    For example, the next command will assign the broker at URI 
    'zpb.rr://192.168.1.1:12081/broker/broker1' to publisher 'pub_1_1_ddt'
    and broker at URI 'zpb.rr://192.168.1.1:12082/broker/broker2 to 
    publisher pub_1_2_ddt:

        ccfGui --uri zpb.rr://127.0.0.1:12091 -l DEBUG -p pub_1_1_ddt 
    -d zpb.rr://192.168.1.1:12081/broker/broker1 -p pub_1_2_ddt -d
    zpb.rr://192.168.1.1:12082/broker/broker2

    """

    try:
        if uri == "none":
            cons = consul_utils.ConsulClient()
            uri = cons.get_uri(name)
    except Exception as e:
        print(e)
        raise SystemExit


    app = TaurusApplication(
        app_name="ccfGui",
        app_version="1.0.0",
        org_name="ESO",
        org_domain="eso.org"
    )

    # Handling of remote DDT broker  
    if len(remote_ddt_uri) != len(publisher_ddt):
        print("Option --remote-ddt-uri and --publisher-ddt must have the same amount of values")
        exit(1)

    remote_ddt_dic = {}
    for remote_uri, pub_name in zip(remote_ddt_uri, publisher_ddt):
         remote_ddt_dic[pub_name] = remote_uri

    window = CcfGuiMainWindow(uri, log_level, remote_ddt_dic)
    window.menu_set_stylesheet(style_resource)
    window.setWindowTitle("CCF GUI")
    window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
