require "DAMN_Armor_Shared";

--***********************************************************
--**                   KI5 / bikinihorst                   **
--***********************************************************
--v2.0.0

M151A2 = M151A2 or {};

function M151A2.activeArmor(player, vehicle)
   
	--

    local part = vehicle:getPartById("GasTank")
    if part:getCondition() < 63 then
        DAMN.Armor:setPartCondition(part, 63)
    end

    --

    local protection = vehicle:getPartById("M151A2WindshieldArmor")
    local part = vehicle:getPartById("Windshield")
    if protection and protection:getInventoryItem() and part and part:getModData()
    then
        local partCond = tonumber(part:getModData().saveCond)
        if protection:getCondition() > 0 and partCond
        then
            if part:getCondition() < partCond
            then
                DAMN.Armor:setPartCondition(part, partCond);
                local cond = protection:getCondition() - (ZombRandBetween(0,100) <= 50 and ZombRandBetween(0,3) or 0)
                DAMN.Armor:setPartCondition(protection, cond);
            end
        end
    end

    --

    for partId, armorPartId in pairs({
        ["WindowFrontLeft"] = "DoorFrontLeft",
        ["WindowFrontRight"] = "DoorFrontRight",
    }) do
        local part = vehicle:getPartById(partId);
        local protection = vehicle:getPartById(armorPartId);
        if protection and protection:getInventoryItem() and part and part:getModData()
        then
            local partCond = tonumber(part:getModData().saveCond);
            if protection:getCondition() > 0 and partCond and part:getCondition() < partCond
            then
                DAMN.Armor:setPartCondition(part, partCond);
                local cond = protection:getCondition() - (ZombRandBetween(0,100) <= 50 and ZombRandBetween(0,2) or 0)
                DAMN.Armor:setPartCondition(protection, cond);
            end
        end
    end

    --

    local protection = vehicle:getPartById("M151A2Bumper")
    local part = vehicle:getPartById("EngineDoor")
        if part and protection and part:getInventoryItem() and protection:getInventoryItem() and part:getModData()
        then
            local partCond = tonumber(part:getModData().saveCond)
            if protection:getCondition() > 0 and partCond
            then
                if part:getCondition() < partCond
                then
                    DAMN.Armor:setPartCondition(part, partCond);
                    local cond = protection:getCondition() - (ZombRandBetween(0,100) <= 95 and ZombRandBetween(0,3) or 0)
                    DAMN.Armor:setPartCondition(protection, cond);
                end
            end
        else
            local protection = vehicle:getPartById("M151A2Bumper")
            local part = vehicle:getPartById("Engine")
                if protection and protection:getInventoryItem() and part and part:getModData()
                then
                    local partCond = tonumber(part:getModData().saveCond)
                    if protection:getCondition() > 0 and partCond
                    then
                        if part:getCondition() < partCond
                        then
                            DAMN.Armor:setPartCondition(part, partCond);
                            local cond = protection:getCondition() - ZombRand(4)
                            DAMN.Armor:setPartCondition(protection, cond);
                        end
                    end
                end
        end

    --

			for partId, armorPartId in pairs({
				["HeadlightLeft"] = "M151A2Bumper",
				["HeadlightRight"] = "M151A2Bumper",
			}) do
				local part = vehicle:getPartById(partId);
				local protection = vehicle:getPartById(armorPartId);
				if protection and protection:getInventoryItem() and part and part:getModData()
				then
					local partCond = tonumber(part:getModData().saveCond);
					if protection:getCondition() > 0 and partCond and part:getCondition() < partCond
					then
						DAMN.Armor:setPartCondition(part, partCond);
					end
				end
			end

        --

        for partId, armorPartId in pairs({
            ["HeadlightRearLeft"] = "M151A2BumperRear",
			["HeadlightRearRight"] = "M151A2BumperRear",
        }) do
            local part = vehicle:getPartById(partId);
            local protection = vehicle:getPartById(armorPartId);
            if protection and protection:getInventoryItem() and part and part:getModData()
            then
                local partCond = tonumber(part:getModData().saveCond);
                if protection:getCondition() > 0 and partCond and part:getCondition() < partCond
                then
                    DAMN.Armor:setPartCondition(part, partCond);
                    local cond = protection:getCondition() - (ZombRandBetween(0,100) <= 70 and ZombRandBetween(0,4) or 0)
                    DAMN.Armor:setPartCondition(protection, cond);
                end
            end
        end
    
        --

end

DAMN.Armor:add("Base.74amgeneralM151A2", M151A2.activeArmor);
DAMN.Armor:add("Base.74amgeneralM151A2MP", M151A2.activeArmor);