#! /usr/bin/env python
#
# spot -- Subaru Planning and Observation Tools
#
import sys
import os
from argparse import ArgumentParser

if 'CONFHOME' in os.environ:
    spot_home = os.path.join(os.environ['CONFHOME'], 'spot')
else:
    spot_home = os.path.join(os.path.expanduser('~'), '.spot')

from ginga.util import paths
paths.set_home(spot_home)
from ginga.rv import main as g_main
from ginga.misc.Bunch import Bunch

import spot.plugins
import spot.icons
icondir = os.path.split(spot.icons.__file__)[0]
paths.set_icon(os.path.join(icondir, "spot.svg"))

default_layout = ['seq', {},
                   ['vbox', dict(name='top', width=1400, height=700),  # noqa
                    dict(row=['ws', dict(name='menubar', wstype='stack',
                                         group=99)],
                         stretch=0),
                    dict(row=['hpanel', dict(name='hpnl'),
                     ['vbox', dict(name='main', width=600),
                      dict(row=['ws', dict(name='channels', wstype='tabs',
                                           group=1, use_toolbar=False,
                                           default=True),
                                 ],
                           stretch=1),
                      # dict(row=['ws', dict(name='cbar', wstype='stack',
                      #                      group=99)], stretch=0),
                      dict(row=['ws', dict(name='readout', wstype='stack',
                                           group=99)], stretch=0),
                      dict(row=['ws', dict(name='operations', wstype='stack',
                                           group=99)], stretch=0),
                      ],
                     ['ws', dict(name='right', wstype='tabs',
                                 width=400, height=-1, group=2),
                      # (tabname, layout), ...
                      [("Dialogs", ['ws', dict(name='dialogs', wstype='tabs',
                                               group=2)
                                    ]
                        )]
                      ],
                     ], stretch=1),  # noqa
                    dict(row=['ws', dict(name='toolbar', wstype='stack',
                                         height=40, group=2)],
                         stretch=0),
                    dict(row=['hbox', dict(name='status')], stretch=0),
                    ]]


if __name__ == "__main__":

    # Tweak the sys.path here if you are loading plugins from some
    # area outside your PYTHONPATH
    pluginHome = os.path.split(sys.modules['spot.plugins'].__file__)[0]
    sys.path.insert(0, pluginHome)

    # construct viewer builder
    viewer = g_main.ReferenceViewer(layout=default_layout,
                                    appname='spot', channels=[])

    # Bring in Ginga plugins that may be useful in SPOT
    for modname, remap in [('Operations', dict(workspace='operations')),
                           # ('Colorbar', dict(workspace='colorbar')),
                           ('Cursor', dict(workspace='readout')),
                           ('Errors', dict(workspace='right')),
                           ('Downloads', dict(workspace='dialogs')),
                           ('Command', dict(workspace='dialogs')),
                           ('Log', dict(workspace='right')),
                           ('ScreenShot', dict(workspace='dialogs')),
                           ('ColorMapPicker', dict(workspace='dialogs')),
                           ('Histogram', dict(workspace='dialogs')),
                           ('Preferences', dict(workspace='dialogs')),
                           ('FBrowser', dict(workspace='dialogs')),
                           # ('Pan', dict(workspace='dialogs', start=False)),
                           ('Header', dict(workspace='dialogs')),
                           ('Zoom', dict(workspace='dialogs')),
                           ('LoaderConfig', dict(workspace='channels'))]:
        specs = g_main.get_plugin_spec(module=modname)
        spec = specs[0]
        # remap to SPOT
        spec.update(remap)
        viewer.add_plugin_spec(spec)

    # add Spot plugins
    for spec in [
        Bunch(module='SPOTMenubar', ptype='global', workspace='menubar',
              category="System", hidden=True, start=True, enabled=True),
        Bunch(module='SPOTToolbar', ptype='global', workspace='toolbar',
              category="System", hidden=True, start=True, enabled=True),
        Bunch(module='CPanel', ptype='global', workspace='right',
              category="Planning", menu="Spot Control Panel", tab='CPanel',
              start=True, enabled=True),
        Bunch(module='FindImage', ptype='local', workspace='dialogs',
              category="Planning", menu="FindImage", tab='FindImage',
              enabled=True),
        Bunch(module='InsFov', ptype='local', workspace='dialogs',
              category="Planning", menu="InsFov", tab='InsFov', enabled=True),
        Bunch(module='PolarSky', ptype='local', workspace='dialogs',
              category="Planning", menu="PolarSky", tab='PolarSky',
              enabled=True),
        Bunch(module='SkyCam', ptype='local', workspace='dialogs',
              category="Planning", menu="SkyCam", tab='SkyCam', enabled=True),
        Bunch(module='TelescopePosition', ptype='local', workspace='channels',
              category="Planning", menu="Telescope Position", tab='TelPos',
              enabled=True),
        Bunch(module='Targets', ptype='local', workspace='channels',
              category="Planning", menu="Targets", tab='Targets', enabled=True),
        Bunch(module='Visibility', ptype='local', workspace='channels',
              category="Planning", menu="Visibility", tab='Visibility',
              enabled=True),
        Bunch(module='RotCalc', ptype='local', workspace='channels',
              category="Planning", menu="RotCalc", tab='RotCalc',
              enabled=True),
        Bunch(module='SiteSelector', ptype='local', workspace='dialogs',
              category="Planning", menu="Site Selector", tab='Site',
              enabled=True),
        Bunch(module='HSCPlanner', ptype='local', workspace='channels',
              category="Planning", menu="HSC Planner", tab='HSC Planner',
              enabled=True),
        ]:
        viewer.add_plugin_spec(spec)

    argprs = ArgumentParser(description="Subaru Planning and Observation Tools")
    viewer.add_default_options(argprs)

    (options, args) = argprs.parse_known_args(sys.argv[1:])

    if options.display:
        os.environ['DISPLAY'] = options.display

    viewer.main(options, args)
