###############################################################################
# File:        guProdOrdersListView.py
# Appointment: List of production orders
# Project:     GalaxyU
# Author:      AR
# History:
# 09.01.2014 Created
###############################################################################
import wxTools
import guObjects
import guApp
import guGroundBattle
import guListView
import guPlanetView

import wx
import zx


#===============================================================================
# List control
#===============================================================================
class CProdOrdersViewCtrl(guListView.CListViewCtrlWithBuilder):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        guListView.CListViewCtrlWithBuilder.__init__(self, parent, aListCreator)
        
    #---------------------------------------------------------------------------
    def SortData(self, aItems):
        # Initial sorting by priority inside order id
        aItems.sort(key = lambda x: x.OrderId)
        aItems.sort(key = lambda x: -x.Priority)

    #---------------------------------------------------------------------------
    # Redefined virtual
    def PrepareBuilder(self, aBuilder):
        # Add columns
        aBuilder.AddColumn(\
            "Nation", lambda data: str(data.NationName))
        aBuilder.AddColumn(\
            "Colony",
            lambda data: \
                guApp.Application.GetReport().GetStarName(data.StarId) if data.StarId != 0 else "")
        aBuilder.AddColumn(\
            "Id", lambda data: str(data.OrderId), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Type", lambda data: data.OrderType)
        aBuilder.AddColumn(\
            "Priority", lambda data: str(data.Priority), wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Ordered",
            lambda data: \
                "{0:.2f}".format(data.OrderedAmount) if data.OrderedAmount != -1 else \
                guObjects.NA, wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Produced",
            lambda data: \
                "{0:.2f}".format(data.ProducedAmount) if data.ProducedAmount != -1 else \
                guObjects.NA, wx.LIST_FORMAT_RIGHT, True)
        aBuilder.AddColumn(\
            "Status", lambda data: data.Status)
        aBuilder.AddColumn(\
            "Info", lambda data: data.Info)
#-------------------------------------------------------------------------------

#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Returns a list of orders for a star
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def GetStarProdOrders(aStarId):
    return zx.GetSubListFor(\
        guApp.Application.GetReport().ProdOrders,
        lambda o: o.StarId == aStarId)
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Shows a list of orders
# aTitle - window title
# aStarId where the orders assigned to. If 0 - all orders
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def ShowProdOrdersList(aTitle, aStarId = 0):
    return guApp.Application.WindowsCollection.RegisterAndShow(\
        lambda: guListView.CListViewFrame(\
            guApp.Application.GetMainWindow(), 
            lambda: guApp.Application.GetReport().ProdOrders if aStarId == 0 else GetStarProdOrders(aStarId),
            lambda panel, array: CProdOrdersViewCtrl(panel, array),
            "CProdOrdersViewCtrl", 
            aTitle))
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
