require "DAMN_Parts";
require "DAMN_Spawns";

--***********************************************************
--**                   KI5 / bikinihorst                   **
--***********************************************************

DAMN.Parts:processConfigV2("CHR69", {
	["FrontBumper"] = {
		partId = "DAMNBumperFront",
		itemToModel = {
			["Base.69chargerBumperFront0"] = "FrontBumper0",
			["Base.69chargerBumperFrontA"] = "FrontBumperA",
			["Base.69chargerBullbarFrontA"] = "FrontBullbarA",
		},
		default = "first",
	},
    ["FrontBumperD"] = {
		partId = "DAMNBumperFront",
		itemToModel = {
			["Base.69chargerBumperFront1"] = "FrontBumper1",
			["Base.69chargerBumperFrontB"] = "FrontBumperB",
			["Base.69chargerBullbarFrontA"] = "FrontBullbarA",
		},
		default = "first",
	},
	["RearBumper"] = {
		partId = "DAMNBumperRear",
		itemToModel = {
			["Base.69chargerBumperRear0"] = "RearBumper0",
		},
		default = "first",
	},
	["WindshieldArmor"] = {
		partId = "DAMNWindshieldArmor",
		itemToModel = {
			["Base.69chargerWindshieldArmor"] = "winda",
		},
	},
	["FrontLeftArmor"] = {
		partId = "DAMNFrontLeftArmor",
		itemToModel = {
			["Base.69chargerFrontWindowArmor"] = "fla",
		},
	},
	["FrontRightArmor"] = {
		partId = "DAMNFrontRightArmor",
		itemToModel = {
			["Base.69chargerFrontWindowArmor"] = "fra",
		},
	},
	["RearLeftArmor"] = {
		partId = "DAMNRearLeftArmor",
		itemToModel = {
			["Base.69chargerRearWindowArmor"] = "rla",
		},
	},
	["RearRightArmor"] = {
		partId = "DAMNRearRightArmor",
		itemToModel = {
			["Base.69chargerRearWindowArmor"] = "rra",
		},
	},
	["WindshieldRearArmor"] = {
		partId = "DAMNWindshieldRearArmor",
		itemToModel = {
			["Base.69chargerWindshieldRearArmor"] = "windra",
		},
	},
    ["Spoiler"] = {
		partId = "DAMNSpoiler",
		itemToModel = {
			["Base.69chargerSpoiler3"] = "spoiler0",
		},
		default = "trve_random",
		noPartChance = 10,
	},
    ["SpareTire"] = {
		partId = "DAMNSpareTire",
		itemToModel = {
			["Base.69chargerTire3"] = "spare0",
            ["damnCraft.SmallTire1"] = "spare1",
            ["Base.moparMagnum500TAtire3"] = "spare2",
		},
		default = "trve_random",
		noPartChance = 15,
	},
    ["TireFrontLeft"] = {
		partId = "TireFrontLeft",
		itemToModel = {
			["Base.69chargerTire3"] = "TireL",
            ["damnCraft.SmallTire1"] = "Tire2L",
            ["Base.moparMagnum500TAtire3"] = "Tire3L",
		},
	},
	["TireFrontRight"] = {
		partId = "TireFrontRight",
		itemToModel = {
			["Base.69chargerTire3"] = "TireR",
            ["damnCraft.SmallTire1"] = "Tire2R",
            ["Base.moparMagnum500TAtire3"] = "Tire3R",
		},
	},
	["TireRearLeft"] = {
		partId = "TireRearLeft",
		itemToModel = {
			["Base.69chargerTire3"] = "TireL",
            ["damnCraft.SmallTire1"] = "Tire2L",
            ["Base.moparMagnum500TAtire3"] = "Tire3L",
		},
	},
	["TireRearRight"] = {
		partId = "TireRearRight",
		itemToModel = {
			["Base.69chargerTire3"] = "TireR",
            ["damnCraft.SmallTire1"] = "Tire2R",
            ["Base.moparMagnum500TAtire3"] = "Tire3R",
		},
	},
    ["TireFrontLeft440"] = {
		partId = "TireFrontLeft",
		itemToModel = {
			["Base.69chargerTire3"] = "TireL",
            ["damnCraft.SmallTire1"] = "Tire2L",
            ["Base.moparMagnum500TAtire3"] = "Tire3L",
		},
        default = "Base.moparMagnum500TAtire3",
	},
	["TireFrontRight440"] = {
		partId = "TireFrontRight",
		itemToModel = {
			["Base.69chargerTire3"] = "TireR",
            ["damnCraft.SmallTire1"] = "Tire2R",
            ["Base.moparMagnum500TAtire3"] = "Tire3R",
		},
        default = "Base.moparMagnum500TAtire3",
	},
	["TireRearLeft440"] = {
		partId = "TireRearLeft",
		itemToModel = {
			["Base.69chargerTire3"] = "TireL",
            ["damnCraft.SmallTire1"] = "Tire2L",
            ["Base.moparMagnum500TAtire3"] = "Tire3L",
		},
        default = "Base.moparMagnum500TAtire3",
	},
	["TireRearRight440"] = {
		partId = "TireRearRight",
		itemToModel = {
			["Base.69chargerTire3"] = "TireR",
            ["damnCraft.SmallTire1"] = "Tire2R",
            ["Base.moparMagnum500TAtire3"] = "Tire3R",
		},
        default = "Base.moparMagnum500TAtire3",
	},
});

DAMN.Spawns:add("Base.69charger440", 12251, 6956, {
    direction = IsoDirections.E, 
    chance = 100,
    sandboxVar = "AllowPro440Spawns",
});

DAMN.Spawns:add("Base.69chargerDemon", 13117, 1331, {
    direction = IsoDirections.S, 
    chance = 50,
    sandboxVar = "AllowDemonChildSpawns",
});

DAMN.Spawns:add("Base.69chargerDemon", 6700, 11254, {     6697, 11226,
    direction = IsoDirections.S, 
    chance = 50,
    sandboxVar = "AllowDemonChildSpawns",
});

function CHR69.PopupLights(player)
    local vehicle = player.getVehicle and player:getVehicle() or nil
    if vehicle and string.find(vehicle:getScriptName(), "69charger") then

        local part = vehicle:getPartById("CHR69PopupLights") or vehicle:getPartById("CHR69LightsCover")
        if not part or not part:getDoor() then return end
        local opened = part:getDoor():isOpen()
        local active = vehicle:getHeadlightsOn()

        if active and opened then return
            elseif not active and opened then
                vehicle:playPartAnim(part, "Close")
                vehicle:playPartSound(part, player, "Close")
                part:getDoor():setOpen(false)
                vehicle:transmitPartDoor(part)
            elseif active and not opened then
                vehicle:playPartAnim(part, "Open")
                vehicle:playPartSound(part, player, "Open")
                part:getDoor():setOpen(true)
                vehicle:transmitPartDoor(part)
        end
    end
end

Events.OnPlayerUpdate.Add(CHR69.PopupLights);

function CHR69.ContainerAccess.TrunkInner(vehicle, part, chr)
    if chr:getVehicle() == vehicle then
        local seat = vehicle:getSeat(chr)
        return seat >= 0;
    elseif chr:getVehicle() then
        return false
    else
        if not vehicle:isInArea(part:getArea(), chr) then return false end
        local doorPart = vehicle:getPartById("DoorFrontLeft")
        if doorPart and doorPart:getInventoryItem() then
            if doorPart:getDoor() and not doorPart:getDoor():isOpen() then
                return false
            end
        end
        return true
    end
end