require "DAMN_Parts";
require "DAMN_Spawns";

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

DAMN.Parts:processConfigV2("FRB82", {
	["BumperFront"] = {
		partId = "DAMNBumperFront",
		itemToModel = {
			["Base.82firebirdBumperFront0"] = "BumperFront0",
            ["Base.82firebirdBumperFront1"] = "BumperFront1",
			["Base.82firebirdBumperFrontA"] = "BumperFrontA",
		},
		default = "first",
	},
    ["BumperFrontK"] = {
		partId = "DAMNBumperFront",
		itemToModel = {
			["Base.82firebirdBumperFront1"] = "BumperFront1",
            ["Base.82firebirdBumperFront0"] = "BumperFront0",
			["Base.82firebirdBumperFrontA"] = "BumperFrontA",
		},
		default = "first",
	},
	["BumperRear"] = {
		partId = "DAMNBumperRear",
		itemToModel = {
			["Base.82firebirdBumperRear"] = "BumperRear0",
		},
		default = "first",
	},
	["WindshieldArmor"] = {
		partId = "DAMNWindshieldArmor",
		itemToModel = {
			["Base.82firebirdWindshieldArmor"] = "FRB82winda0",
		},
	},
	["WindshieldRearArmor"] = {
		partId = "DAMNWindshieldRearArmor",
		itemToModel = {
			["Base.82firebirdWindshieldRearArmor"] = "FRB82windra",
		},
	},
	["FrontLeftArmor"] = {
		partId = "DAMNFrontLeftArmor",
		itemToModel = {
			["Base.82firebirdFrontWindowArmor"] = "FRB82leftdoora",
		},
	},
	["FrontRightArmor"] = {
		partId = "DAMNFrontRightArmor",
		itemToModel = {
			["Base.82firebirdFrontWindowArmor"] = "FRB82rightdoora",
		},
	},
	["Roof"] = {
		partId = "FRB82RoofPanels",
		itemToModel = {
			["Base.82firebirdRearRoofPanels3"] = "FRB82Roof",
		},
		default = "trve_random",
		noPartChance = 20,
	},
	["Roofrack"] = {
		partId = "FRB82Roofrack",
		itemToModel = {
			["Base.82firebirdRoofrack3"] = "FRB82Roofrack0",
		},
		default = "trve_random",
		noPartChance = 95,
	},
	["Spoiler"] = {
		partId = "DAMNSpoiler",
		itemToModel = {
			["Base.82firebirdSpoilerA"] = "FRB82Spoiler0",
			["Base.82firebirdSpoilerB"] = "FRB82Spoiler1",
		},
		default = "trve_random",
		noPartChance = 33,
	},
    ["SpoilerK"] = {
		partId = "DAMNSpoiler",
		itemToModel = {
			["Base.82firebirdSpoilerB"] = "FRB82Spoiler1",
			["Base.82firebirdSpoilerA"] = "FRB82Spoiler0",
		},
		default = "first",
	},
    ["TireFrontLeft"] = {
		partId = "TireFrontLeft",
		itemToModel = {
			["Base.82firebirdTire3"] = "Tire1",
            ["damnCraft.SmallTire1"] = "Tire2",
		},
	},
	["TireFrontRight"] = {
		partId = "TireFrontRight",
		itemToModel = {
			["Base.82firebirdTire3"] = "Tire1",
            ["damnCraft.SmallTire1"] = "Tire2",
		},
	},
	["TireRearLeft"] = {
		partId = "TireRearLeft",
		itemToModel = {
			["Base.82firebirdTire3"] = "Tire1",
            ["damnCraft.SmallTire1"] = "Tire2",
		},
	},
	["TireRearRight"] = {
		partId = "TireRearRight",
		itemToModel = {
			["Base.82firebirdTire3"] = "Tire1",
            ["damnCraft.SmallTire1"] = "Tire2",
		},
	},
});

function FRB82.popupLights(player)
    local vehicle = player.getVehicle and player:getVehicle() or nil
        if (vehicle and string.find( vehicle:getScriptName(), "82firebird" )) then

            local part = vehicle:getPartById("FRB82PopupLights")
            local opened = part:getDoor():isOpen()
            local active = vehicle:getHeadlightsOn()

            if active and opened then return end

            if 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

function FRB82.KITTLights(player)
    local vehicle = player.getVehicle and player:getVehicle() or nil
        if (vehicle and (string.find(vehicle:getScriptName(), "82firebirdKITT") or string.find(vehicle:getScriptName(), "82firebirdKARR"))) then

            local part = vehicle:getPartById("FRB82KITTLights")
            local opened = part:getDoor():isOpen()
            local active = vehicle:getLightbarLightsMode() > 0

            if active and opened then return end

            if 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(FRB82.popupLights);
Events.OnPlayerUpdate.Add(FRB82.KITTLights);