###############################################################################
# File:        guSpaceGroupsListView.py
# Appointment: Space groups viewer
# Project:     GalaxyU
# Author:      AR
# History:
# 19.01.2014 Created
###############################################################################
import wxTools
import guObjects
import guShipCalc
import guApp
import guListView
import guShipsView
import zx

import wx


#===============================================================================
# Space groupd list control
#===============================================================================
class CSpaceGroupsViewCtrl(guListView.CListViewCtrlWithBuilder):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        guListView.CListViewCtrlWithBuilder.__init__(self, parent, aListCreator)

        # Colour attributes
        self.__attrMyGroup = wx.ListItemAttr()
        self.__attrMyGroup.SetTextColour((0,0,200))
        self.__attrEnemyGroup = wx.ListItemAttr()
        self.__attrEnemyGroup.SetTextColour((150, 0, 0))
        self.__attrUnknownGroup = wx.ListItemAttr()
        self.__attrUnknownGroup.SetTextColour((150, 0, 150))
        

    #---------------------------------------------------------------------------
    # Redefined virtual
    def PrepareBuilder(self, aBuilder):
        # Add columns
        aBuilder.AddColumn("Id", 
            lambda data: str(data.Id), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Mass", 
            lambda data: "{0:.2f}".format(data.Mass) if data.Mass > 0 else guObjects.NA, wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Destination", 
            lambda data: \
                guApp.Application.GetReport().GetStarName(data.Destination) \
                if data.Destination != 0 else guObjects.NA)
        aBuilder.AddColumn("Speed", 
            lambda data: "{0:.2f}".format(data.Speed) if data.Speed != 0 else guObjects.NA, wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("ETA", 
            lambda data: str(data.ETA) if data.ETA != 0 else guObjects.NA, wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Location", 
            lambda data: data.GetLocationStr())
                               

    #---------------------------------------------------------------------------
    def OnGetItemAttr(self, item):
        elem = self.GetItems()[self.RowToItem(item)]
        return self.__attrMyGroup\
            if elem.IsMyGroup\
            else (self.__attrEnemyGroup if elem.Destination != 0 else self.__attrUnknownGroup)

    #---------------------------------------------------------------------------
    # Redefined virtual
    def OnEdit(self, aPos):
        self.GetItems()[self.GetSelectedDataItem()].ShowShips(self)


    #---------------------------------------------------------------------------
    # Redefined virtual
    def GetPopupMenu(self):
        menu = wx.Menu()
        mi = wx.MenuItem(menu, wx.NewId(), 'Locate the group on the map')
        self.Bind(wx.EVT_MENU,
            lambda event: guApp.Application.GetStarMap().ShowSpaceObject(\
                self.GetItems()[self.GetSelectedDataItem()].Id))
        menu.AppendItem(mi)
        self.GetItems()[self.GetSelectedDataItem()].AddShowShipsMenuItem(self, menu)
        return menu
#-------------------------------------------------------------------------------


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Shows a list of space groups
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# General function
def ShowSpaceGroups(aListCreator, aTitle = "All Space Groups", aCfgPostfix = ""):
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(),
            aListCreator,
            lambda panel, aListCreator: CSpaceGroupsViewCtrl(panel, aListCreator),
            "CSpaceGroupsViewCtrl" + aCfgPostfix, 
            aTitle))

# All groups
def ShowSpaceGroupsList(aFunc = None, aTitle = "All Space Groups", aCfgPostfix = ""):
    return ShowSpaceGroups(\
        lambda: zx.GetSubListFor(\
                    guApp.Application.GetReport().SpaceGroups,\
                    None if guApp.Application.GetReport().IsGM() else aFunc),
        aTitle, aCfgPostfix) 

# Own groups
def ShowOwnSpaceGroupsList():
    return ShowSpaceGroupsList(lambda sg: sg.IsMyGroup, "Approaching own Space Groups", "Own")

# Approaching groups
def ShowApproachingSpaceGroupsList():
    return ShowSpaceGroupsList(\
        lambda sg: not sg.IsMyGroup and sg.Destination != 0, "Approaching alien Space Groups", "Approaching")
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
