require "DAMN_Armor_Shared";

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

SUB87 = SUB87 or {};

function SUB87.activeArmor(player, vehicle)
   
		--

			local part = vehicle:getPartById("TruckBed")
				if part and part:getCondition() < 53 then
					DAMN.Armor:setPartCondition(part, 53);
				end

		--

			local protection = vehicle:getPartById("SUB87BumperFront")
			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.87chevySuburbanBumperFront0" and inventoryItem:getFullType() ~= "Base.87chevySuburbanBumperFront1" 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.87chevySuburbanBumperFront0" or inventoryItem:getFullType() == "Base.87chevySuburbanBumperFront1" 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 protection = vehicle:getPartById("SUB87BumperFront")
					local inventoryItem = protection:getInventoryItem();
					local part = vehicle:getPartById("Engine")
						if protection and inventoryItem and part and part:getModData()
						then
							if inventoryItem:getFullType() ~= "Base.87chevySuburbanBumperFront0" and inventoryItem:getFullType() ~= "Base.87chevySuburbanBumperFront1" 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("SUB87BumperRear")
			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

		--

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

		--

		for partId, armorPartId in pairs({
				["WindowFrontLeft"] = "SUB87WindowFrontLeftArmor",
				["WindowFrontRight"] = "SUB87WindowFrontRightArmor",
				["WindowRearLeft"] = "SUB87WindowRearLeftArmor",
				["WindowRearRight"] = "SUB87WindowRearRightArmor",
				["WindowBackLeft"] = "SUB87WindowBackLeftArmor",
				["WindowBackRight"] = "SUB87WindowBackRightArmor",
			}) 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"] = "SUB87BumperFront",
				["HeadlightRight"] = "SUB87BumperFront",
				["HeadlightRearLeft"] = "SUB87BumperRear",
				["HeadlightRearRight"] = "SUB87BumperRear",
			}) 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({
				["Windshield"] = "SUB87WindshieldArmor",
				["WindshieldRear"] = "SUB87WindshieldRearArmor",
			}) 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) <= 65 and ZombRandBetween(0,3) or 0)
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end

		--

		for i, freezeState in ipairs ({"SUB87SpareTire", "SUB87RoofLights", "SUB87Roofrack", "SUB87SideSteps", "SUB87GasCan0", "SUB87GasCan1", "SUB87GasCan2", "SUB87Generator", "SUB87SideStorage", "SUB87SideCan0", "SUB87SideCan1"})
				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

end

DAMN.Armor:add("Base.87chevySuburban", SUB87.activeArmor);
DAMN.Armor:add("Base.87chevySuburbanOP", SUB87.activeArmor);
DAMN.Armor:add("Base.87chevySuburbanCUCV", SUB87.activeArmor);