if SERVER then
	AddCSLuaFile("shared.lua")
end

if CLIENT then
	SWEP.PrintName = "Cuffed"
	SWEP.Slot = 2
	SWEP.SlotPos = 1
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
end

SWEP.Author = "Military RP"
SWEP.Instructions = ""
SWEP.Contact = ""
SWEP.Purpose = ""

SWEP.HoldType = "passive"
SWEP.ViewModelFlip = false
SWEP.AnimPrefix	 = "passive"
SWEP.Category = "Military RP"

SWEP.Spawnable = false
SWEP.AdminSpawnable = false

SWEP.ViewModel = "models/tobadforyou/c_hand_cuffs.mdl"
SWEP.WorldModel = "models/tobadforyou/handcuffs.mdl"

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = 0
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

local CuffedBones = {
	["ValveBiped.Bip01_R_UpperArm"] = Angle(-21.4, 21.4, 3.6),
	["ValveBiped.Bip01_L_UpperArm"] = Angle(30, 8.2, 27.8),
	["ValveBiped.Bip01_R_Forearm"] = Angle(0, 25, -10.7),
	["ValveBiped.Bip01_L_Forearm"] = Angle(-11.5, 31.1, -18),
	["ValveBiped.Bip01_R_Hand"] = Angle(14.3, -14.3, 60),
	["ValveBiped.Bip01_L_Hand"] = Angle(-11, 14.7, 70.4),
}

function SWEP:Initialize() 
    self:SetHoldType("normal") 
end
function SWEP:CanPrimaryAttack() return false end
function SWEP:SecondaryAttack() return false end
function SWEP:Reload() return false end

function SWEP:Deploy()
    self:SetHoldType("normal")
    if SERVER then
        local ply = self:GetOwner()
        if IsValid(ply) then
            for boneName, ang in pairs(CuffedBones) do
                local boneId = ply:LookupBone(boneName)
                if boneId then
                    ply:ManipulateBoneAngles(boneId, ang)
                end
            end
        end
    end
    return true
end

function SWEP:Holster()
    if SERVER then
        local ply = self:GetOwner()
        if IsValid(ply) then
            for boneName, _ in pairs(CuffedBones) do
                local boneId = ply:LookupBone(boneName)
                if boneId then
                    ply:ManipulateBoneAngles(boneId, Angle(0,0,0))
                end
            end
        end
    end
    return true
end

function SWEP:OnRemove()
    if SERVER then
        local ply = self:GetOwner()
        if IsValid(ply) then
            for boneName, _ in pairs(CuffedBones) do
                local boneId = ply:LookupBone(boneName)
                if boneId then
                    ply:ManipulateBoneAngles(boneId, Angle(0,0,0))
                end
            end
        end
    end
end

if CLIENT then
    function SWEP:PreDrawViewModel(vm) return true end

    function SWEP:DrawWorldModel()
        local ply = self:GetOwner()
        if not IsValid(ply) then return end
        local boneindex = ply:LookupBone("ValveBiped.Bip01_L_Hand") 
        if boneindex then
            local pos, ang = ply:GetBonePosition(boneindex)
            if pos and ang then
                -- Hardcoded tuned values
                local cx, cy, cz = 0.9, 0.8, 4.8
                local cp, cyaw, cr = 7.1, -14.3, 96.2

                local offset = ang:Forward() * cx + ang:Right() * cy + ang:Up() * cz
                ang:RotateAroundAxis(ang:Right(), cp)
                ang:RotateAroundAxis(ang:Up(), cyaw)
                ang:RotateAroundAxis(ang:Forward(), cr)
                self:SetRenderOrigin(pos + offset)
                self:SetRenderAngles(ang)
                self:DrawModel()
            end
        end
    end
end

if SERVER then
    hook.Add("PlayerSwitchWeapon", "MRP_Handcuffs_PreventSwitch", function(ply, oldWep, newWep)
        if ply:GetNWBool("rhc_cuffed", false) and newWep:GetClass() ~= "weapon_r_cuffed" then
            return true -- Block switching weapons when cuffed
        end
    end)
end

if CLIENT then
    function SWEP:DrawHUD()
        draw.SimpleTextOutlined("Вы закованы в наручники","Trebuchet24",ScrW()/2,ScrH() - 100,Color(255,100,100,255), TEXT_ALIGN_CENTER, TEXT_ALIGN_BOTTOM,2,Color(0,0,0,255))
    end
end
