###############################################################################
# File:        guStarsListView.py
# Appointment: List of all visible stars
# Project:     GalaxyU
# Author:      AR
# History:
# 06.01.2014 Created
###############################################################################
import wxTools
import guObjects
import guApp
import guGroundBattle
import guListView
import guPlanetView
import zx

import wx


#===============================================================================
# Ship Types list control
#===============================================================================
class CStarsViewCtrl(guListView.CListViewCtrlWithBuilder):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        guListView.CListViewCtrlWithBuilder.__init__(self, parent, aListCreator)
                
    #---------------------------------------------------------------------------
    # Redefined virtual
    def PrepareBuilder(self, aBuilder):
        # Add columns
        aBuilder.AddColumn(\
            "Id", 
            lambda data: str(data.Id), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Name",
            lambda data: data.GetName())
        aBuilder.AddColumn(\
            "Galaxy Name",
            lambda data: "" if data.GalaxyName == "?" else data.GalaxyName)
        aBuilder.AddColumn(\
            "Owner",
            lambda data: \
                "[unknown]" if not data.IsInvestigated() \
                    else ("<uninhabited>" if data.Owner == "?" else data.Owner))
        aBuilder.AddColumn(\
            "Last Reported (years ago)", 
            lambda data: \
                str(data.CalcLastReportedAgo()) if not data.IsActualKnown() and \
                        data.IsInvestigated() and not data.IsOwn() else "0",
            wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Size",
            lambda data: str(data.Size), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Reached Max", 
            lambda data: "yes" if data.Population >= data.Size and data.Industry >= data.Size else "",
            wx.LIST_FORMAT_CENTER)
        aBuilder.AddColumn(\
            "Total Population", 
            lambda data: "{0:.2f}".format(data.Population), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Active Population", 
            lambda data: "{0:.4f}".format(data.CalcActivePopulation()), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Colonists", 
            lambda data: "{0:.2f}".format(data.CalcCol()), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Industry", 
            lambda data: "{0:.4f}".format(data.Industry), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Materials", 
            lambda data: "{0:.2f}".format(data.Materials), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Live cond.", 
            lambda data: "{0:.2f}".format(data.LiveConditions), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Resources",
            lambda data: "{0:.2f}".format(data.ResourceFactor), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Restricted",
            lambda data: "yes" if data.IsBanned() else "")
        aBuilder.AddColumn(\
            "Bombing prevented",
            lambda data: "yes" if data.IsBombingBanned() else "")


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


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


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Shows a list of stars
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# All stars
def ShowStarsList(aFunc = None, aTitle = "All Star Systems", aCfgPostfix = ""):
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(),
            lambda: zx.GetSubListFor(\
                guApp.Application.GetReport().VisibleStars,
                aFunc),
            lambda panel, aListCreator: CStarsViewCtrl(panel, aListCreator),
            "CStarsViewCtrl" + aCfgPostfix, 
            aTitle))

# Own colonies
def ShowYourColoniesList():
    return ShowStarsList(\
       lambda star:\
           star.Population > 0 if guApp.Application.GetReport().IsGM() else\
               star.Owner == guApp.Application.GetReport().NationName,
       "Your Colonies", "OwnColonies")

# Uninhabited systems
def ShowUninhabitedSystemsList():
    return ShowStarsList(\
       lambda star:\
           star.Population == 0 if guApp.Application.GetReport().IsGM()\
               else star.IsInvestigated() and star.IsActualKnown() and star.Population == 0,
       "Uninhabited systems", "Uninhabited")

# Alien colonies
def ShowAlienColoniesList():
    return ShowStarsList(\
       lambda star: star.Owner != guApp.Application.GetReport().NationName and star.Population > 0,
       "Alien Colonies", "Alien")

# Known systems
def ShowKnownSystemsList():
    return ShowStarsList(\
       lambda star: guApp.Application.GetReport().IsGM() or star.IsKnown() and not star.IsActualKnown(),
       "Known before star systems", "Known")

# Not investigated systems
def ShowNotInvestigatedSystemsList():
    return ShowStarsList(\
       lambda star: guApp.Application.GetReport().IsGM() or not star.IsInvestigated(),
       "Not investigated star systems", "NotInvestigated")
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
