--*****************************************************************************
--* File: lua/modules/ui/game/controlgroups.lua
--*
--* Copyright © 2006 Gas Powered Games, Inc.  All rights reserved.
--*****************************************************************************

local UIUtil = import('/lua/ui/uiutil.lua')
local LayoutHelpers = import('/lua/maui/layouthelpers.lua')
local GameMain = import('/lua/ui/game/gamemain.lua')
local Group = import('/lua/maui/group.lua').Group
local Button = import('/lua/maui/button.lua').Button
local Checkbox = import('/lua/maui/checkbox.lua').Checkbox
local Movie = import('/lua/maui/movie.lua').Movie
local Bitmap = import('/lua/maui/bitmap.lua').Bitmap
local GameCommon = import('/lua/ui/game/gamecommon.lua')
local Announcement = import('/lua/ui/game/announcement.lua').CreateAnnouncement
local Selection = import('/lua/ui/game/selection.lua')
local Tooltip = import('/lua/ui/game/tooltip.lua')

-------------------------------------------------------------------------------
-- Define some more valid groups, these replace the originals -----------------
-------------------------------------------------------------------------------

validGroups = {
    ['1'] = true,
    ['2'] = true,
    ['3'] = true,
    ['4'] = true,
    ['5'] = true,
    ['6'] = true,
    ['7'] = true,
    ['8'] = true,
    ['9'] = true,
    ['10'] = true,
}

groupOrder = {'1','2','3','4','5','6','7','8','9','10'}

-------------------------------------------------------------------------------
-- Replacement for the original OnSelectionSetChanged function ----------------
-------------------------------------------------------------------------------

-- Had to alter this because the set naming logic has changed.
function OnSelectionSetChanged(initialname, initialunitset, applied)
	-- mangle the battalion name
	local name = string.sub(initialname,1,1)
	--print('Calling with ' .. name)
	-- die if battalion name is not valid
    if not validGroups[name] then return end
	--print('Did not quit')
    local function CreateGroup(units, label)
        local bg = Bitmap(controls.container, UIUtil.SkinnableFile('/game/avatar/avatar-control-group_bmp.dds'))
        
        bg.icon = Bitmap(bg)
        bg.icon.Width:Set(28)
        bg.icon.Height:Set(20)
        LayoutHelpers.AtCenterIn(bg.icon, bg, 0, -4)
        
        bg.label = UIUtil.CreateText(bg.icon, label, 18, UIUtil.bodyFont)
        bg.label:SetColor('ffffffff')
        bg.label:SetDropShadow(true)
        LayoutHelpers.AtRightIn(bg.label, bg.icon)
        LayoutHelpers.AtBottomIn(bg.label, bg, 5)
        
        bg.icon:DisableHitTest()
        bg.label:DisableHitTest()
        
        bg.units = units
        
        bg.UpdateGroup = function(self)
            self.units = ValidateUnitsList(self.units)
            
            if table.getsize(self.units) > 0 then
                local sortedUnits = {}
                sortedUnits[1] = EntityCategoryFilterDown(categories.COMMAND, self.units)
                sortedUnits[2] = EntityCategoryFilterDown(categories.EXPERIMENTAL, self.units)
                sortedUnits[3] = EntityCategoryFilterDown(categories.TECH3 - categories.FACTORY, self.units)
                sortedUnits[4] = EntityCategoryFilterDown(categories.TECH2 - categories.FACTORY, self.units)
                sortedUnits[5] = EntityCategoryFilterDown(categories.TECH1 - categories.FACTORY, self.units)
                sortedUnits[6] = EntityCategoryFilterDown(categories.FACTORY, self.units)
                
                local iconID = ''
                for _, unitTable in sortedUnits do
                    if table.getn(unitTable) > 0 then
                        iconID = unitTable[1]:GetBlueprint().BlueprintId
                        break
                    end
                end
                
                if iconID != '' and DiskGetFileInfo('/textures/ui/common/icons/units/'..iconID..'_icon.dds') then
                    self.icon:SetTexture('/textures/ui/common/icons/units/'..iconID..'_icon.dds')
                else
                    self.icon:SetTexture('/textures/ui/common/icons/units/default_icon.dds')
                end
            else
                self:Destroy()
                controls.groups[self.name] = nil
            end
        end
        bg.name = label
        bg.HandleEvent = function(self,event)
            if event.Type == 'ButtonPress' or event.Type == 'ButtonDClick' then
                if event.Modifiers.Shift and event.Modifiers.Ctrl then
					import("/mods/W_Groups/modules/groupinterface.lua").FactorySelection(tonumber(name))
                    --Selection.FactorySelection(self.name)
                elseif event.Modifiers.Shift then
					import("/mods/W_Groups/modules/groupinterface.lua").AppendBattalionToSelection(tonumber(name))
                    --Selection.AppendSetToSelection(self.name)
                else
					import("/mods/W_Groups/modules/groupinterface.lua").SelectBattalion(tonumber(name))
                    --Selection.ApplySelectionSet(self.name)
                end
            end
        end
        
        bg:UpdateGroup()
        
        return bg
    end

	-- get all units in the battalion
	local units = {}
	units = import("/mods/W_Groups/modules/groupinterface.lua").GetAllSquads(name)

    if not controls.groups[name] and units then
		--LOG('Calling create on ' .. name)
        PlaySound(Sound({Bank = 'Interface', Cue = 'UI_Economy_Click'}))
        controls.groups[name] = CreateGroup(units, name)
        local unitIDs = {}
        for _, unit in units do
            table.insert(unitIDs, unit:GetEntityId())
        end
        SimCallback({Func = 'OnControlGroupAssign', Args = unitIDs})
	end

--    elseif controls.groups[name] and not units then
--		LOG('Calling destroy on ' .. name)
--        controls.groups[name]:Destroy()
--        controls.groups[name] = nil
--    elseif controls.groups[name] then
--		LOG('Calling updategroup on ' .. name)
--        controls.groups[name].units = units
--        controls.groups[name]:UpdateGroup()
--        local unitIDs = {}
--        for _, unit in units do
--            table.insert(unitIDs, unit:GetEntityId())
--        end        
--        SimCallback({Func = 'OnControlGroupAssign', Args = unitIDs})
--    end

	-- As an alternative to checking on call, because group manipulation functions may also delete groups,
	-- We must check that all the groups really still exist.
	-- This is rather slow, but it isn't done often.
	for i = 1,10,1 do
		local number = tostring(i)
		if controls.groups[number] then
			units = import("/mods/W_Groups/modules/groupinterface.lua").GetAllSquads(i)
			if (units) then
				controls.groups[number].units = units
				controls.groups[number]:UpdateGroup()
			else
				controls.groups[number]:Destroy()
			end
			-- I looked at the callback and it does not seem to do anything relevant
			-- It is probably still necessary in single player, so I'm doing it too
	        local unitIDs = {}
    	    for _, unit in units do
        	    table.insert(unitIDs, unit:GetEntityId())
	        end
			SimCallback({Func = 'OnControlGroupAssign', Args = unitIDs})
		end
	end

    import(UIUtil.GetLayoutFilename('controlgroups')).LayoutGroups()
end