###############################################################################
# File:        guRatingListView.py
# Appointment: List nations rating
# Project:     GalaxyU
# Author:      AR
# History:
# 16.01.2014 Created
###############################################################################
import wxTools
import guObjects
import guApp
import guListView

import wx


#===============================================================================
# List control
#===============================================================================
class CRatingViewCtrl(guListView.CListViewCtrlWithBuilder):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        guListView.CListViewCtrlWithBuilder.__init__(self, parent, aListCreator)

    #---------------------------------------------------------------------------
    # Redefined virtual
    def PrepareBuilder(self, aBuilder):

        # Formatting function
        def FormatStr(aVal, aDelta, aIsInt = False):
            def FmtVal(a):
                return str(a) if aIsInt else "{0:.2f}".format(a);
            return (FmtVal(aVal) + \
                ("" if aDelta == 0 else ("(" + ("" if aDelta < 0 else "+") + \
                FmtVal(aDelta) + ")"))) if aVal != guObjects.CRatingData.RATING_NA else guObjects.NA

        
        # Add columns
        aBuilder.AddColumn("Nation",
            lambda data: data.Nation)
        aBuilder.AddColumn("Dipl. State",
            lambda data: guApp.Application.GetReport().GetDiplomaticState(data.Nation))
        aBuilder.AddColumn("Index",
            lambda data: str(data.RatingIndex), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Rating",
            lambda data: FormatStr(data.Rating, data.RatingDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Age",
            lambda data: str(guApp.Application.GetReport().Turn - data.Created), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Ruler Title",
            lambda data: data.GetRuler().Title)
        aBuilder.AddColumn("Ruler Name",
            lambda data: data.GetRuler().Name)
        aBuilder.AddColumn("Galaxy Title",
            lambda data: data.GetRuler().GalaxyRulerTitle)
        aBuilder.AddColumn("Population",
            lambda data: FormatStr(data.Population, data.PopulationDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Industry", 
            lambda data: FormatStr(data.Industry, data.IndustryDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Ships Number", 
            lambda data: FormatStr(data.ShipsNumber, data.ShipsNumberDelta, True), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Total Ships Mass", 
            lambda data: FormatStr(data.ShipsMass, data.ShipsMassDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Tech Drive", 
            lambda data: FormatStr(data.TechDrive, data.TechDriveDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Tech Weapon", 
            lambda data: FormatStr(data.TechWeapon, data.TechWeaponDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Tech Shield", 
            lambda data: FormatStr(data.TechShield, data.TechShieldDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Tech Cargo",
            lambda data: FormatStr(data.TechCargo, data.TechCargoDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Avg Tech", 
            lambda data: FormatStr(data.Tech, data.TechDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Colonies", 
            lambda data: FormatStr(data.ColoniesNumber, data.ColoniesNumberDelta, True), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Colonies Size", 
            lambda data: FormatStr(data.ColoniesTotalSize, data.ColoniesTotalSizeDelta, True), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Money", 
            lambda data: FormatStr(data.Money, data.MoneyDelta), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn("Debt", 
            lambda data: FormatStr(data.Debt, data.DebtDelta), wx.LIST_FORMAT_RIGHT, True)


    #---------------------------------------------------------------------------
    def OnGetItemAttr(self, item):
        return guApp.Application.GetReport().GetDiplomaticStateColourAttr(\
            self.GetItems()[self.RowToItem(item)].Nation)

#-------------------------------------------------------------------------------

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Shows a list of nation ratings
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def ShowRatingList():
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(),
            lambda: guApp.Application.GetReport().NationsRating,
            lambda panel, aListCreator: CRatingViewCtrl(panel, aListCreator),
            "CRatingViewCtrl", 
            "Nations Rating"))
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
