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

require "Hooks/DAMN_EnterAnimations";

DAMN = DAMN or {};
CHR69 = CHR69 or {};

DAMN.EnterAnimations:registerVehicleScript("Base.69charger440", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.69charger500", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.69chargerDaytona", "low");
DAMN.EnterAnimations:registerVehicleScript("Base.69chargerRT", "low");

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

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