###############################################################################
# File:        guBattleListView.py
# Appointment: Space Battles list viewer
# Project:     GalaxyU
# Author:      AR
# History:
# 01.01.2014 Created (Happy New Year!)
###############################################################################
import wxTools
import guObjects
import guApp
import guBattle
import guListView

import wx


#===============================================================================
# Ship Types list control
#===============================================================================
class CSpaceBattleViewCtrl(wxTools.CListCtrl):
    # Internal battle desc
    class CBattleDesc:
        def __init__(self, aStar):
            self.Star = aStar
            self.Participants = ""    
    
    #---------------------------------------------------------------------------
    def __init__(self, parent):
        wxTools.CListCtrl.__init__(self, parent)
        
        # Create a list of battles
        self.__battlesList = []
        for star in guApp.Application.GetReport().VisibleStars:
            if star.HasBattle():
                desc = self.CBattleDesc(star)
                self.__battlesList.append(desc)
                # Find participants
                participants = {} # map <nation name: ships number, total mass)>
                for unit in star.BattleSequences[0].Units:
                    nation = unit.GetNation()
                    if not nation in participants:
                        participants[nation] = [0, 0]
                    participants[nation][0] += unit.Number
                    participants[nation][1] += unit.Mass * unit.Number
                for nation, data in participants.items():
                    desc.Participants += (", " if desc.Participants != "" else "") + nation + "(" + str(data[0]) + "/" + "{0:.2f}".format(data[1]) + ")"

        self.SetItemCount(len(self.__battlesList))
        
        # Add columns
        self.__builder = wxTools.CColDataBuilder()
        self.__builder.AddColumn("Star", lambda data: guApp.Application.GetReport().GetStarName(data.Star.Id))
        self.__builder.AddColumn("Participants: name(Number of Ships/Total Ships Mass)", lambda data: data.Participants)
        self.__builder.Prepare(self.__battlesList, self)

    #---------------------------------------------------------------------------
    # Redefined virtual
    def OnEdit(self, aPos):
        star = self.__battlesList[self.GetSelectedDataItem()].Star
        guBattle.ViewBattle(star, star.BattleSequences, {})
        self.SetFocus()


    #---------------------------------------------------------------------------
    # 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.__battlesList[self.GetSelectedDataItem()].Star.Id))
        menu.AppendItem(mi)
        return menu
#-------------------------------------------------------------------------------


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Shows a list of space battles for star
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def ShowSpaceBattles():
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(), lambda: [],
            lambda panel, array: CSpaceBattleViewCtrl(panel),
            "CSpaceBattleViewCtrl", 
            "All Space Battles"))
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
