require "DAMN_Armor_Shared";

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

CH76 = CH76 or {};

function CH76.activeArmor(player, vehicle)
   
		--

			local protection = vehicle:getPartById("DAMNBumperFront")
			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.76chevyCseriesBumperFrontC" 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.76chevyKseriesBumperFront0" 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.76chevyKseriesBumperFront0" 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.76chevyKseriesBumperFront0" 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("DAMNBumperRear")
			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("DAMNBumperRear")
			local inventoryItem = protection:getInventoryItem();
			local part = vehicle:getPartById("CH76Trunk")
				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"] = "DAMNFrontLeftArmor",
				["WindowFrontRight"] = "DAMNFrontRightArmor",
                ["WindowRearLeft"] = "DAMNRearLeftArmor",
				["WindowRearRight"] = "DAMNRearRightArmor",
			}) 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"] = "DAMNBumperFront",
				["HeadlightRight"] = "DAMNBumperFront",
				["HeadlightRearLeft"] = "DAMNBumperRear",
				["HeadlightRearRight"] = "DAMNBumperRear",
			}) 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("DAMNWindshieldArmor")
			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

		--

		local protection = vehicle:getPartById("CH76TruckBedCover")
			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("CH76TruckBedExtras")
			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("DAMNWindshieldRearArmor")
			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

		--

		for i, freezeState in ipairs ({"DAMNSpareTireBed", "DAMNSpareTireRoof", "DAMNSpareTireRoof2", "CH76Roofrack", "DAMNGenerator", "DAMNTent", "DAMNGasCanOne", "DAMNGasCanTwo", "DAMNGasCanThree", "DAMNGasCanFour"})
				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

            --

            if 	vehicle and (
                string.find( vehicle:getScriptName(), "76chevyK20fd" ) or
                string.find( vehicle:getScriptName(), "76chevyK30CCfd" ) or
                string.find( vehicle:getScriptName(), "76chevyC30SCwrecker" ) or
                string.find( vehicle:getScriptName(), "76chevyK30CCduallyS" ) or
                string.find( vehicle:getScriptName(), "76chevyC30CCwrecker" )) 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.76chevyK10", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK20", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK20BigRed", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK20utility", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CC", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CCdually", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CCutility", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30SCdually", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK10spirit", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK10fd", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK20fd", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CCfd", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyC30SCwrecker", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyC30CCwrecker", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30SCwrecker", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CCwrecker", CH76.activeArmor);
DAMN.Armor:add("Base.76chevyK30CCduallyS", CH76.activeArmor);