require "Vehicles/TimedActions/ISLockDoors"
require "Vehicles/TimedActions/ISLockVehicleDoor"
require "Vehicles/TimedActions/ISUnlockVehicleDoor"

local ISLockDoorsClass = rawget(_G, "ISLockDoors")
local ISLockVehicleDoorClass = rawget(_G, "ISLockVehicleDoor")
local ISUnlockVehicleDoorClass = rawget(_G, "ISUnlockVehicleDoor")

local function syncDoorParts(vehicle, locked, partIds)
    if not vehicle then
        return
    end

    for _, partId in ipairs(partIds) do
        local part = vehicle:getPartById(partId)
        local door = part and part:getDoor() or nil
        if door and not door:isLockBroken() and door:isLocked() ~= locked then
            door:setLocked(locked)
            vehicle:transmitPartDoor(part)
        end
    end
end

local function syncTwinTrunkDoors(vehicle, locked)
    local trunkDoor = vehicle and vehicle:getPartById("TrunkDoor") or nil
    local trunkDoorLeft = vehicle and vehicle:getPartById("TrunkDoorLeft") or nil
    if not trunkDoor or not trunkDoorLeft then
        return
    end

    syncDoorParts(vehicle, locked, { "TrunkDoor", "TrunkDoorLeft" })
end

local function getLinkedTrailer(vehicle)
    if not vehicle then
        return nil
    end
    if vehicle.getVehicleTowing then
        local towing = vehicle:getVehicleTowing()
        if towing then
            return towing
        end
    end
    if vehicle.getVehicleTowedBy then
        return vehicle:getVehicleTowedBy()
    end
    return nil
end

local function syncAttachedTrailerDoors(vehicle, locked)
    local trailer = getLinkedTrailer(vehicle)
    if not trailer then
        return
    end

    syncDoorParts(trailer, locked, { "DoorRear", "ToolboxDoor" })
end

if ISLockDoorsClass then
    local originalLockDoorsComplete = ISLockDoorsClass.complete
    function ISLockDoorsClass:complete()
        local result = originalLockDoorsComplete(self)
        if result then
            syncTwinTrunkDoors(self.vehicle, self.locked)
            syncAttachedTrailerDoors(self.vehicle, self.locked)
        end
        return result
    end
end

if ISLockVehicleDoorClass then
    local originalLockVehicleDoorComplete = ISLockVehicleDoorClass.complete
    function ISLockVehicleDoorClass:complete()
        local result = originalLockVehicleDoorComplete(self)
        if result and self.part then
            local partId = self.part:getId()
            if partId == "TrunkDoor" or partId == "TrunkDoorLeft" then
                syncTwinTrunkDoors(self.vehicle, true)
                syncAttachedTrailerDoors(self.vehicle, true)
            elseif partId == "DoorRear" or partId == "ToolboxDoor" then
                syncAttachedTrailerDoors(self.vehicle, true)
            end
        end
        return result
    end
end

if ISUnlockVehicleDoorClass then
    local originalUnlockVehicleDoorComplete = ISUnlockVehicleDoorClass.complete
    function ISUnlockVehicleDoorClass:complete()
        local result = originalUnlockVehicleDoorComplete(self)
        if result and self.part then
            local partId = self.part:getId()
            if partId == "TrunkDoor" or partId == "TrunkDoorLeft" then
                syncTwinTrunkDoors(self.vehicle, false)
                syncAttachedTrailerDoors(self.vehicle, false)
            elseif partId == "DoorRear" or partId == "ToolboxDoor" then
                syncAttachedTrailerDoors(self.vehicle, false)
            end
        end
        return result
    end
end
