require "DAMN_Parts";
require "DAMN_Spawns";

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

DAMN.Parts:processConfigV2("MR2", {
	["BumperFront"] = {
		partId = "DAMNBumperFront",
		itemToModel = {
			["Base.87toyotaMR2BumperFront0"] = "FrontBumper0",
			["Base.87toyotaMR2BumperFrontA"] = "FrontBumperA",
			["Base.87toyotaMR2BullbarFrontA"] = "FrontBullbarA",
		},
		default = "first",
	},
	["BumperRear"] = {
		partId = "DAMNBumperRear",
		itemToModel = {
			["Base.87toyotaMR2BumperRear0"] = "BumperRear0",
		},
		default = "first",
	},
	["WindshieldArmor"] = {
		partId = "DAMNWindshieldArmor",
		itemToModel = {
			["Base.87toyotaMR2WindshieldArmor"] = "MR2winda0",
		},
	},
	["WindowLeftArmor"] = {
		partId = "DAMNFrontLeftArmor",
		itemToModel = {
			["Base.87toyotaMR2WindowArmor"] = "MR2leftdoora",
		},
	},
	["WindowRightArmor"] = {
		partId = "DAMNFrontRightArmor",
		itemToModel = {
			["Base.87toyotaMR2WindowArmor"] = "MR2rightdoora",
		},
	},
	["WindshieldRearArmor"] = {
		partId = "DAMNWindshieldRearArmor",
		itemToModel = {
			["Base.87toyotaMR2WindshieldRearArmor"] = "MR2windra",
		},
	},
	["Sideskirts"] = {
		partId = "DAMNSideskirts",
		itemToModel = {
			["Base.87toyotaMR2Sideskirts3"] = "MR2sideskirts",
		},
		default = "trve_random",
		noPartChance = 30,
	},
	["Spoiler"] = {
		partId = "DAMNSpoiler",
		itemToModel = {
			["Base.87toyotaMR2Spoiler3"] = "MR2spoiler0",
		},
		default = "trve_random",
		noPartChance = 30,
	},
	["SpoilerRoof"] = {
		partId = "DAMNSpoilerRoof",
		itemToModel = {
			["Base.87toyotaMR2RoofSpoiler3"] = "MR2spoiler1",
		},
		default = "trve_random",
		noPartChance = 30,
	},
	["SpareTire"] = {
		partId = "DAMNSpareTire",
		itemToModel = {
			["Base.87toyotaMR2TireT13"] = "MR2spare1",
			["Base.87toyotaMR2TireT23"] = "MR2spare2",
            ["damnCraft.SmallTire1"] = "MR2spare3",
		},
		default = "trve_random",
		noPartChance = 10,
	},
	["Roofrack"] = {
		partId = "MR2Roofrack",
		itemToModel = {
			["Base.87toyotaMR2Roofrack3"] = "MR2Roofrack0",
		},
		default = "trve_random",
		noPartChance = 95,
	},
	["Mudflaps"] = {
		partId = "DAMNMudflaps",
		itemToModel = {
			["Base.87toyotaMR2Mudflaps3"] = "MR2mudflaps0",
		},
		default = "trve_random",
		noPartChance = 20,
	},
	["TireFrontLeft"] = {
		partId = "TireFrontLeft",
		itemToModel = {
			["Base.87toyotaMR2TireT13"] = "MR2Tire0",
			["Base.87toyotaMR2TireT23"] = "MR2Tire1",
            ["damnCraft.SmallTire1"] = "MR2Tire2",
		},
	},
	["TireFrontRight"] = {
		partId = "TireFrontRight",
		itemToModel = {
			["Base.87toyotaMR2TireT13"] = "MR2Tire0",
			["Base.87toyotaMR2TireT23"] = "MR2Tire1",
            ["damnCraft.SmallTire1"] = "MR2Tire2",
		},
	},
	["TireRearLeft"] = {
		partId = "TireRearLeft",
		itemToModel = {
			["Base.87toyotaMR2TireT13"] = "MR2Tire0",
			["Base.87toyotaMR2TireT23"] = "MR2Tire1",
            ["damnCraft.SmallTire1"] = "MR2Tire2",
		},
	},
	["TireRearRight"] = {
		partId = "TireRearRight",
		itemToModel = {
			["Base.87toyotaMR2TireT13"] = "MR2Tire0",
			["Base.87toyotaMR2TireT23"] = "MR2Tire1",
            ["damnCraft.SmallTire1"] = "MR2Tire2",
		},
	},
});


function MR2.ContainerAccess.FrontTrunk(vehicle, part, chr)
	if chr:getVehicle() then return false end
	if not vehicle:isInArea(part:getArea(), chr) then return false end
	local TrunkTwo = vehicle:getPartById("MR2TrunkDoorFront")
	if TrunkTwo and TrunkTwo:getDoor() then
		if not TrunkTwo:getInventoryItem() then return true end
		if not TrunkTwo:getDoor():isOpen() then return false end
	end
	--
	return true
end

function MR2.popupLights(player)
    local vehicle = player.getVehicle and player:getVehicle() or nil
        if (vehicle and string.find( vehicle:getScriptName(), "87toyotaMR2" )) then

            local part = vehicle:getPartById("MR2PopupLights")
            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

Events.OnGameStart.Add(function()

    Events.OnKeyPressed.Add(function(key)
        local player = getPlayer()
        local vehicle = player and player:getVehicle() or nil
            if (vehicle and string.find( vehicle:getScriptName(), "87toyotaMR2" )) then

                local part = vehicle:getPartById("MR2Sunroof")

                if part then
                    local item = part:getInventoryItem()
                    local opened = part:getDoor():isOpen()

                    if item and opened and key == 210 then
                        vehicle:playPartAnim(part, "Close")
                        vehicle:playPartSound(part, player, "Close")
                        part:getDoor():setOpen(false)
                        vehicle:transmitPartDoor(part)
                        
                    elseif item and not opened and key == 210 then
                        vehicle:playPartAnim(part, "Open")
                        vehicle:playPartSound(part, player, "Open")
                        part:getDoor():setOpen(true)
                        vehicle:transmitPartDoor(part)
                    end
                else end
        end
    end )

end);

Events.OnPlayerUpdate.Add(MR2.popupLights);