#! /usr/bin/env python
#
# spot -- Site Planning and Observation Tool
#
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
import spot.version as version

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=750),  # 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),
                                    ]
                        ),
                       ("Control", ['vpanel', {},
                                    ['ws', dict(name='uright', wstype='stack',
                                                height=250, group=3)],
                                    ['ws', dict(name='mright', wstype='stack',
                                                height=250, group=3)],
                                    ['ws', dict(name='lright', wstype='stack',
                                                height=250, group=3)],
                                    ]
                        ),
                       ],
                      ],
                     ], 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
    # special area
    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=[])
    viewer.add_separately_distributed_plugins()

    # 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', start=True)),
                           ('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='mright', start=False,
                                        category='Utils', hidden=False)),
                           ('Zoom', dict(workspace='lright', start=False,
                                        category='Utils', hidden=False)),
                           ('Header', dict(workspace='dialogs')),
                           ('LoaderConfig', dict(workspace='channels')),
                           ('PluginConfig', 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='SPOTToolbar', klass='SPOTToolbar_Ginga_Image',
              hidden=True, category='System', ptype='local',
              enabled=True, exclusive=False),
        Bunch(module='CPanel', ptype='global', workspace='uright',
              category="System", tab='CPanel',
              start=True, enabled=True, hidden=True),
        Bunch(module='SiteSelector', ptype='local', workspace='dialogs',
              category="Planning", menu="Site Selector", tab='Site',
              ch_sfx='_TGTS', index=0.0, enabled=True, exclusive=False),
        Bunch(module='PolarSky', ptype='local', workspace='dialogs',
              category="Planning", menu="PolarSky", tab='PolarSky',
              ch_sfx='_TGTS', index=1.0, enabled=True, exclusive=False),
        Bunch(module='Targets', ptype='local', workspace='channels',
              category="Planning", menu="Target List", tab='Targets',
              ch_sfx='_TGTS', index=2.0, enabled=True, exclusive=False),
        Bunch(module='Visibility', ptype='local', workspace='channels',
              category="Planning", menu="Visibility Plot", tab='Visibility',
              ch_sfx='_TGTS', index=3.0, enabled=True, exclusive=False),
        Bunch(module='SkyCam', ptype='local', workspace='dialogs',
              category="Planning", menu="Sky Cams", tab='SkyCam',
              ch_sfx='_TGTS', index=4.0, enabled=True, exclusive=False),
        Bunch(module='TargetGenerator', ptype='local', workspace='channels',
              category="Planning", menu="Target Generator", tab='TargetGen',
              ch_sfx='_TGTS', index=5.0, enabled=True, exclusive=False),
        Bunch(module='TelescopePosition', ptype='local', workspace='channels',
              category="Planning", menu="Telescope Position", tab='TelPos',
              ch_sfx='_TGTS', index=6.0, enabled=False, exclusive=False),
        Bunch(module='LGS', ptype='local', workspace='channels',
              category="Planning", menu="LGS", tab='LGS',
              ch_sfx='_TGTS', index=7.0, enabled=False, exclusive=True),
        Bunch(module='FindImage', ptype='local', workspace='dialogs',
              category="Planning", menu="Finding Chart", tab='FindImage',
              ch_sfx='_FIND', index=10.0, enabled=True, exclusive=False),
        Bunch(module='InsFov', ptype='local', workspace='dialogs',
              category="Planning", menu="Instrument FOV", tab='InsFov',
              ch_sfx='_FIND', index=11.0, enabled=True, exclusive=False),
    ]:
        viewer.add_plugin_spec(spec)

    argprs = ArgumentParser(description="Site Planning and Observation Tools")
    viewer.add_default_options(argprs)
    argprs.add_argument('-V', '--version', action='version',
                        version='%(prog)s {}'.format(version.version))

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

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

    viewer.main(options, args)
