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

require "Hooks/DAMN_EnterAnimations";

DAMN = DAMN or {};
CUDA = CUDA or {};

DAMN.EnterAnimations:registerVehicleScript("Base.70cuda", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.70barracudaAAR", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.70barracuda", "low");

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

    if not ((vehicle and string.find(vehicle:getScriptName(), "70cuda")) or
    (vehicle and string.find(vehicle:getScriptName(), "70barracudaAAR")) or
    (vehicle and string.find(vehicle:getScriptName(), "70barracuda"))) 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(CUDA.SyncRearWindows)