AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

util.AddNetworkString("RHC_Jailer_Menu")

local RUSSIAN_FACTION = FACTION_RUSSIAN
local SPECIAL_PODR = 6

function ENT:Initialize()
    self:SetModel("models/Barney.mdl")
    self:SetSolid(SOLID_BBOX)
    self:PhysicsInit(SOLID_BBOX)
    self:SetMoveType(MOVETYPE_NONE)
    self:DrawShadow(true)
    self:SetUseType(SIMPLE_USE)
    self:SetFlexWeight(10, 0)
    self:ResetSequence(3)
end

function ENT:Use(activator)
    if self.Touched and self.Touched > CurTime() then return end
    self.Touched = CurTime() + 1.5

    local char = activator:GetCharacter()
    if not char then return end

    -- Removing specific job check to allow anyone to use the jailer
    -- if not isAllowed then
    --     activator:ChatPrint("Только сотрудники исполнительной власти могут использовать этого NPC.")
    --     return
    -- end

    local targetPly = activator.Dragging
    if not IsValid(targetPly) then
        local closestDist = 40000 -- 200 units
        for _, p in ipairs(player.GetAll()) do
            if p ~= activator and p:GetNWBool("rhc_cuffed", false) then
                local dist = p:GetPos():DistToSqr(activator:GetPos())
                if dist < closestDist then
                    closestDist = dist
                    targetPly = p
                end
            end
        end
    end

    activator.LastJailerNPC = self
    activator.LastJailerFaction = "RF"

    net.Start("RHC_Jailer_Menu")
        net.WriteEntity(targetPly)
        net.WriteString("RF")
    net.Send(activator)
end
