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

require "Hooks/DAMN_EnterAnimations";

DAMN = DAMN or {};
FLC73 = FLC73 or {};

DAMN.EnterAnimations:registerVehicleScript("Base.73fordFalconPS", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.73fordFalconPSlhd", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.73fordFalconXBGT", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.73fordFalconXBGTlhd", "low");


function FLC73.SyncRearWindows(player)
    local player = getPlayer()
    local vehicle = player:getVehicle()

    if not (vehicle and string.find(vehicle:getScriptName(), "73fordFalcon")) then return end

    local frontLeftOpen  = vehicle:getPartById("WindowFrontLeft"):getWindow():isOpen()
    local frontRightOpen = vehicle:getPartById("WindowFrontRight"):getWindow():isOpen()

    local rearLeft  = vehicle:getPartById("WindowRearLeft")
    local rearRight = vehicle:getPartById("WindowRearRight")

    if rearLeft and rearLeft:getDoor() then
        local door = rearLeft:getDoor()
        if frontLeftOpen and not door:isOpen() then
            vehicle:playPartAnim(rearLeft, "Open")
            door:setOpen(true)
        elseif not frontLeftOpen and door:isOpen() then
            vehicle:playPartAnim(rearLeft, "Close")
            door:setOpen(false)
        end
    end

    if rearRight and rearRight:getDoor() then
        local door = rearRight:getDoor()
        if frontRightOpen and not door:isOpen() then
            vehicle:playPartAnim(rearRight, "Open")
            door:setOpen(true)
        elseif not frontRightOpen and door:isOpen() then
            vehicle:playPartAnim(rearRight, "Close")
            door:setOpen(false)
        end
    end
end

local scoopOpen = false

function FLC73.GasPedalDown(key)
    if key == 17 then
        local player = getPlayer()
        local vehicle = player and player:getVehicle()
        if vehicle and string.find(vehicle:getScriptName(), "73fordFalconPS") then
            local seat = vehicle:getSeat(player)
            if seat ~= 0 then return end

            local part = vehicle:getPartById("FLCSPScoop")
            if part and part:getDoor() and vehicle:isEngineRunning() and not scoopOpen then
                vehicle:playPartAnim(part, "Open")
                part:getDoor():setOpen(true)
                scoopOpen = true
            end
        end
    end
end

function FLC73.GasPedalUp(key)
    if key == 17 then
        local player = getPlayer()
        local vehicle = player and player:getVehicle()
        if vehicle and string.find(vehicle:getScriptName(), "73fordFalconPS") then
            local seat = vehicle:getSeat(player)
            if seat ~= 0 then return end

            local part = vehicle:getPartById("FLCSPScoop")
            if part and part:getDoor() and vehicle:isEngineRunning() and scoopOpen then
                vehicle:playPartAnim(part, "Close")
                part:getDoor():setOpen(false)
                scoopOpen = false
            end
        end
    end
end

Events.OnPlayerUpdate.Add(FLC73.SyncRearWindows)
Events.OnKeyStartPressed.Add(FLC73.GasPedalDown)
Events.OnKeyPressed.Add(FLC73.GasPedalUp)