require "DAMN_Armor_Shared";

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

SUB93 = SUB93 or {};

function SUB93.activeArmor(player, vehicle)
   
		--

			local protection = vehicle:getPartById("SUB93BumperFront")
			local inventoryItem = protection:getInventoryItem();
			local part = vehicle:getPartById("EngineDoor")
				if part and protection and part:getInventoryItem() and inventoryItem and part:getModData()
				then 
                    if inventoryItem:getFullType() == "Base.93chevySuburbanBullbarFrontD" 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) <= 20 and ZombRandBetween(0,3) or 0);
								DAMN.Armor:setPartCondition(protection, cond);
                            end
                        end
                elseif inventoryItem:getFullType() ~= "Base.93chevySuburbanBumperFront0" and inventoryItem:getFullType() ~= "Base.93chevySuburbanBumperFront1" 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) <= 40 and ZombRandBetween(0,5) or 0);
								DAMN.Armor:setPartCondition(protection, cond);
							end
						end
				elseif inventoryItem:getFullType() == "Base.93chevySuburbanBumperFront0" or inventoryItem:getFullType() == "Base.93chevySuburbanBumperFront1" 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,12);
								DAMN.Armor:setPartCondition(protection, cond);
							end
						end
					end
				else
					local part = vehicle:getPartById("Engine")
						if protection and inventoryItem and part and part:getModData()
						then
							if inventoryItem:getFullType() ~= "Base.93chevySuburbanBumperFront0" and inventoryItem:getFullType() ~= "Base.93chevySuburbanBumperFront1" 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,3);
										DAMN.Armor:setPartCondition(protection, cond);
									end
								end
							end
						end
				end

				--

			local protection = vehicle:getPartById("SUB93BumperRear")
			local inventoryItem = protection:getInventoryItem();
			local part = vehicle:getPartById("TrunkDoor")
				if part and protection and part:getInventoryItem() and inventoryItem 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,10);
							DAMN.Armor:setPartCondition(protection, cond);
						end
					end
				end

		--

			for partId, armorPartId in pairs({
				["WindowFrontLeft"] = "SUB93WindowFrontLeftArmor",
				["WindowFrontRight"] = "SUB93WindowFrontRightArmor",
				["WindowRearLeft"] = "SUB93WindowRearLeftArmor",
				["WindowRearRight"] = "SUB93WindowRearRightArmor",
				["WindowBackLeft"] = "SUB93WindowBackLeftArmor",
				["WindowBackRight"] = "SUB93WindowBackRightArmor",
			}) 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,2)
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end

		--

			for partId, armorPartId in pairs({
				["HeadlightLeft"] = "SUB93BumperFront",
				["HeadlightRight"] = "SUB93BumperFront",
				["HeadlightRearLeft"] = "SUB93BumperRear",
				["HeadlightRearRight"] = "SUB93BumperRear",
			}) 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

		--

			local protection = vehicle:getPartById("SUB93WindshieldArmor")
			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) <= 65 and ZombRandBetween(0,3) or 0)
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end

		--

		for i, freezeState in ipairs ({"SUB93SpareTire", "SUB93SideSteps", "SUB93BugGuard", "SUB93PDgunrack"})
				do
					if vehicle:getPartById(freezeState) then
						local part = vehicle:getPartById(freezeState)
						local freezeCond = tonumber(part:getModData().saveCond)
					    	if freezeCond and part:getCondition() < freezeCond then
					    		DAMN.Armor:setPartCondition(part, freezeCond)
							end
					end
			end

		--

			local protection = vehicle:getPartById("SUB93WindshieldRearArmor")
			local part = vehicle:getPartById("WindshieldRear")
			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) <= 65 and ZombRandBetween(0,3) or 0)
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end

        --

			local protection = vehicle:getPartById("SUB93RollbarToolbox")
			local part = vehicle:getPartById("WindshieldRear")
			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) <= 65 and ZombRandBetween(0,3) or 0)
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end

        --

            if 	vehicle and (
                string.find( vehicle:getScriptName(), "93chevySilveradoK3500" )) then
                    local part = vehicle:getPartById("WindshieldRear")
                    if part then
                        local xtraCond = tonumber(part:getModData().saveCond)
                        if xtraCond and part:getCondition() < xtraCond then
                            DAMN.Armor:setPartCondition(part, xtraCond)
                        end
                    end
            end

end

DAMN.Armor:add("Base.93chevySuburban", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanfbi", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanfd", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanksp", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanpd", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoCCdually", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoCC", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoCClong", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoCClongfd", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoSC", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoSClong", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoSClongFossoil", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoXC", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoXClong", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoXClongMcCoy", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoSCdually", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoXClongRanger", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanpdu", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoXCdually", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySuburbanDually", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoK3500flatbed", SUB93.activeArmor);
DAMN.Armor:add("Base.93chevySilveradoK3500wrecker", SUB93.activeArmor);