-- ClothHandler: операции с одеждой
-- PantsLocation проверяется по аналогу из файла projectzomboid/media/scripts/generated/items/clothing.txt
local ClothHandler = {
    PantsLocation = {"base:pants","base:shortpants","base:pants_skinny","base:shortsshort","base:underwear","base:underwearbottom","base:fullsuit"},
    RemovedPants = {}
}

function ClothHandler.Undress(Character)
    DebugLog.log("[Zombolewd] /ZomboLewdClothHandler/ClothHandler.Undress")    
    DebugLog.log("Undressing...")
    local CharacterClothes = Character:getWornItems()
    local ResultClothes = {}
    --определение предметов для снятия
    if CharacterClothes then
        for i = 1, CharacterClothes:size() do
            local wornItem = CharacterClothes:get(i-1)
            if wornItem then
                DebugLog.log("wornItem found")
                local item = wornItem:getItem()
                local location = wornItem:getLocation()  
                DebugLog.log(string.format("wornItem location %s", tostring(location)))              
                for _, PantLocation in ipairs(ClothHandler.PantsLocation) do
                    if PantLocation == tostring(location) then
                        DebugLog.log(string.format("Try to undress %s (getName), %s (getDisplayName)", tostring(item:getName()), tostring(item:getDisplayName())))                        
                        table.insert(ResultClothes, item)                                           
                    end
                end
            end            
        end
    end
    --если снимать сразу то будет выдавать ошибку из-за иного размера массива CharacterClothes
    for _, ResultCloth in ipairs(ResultClothes) do
        Character:removeWornItem(ResultCloth)   
    end
    DebugLog.log("Uneqip weapon...")    
    Character:setPrimaryHandItem(nil)
    Character:setSecondaryHandItem(nil)
    if not Character:isFemale() then
        DebugLog.log("Try to add penis...")
        local penis = instanceItem("Zombolewd.Penis")
        Character:setWornItem(ZombolewdRegistries.PenisLoc,penis)
    end
    return ResultClothes
end

function ClothHandler.RemovePenis(Character)
    DebugLog.log("[Zombolewd] /ZomboLewdClothHandler/ClothHandler.RemovePenis")
	-- Remove from inventory
	Character:setWornItem(ZombolewdRegistries.PenisLoc,nil)
end

function ClothHandler.DressUp(Character,CharacterClothes)
    DebugLog.log("[Zombolewd] /ZomboLewdClothHandler/ClothHandler.DressUp")
    if #CharacterClothes > 0 then    
        for _, item in ipairs(CharacterClothes) do    
            ISTimedActionQueue.add(ISWearClothing:new(Character, item, 1))
        end
    end
    if not Character:isFemale() then
        ClothHandler.RemovePenis(Character)
    end
end

return ClothHandler