#!/usr/bin/env python
#   This file is part of nexdatas - Tango Server for NeXus data writer
#
#    Copyright (C) 2014-2017 DESY, Jan Kotanski <jkotan@mail.desy.de>
#
#    nexdatas is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    nexdatas is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with nexdatas.  If not, see <http://www.gnu.org/licenses/>.
# \package nxsselector nexdatas
# \file nxsscan
# GUI for setting sardana scans with NeXus Recorder

""" NXS Scan - GUI for setting sardana scans with NeXus Recorder"""


import logging
import sys
import os

import nxsselector
from nxsselector.Selector import Selector
from nxsselector import __version__

import nxsselector.qrc as qrc


logger = logging.getLogger(__name__)


def main():
    """ the main function
    """

    if "GNOME_DESKTOP_SESSION_ID" not in os.environ:
        os.environ["GNOME_DESKTOP_SESSION_ID"] = "qtconfig"
    if os.path.isdir("/usr/lib/kde4/plugins/") and \
       "QT_PLUGIN_PATH" not in os.environ:
        os.environ["QT_PLUGIN_PATH"] = "/usr/lib/kde4/plugins/"
    try:
        from taurus.external.qt import Qt
    except Exception:
        from taurus.qt import Qt

    Qt.QCoreApplication.setAttribute(Qt.Qt.AA_X11InitThreads)
    Qt.QResource.registerResource(
        os.path.join(qrc.__path__[0], "resources.rcc"))

    import taurus.qt.qtgui.application
    Application = taurus.qt.qtgui.application.TaurusApplication

    app = Application.instance()
    standalone = app is None
    server = None
    expert = True
    umode = None
    setdefault = False
    door = None
    switch = True

    if standalone:
        import taurus.core.util.argparse
        parser = taurus.core.util.argparse.get_taurus_parser()

        parser.add_option(
            "-s", "--server", dest="server",
            help="selector server")
        parser.add_option(
            "-d", "--door", dest="door",
            help="door device name")
        parser.add_option(
            "-t", "--style", dest="style",
            help="Qt style")
        parser.add_option(
            "-y", "--stylesheet", dest="stylesheet",
            help="Qt stylesheet")
        parser.add_option(
            "-m", "--mode", dest="mode",
            help="interface mode, i.e. simple, user, advanced, "
            "special, expert")
        parser.add_option(
            "", "--set-as-default-mode",
            action="store_true",
            default=False,
            dest="setdefault",
            help="set the current mode as default")
        parser.add_option(
            "", "--dont-switch-mntgrp",
            action="store_false",
            default=True,
            dest="switch",
            help="do not switch MntGrp to the ActiveMntGrp")

        app = Application(
            sys.argv,
            cmd_line_parser=parser,
            app_name="NXS Component Selector",
            app_version=nxsselector.__version__,
            org_domain="desy.de",
            org_name="DESY")

        app.setWindowIcon(Qt.QIcon(":/configtools.png"))

        (options, _) = parser.parse_args()
        if options.style:
            app.setStyle(options.style)
        server = options.server
        if options.stylesheet:
            app.setStyle(options.stylesheet)
        if options.mode:
            umode = options.mode
        else:
            umode = None
        setdefault = options.setdefault
        door = options.door
        switch = options.switch

    form = Selector(server, standalone=True, umode=umode,
                    setdefault=setdefault,
                    door=door,switch=switch)
    form.show()

    if standalone:
        sys.exit(app.exec_())
    else:
        return form


if __name__ == "__main__":
    main()
