require "DAMN_Armor_Shared";

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

CAP85 = CAP85 or {};

function CAP85.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.85chevyCapriceBumperFrontA" or inventoryItem:getFullType() == "Base.85gmBbodyBullbarFrontA" 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,4) or 0);
								DAMN.Armor:setPartCondition(protection, cond);
							end
						end
					elseif inventoryItem:getFullType() == "Base.85chevyCapriceBumperFront0" 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(1,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.85chevyCapriceBumperFront0" 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(1,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 
                    if inventoryItem:getFullType() ~= "Base.85chevyCapriceBumperRear0" 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
                    elseif inventoryItem:getFullType() == "Base.85chevyCapriceBumperRear0" 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,15);
                            DAMN.Armor:setPartCondition(protection, cond);
                        end
                    end
                end
            end

		--

            for partId, armorPartId in pairs({
                ["WindowFrontLeft"] = "DAMNFrontLeftArmor",
                ["WindowFrontRight"] = "DAMNFrontRightArmor",
                ["WindowRearLeft"] = "DAMNRearLeftArmor",
                ["WindowRearRight"] = "DAMNRearRightArmor",
                ["WindowBackLeft"] = "DAMNBackLeftArmor",
                ["WindowBackRight"] = "DAMNBackRightArmor",
            }) do
                local part = vehicle:getPartById(partId)
                if part then
                    local protection = vehicle:getPartById(armorPartId)
                    if protection then
                        local inventoryItem = protection:getInventoryItem()
                        if inventoryItem 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, string.find(inventoryItem:getFullType(), "M$") and 2 or 4)
                                DAMN.Armor:setPartCondition(protection, cond)
                            end
                        end
                    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 inventoryItem = protection:getInventoryItem();
		local part = vehicle:getPartById("Windshield")
			if part and protection and part:getInventoryItem() and inventoryItem and part:getModData()
			then 
				if inventoryItem:getFullType() ~= "Base.85gmBbodyWindshieldArmorW" 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
				elseif inventoryItem:getFullType() == "Base.85gmBbodyWindshieldArmorW" 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(1,3);
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end
		end

		--

		for i, freezeState in ipairs ({"DAMNSpareTire", "CAP85Roofrack", "DAMNBarrier"})
				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("DAMNWindshieldRearArmor")
		local inventoryItem = protection:getInventoryItem();
		local part = vehicle:getPartById("WindshieldRear")
			if part and protection and part:getInventoryItem() and inventoryItem and part:getModData()
			then 
				if inventoryItem:getFullType() ~= "Base.85gmBbodyWindshieldRearArmorW" 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
				elseif inventoryItem:getFullType() == "Base.85gmBbodyWindshieldRearArmorW" 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(1,3);
						DAMN.Armor:setPartCondition(protection, cond);
					end
				end
			end
		end

        --temp tire fix

			for i, tempPart in ipairs ({"TireFrontLeft", "TireFrontRight", "TireRearLeft", "TireRearRight"})
				do
					if vehicle:getPartById(tempPart) then
						local part = vehicle:getPartById(tempPart)
					    	if part:getContainerContentAmount() < 1 then
								DAMN.Parts:setContainerAmount(part, DAMN.Armor["tireCTISPressure"]);
							end
					end
			end

end

DAMN.Armor:add("Base.85chevyCapriceSedan", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyCapriceCoupe", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyCapriceWagon", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyCapriceWagon2", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanAirport", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanBCS", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanCLPD", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanFD", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanKSP", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanLCPD", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanMCS", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanMPD", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanPD", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanPDu", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanPrison", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanRanger", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanTaxi", CAP85.activeArmor);
DAMN.Armor:add("Base.85chevyImpalaSedanWPPD", CAP85.activeArmor);