###############################################################################
# File:        guFleetsView.py
# Appointment: Fleets viewer
# Project:     GalaxyU
# Author:      AR
# History:
# 05.01.2014 Created
###############################################################################
import wxTools
import guObjects
import guApp
import guListView
import guShipsView

import wx


#===============================================================================
# Fleets components list control
#===============================================================================
class CFleetsViewCtrl(guListView.CListViewCtrlWithBuilder):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        guListView.CListViewCtrlWithBuilder.__init__(self, parent, aListCreator)
        
    #---------------------------------------------------------------------------
    # Redefined virtual
    def PrepareBuilder(self, aBuilder):
        # Add columns
        aBuilder.AddColumn("", 
            lambda data: "*" if data.IsSystem else "", wx.LIST_FORMAT_LEFT, True)
        aBuilder.AddColumn("Name", 
            lambda data: ((data.NationName + "/") if guApp.Application.GetReport().IsGM() else "") + data.Name)
        aBuilder.AddColumn("Ships", 
            lambda data: str(data.NumberOfShips), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Total Mass", 
            lambda data: "{0:.2f}".format(data.TotalMass), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Speed", 
            lambda data: "{0:.2f}".format(data.Speed), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Location", 
            lambda data: guObjects.NA if data.IsSystem else ("in space" if data.StarName == "" else data.StarName))
        aBuilder.AddColumn("Assignment information", 
            lambda data: str(data.Assignment), wx.LIST_FORMAT_LEFT, True)

    #---------------------------------------------------------------------------
    # Redefined virtual
    def OnEdit(self, aPos):
        nation = self.GetItems()[self.GetSelectedDataItem()].NationName
        name = self.GetItems()[self.GetSelectedDataItem()].Name
        guShipsView.AllShipsList(\
            lambda ship: ship.NationName == nation and ship.FleetName == name,
            "Ships for " + nation + "/" + name)
        

    #---------------------------------------------------------------------------
    # Redefined virtual
    def GetPopupMenu(self):
        item = self.GetItems()[self.GetSelectedDataItem()]
        menu = wx.Menu()
        return menu if guApp.Application.GetReport().AddShipsJoiningFleetMenuItem(\
            self, menu, item.Name, item.NationName) else None
#-------------------------------------------------------------------------------


################################################################################
# List of fleets
def FleetsList(aFleetsListCreator, aTitle = ""):
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(), aFleetsListCreator,
            lambda panel, aListCreator: CFleetsViewCtrl(panel, aListCreator),
            "CFleetsViewFrame", aTitle))

