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

require "Hooks/DAMN_EnterAnimations";

DAMN = DAMN or {};
CAM69 = CAM69 or {};

DAMN.EnterAnimations:registerVehicleScript("Base.69camaroRS", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.69camaroSS", "low");

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

    if not (vehicle and string.find(vehicle:getScriptName(), "69camaro")) 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

Events.OnPlayerUpdate.Add(CAM69.SyncRearWindows)