-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
AddCSLuaFile()

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.HealHere",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_healhere.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_healhere_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.OutOfMedic",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_outofmedic.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/auto_moodgp_tm_outofmedic_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.VO.English.Grunt.NoFilter.RequestMedic",
	["channel"] = CHAN_VOICE ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = { 
		"v92/bf2/vo/english/grunt/no_filter/PLAYER_REQUEST_TM_requestmedic.ogg" ,
		"v92/bf2/vo/english/grunt/no_filter/PLAYER_REQUEST_TM_requestmedic_alt.ogg"
	}
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Medikit.Pickup",
	["channel"] = CHAN_ITEM ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = 100 ,
	["sound"] = "v92/bf2/common/pick_up_medipack.wav"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Medikit.Deploy" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 },
	["sound"] = "v92/bf2/weapons/handheld/medikit/medikit_deploy.wav"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Medikit.Holster" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 } ,
	["sound"] = "v92/bf2/weapons/handheld/medikit/medikit_deploy.wav"
})

sound.Add({	["name"] = "BF2.Weapon.Handheld.Medikit.Throw" ,
	["channel"] = CHAN_BODY ,
	["volume"] = 1.0 ,
	["level"] = 85 ,
	["pitch"] = { 95 , 105 } ,
	["sound"] = "v92/bf2/weapons/handheld/medikit/throw.wav"
})

if CLIENT then

	local strEntityName = "v92_bf2_medikit"
	local strInfoName = "Medical Kit"

	SWEP.Category = "Utilities"
	SWEP.PrintName = strInfoName
	SWEP.Author = "V92"
	SWEP.Contact = "Steam"
	SWEP.Purpose = "Healing Nearby Units"
	SWEP.Instructions = "Primary: Надеть/снять аптечку на себя\nUse+Primary: Вылечить себя\n"
	SWEP.Slot = 4
	SWEP.SlotPos = 32
	SWEP.WepSelectIcon = surface.GetTextureID("vgui/hud/" .. strEntityName )
	SWEP.ViewModelFOV = 67
	SWEP.BounceWeaponIcon = false

	language.Add( strEntityName, strInfoName )	
	killicon.Add( strEntityName, "vgui/entities/".. strEntityName , Color( 255, 255, 255 ) )

end

SWEP.Weight = 1
SWEP.Spawnable = true
SWEP.AdminOnly = false
SWEP.UseHands = true
SWEP.ViewModel = Model("models/v92/bf2/weapons/handheld/medikit_c.mdl")
SWEP.WorldModel = Model("models/v92/bf2/weapons/handheld/medikit_w.mdl")

SWEP.Primary.ClipSize = 100
SWEP.Primary.DefaultClip = 100
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"

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

SWEP.HealAmount = GetConVarNumber("sk_healthkit")
SWEP.MaxAmmo = 100

local ThrowSound = Sound( "BF2.Weapon.Handheld.Medikit.Throw" )
local HealSound = Sound( "BF2.Weapon.Handheld.Medikit.Pickup" )
local HealSoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.HealHere" )
local EmptySoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.OutOfMedic" )
local RequestSoundVO = Sound( "BF2.VO.English.Grunt.NoFilter.RequestMedic" )

function SWEP:Initialize()
	self:SetHoldType( "slam" )
	self.HealAmount = GetConVarNumber("sk_healthkit")

	if CLIENT then 
		return
	end

	timer.Create( "medkit_ammo" .. self:EntIndex() , 10 , 0 , function()
		if ( self:Clip1() < self.MaxAmmo ) then 
			self:SetClip1( math.min( self:Clip1() + 5 , self.MaxAmmo ) ) 
		end
	end )
end

function SWEP:PrimaryAttack()
    if CLIENT then return end

    local owner = self.Owner
    if not IsValid(owner) then return end

    local medikitOnBack = nil
    for _, ent in ipairs(ents.FindByClass("v92_bf2_medikit_ent")) do
        if IsValid(ent) and ent.IsAttached and ent.AttachedPlayer == owner then
            medikitOnBack = ent
            break
        end
    end

    if medikitOnBack then

        if owner:KeyDown(IN_USE) then
            owner:ChatPrint("[!] Нельзя лечить себя, пока аптечка на спине!")
            self:SetNextPrimaryFire(CurTime() + 0.5)
            return
        end

        medikitOnBack:DetachFromPlayer()
        self:SetNextPrimaryFire(CurTime() + 1)
        return
    end

    if owner:KeyDown(IN_USE) then
        local need = GetConVarNumber("sk_healthkit")
        need = math.min(owner:GetMaxHealth() - owner:Health(), self.HealAmount)

        if self:Clip1() >= need and owner:Health() < owner:GetMaxHealth() then
            self:TakePrimaryAmmo(need)
            owner:SetHealth(math.min(owner:GetMaxHealth(), owner:Health() + need))

            self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
            self:SetNextPrimaryFire(CurTime() + self:SequenceDuration() + 0.5)
            owner:SetAnimation(PLAYER_ATTACK1)
        else
            if need > self.HealAmount then
                owner:EmitSound(EmptySoundVO)
            end
            self:SetNextPrimaryFire(CurTime() + 1)
        end

        return
    end

    local deployEnt = ents.Create("v92_bf2_medikit_ent")
    if not IsValid(deployEnt) then
        self:SetNextPrimaryFire(CurTime() + 0.5)
        return
    end

    deployEnt:SetPos(owner:GetPos() + Vector(0, 0, 50))
    deployEnt:SetAngles(owner:GetAngles())
    deployEnt:SetOwner(owner)
    deployEnt:Spawn()

    if deployEnt.AttachToPlayer and deployEnt:AttachToPlayer(owner) then
        self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
        self:SetNextPrimaryFire(CurTime() + self:SequenceDuration() + 0.5)
        owner:SetAnimation(PLAYER_ATTACK1)
    else
        self:SetNextPrimaryFire(CurTime() + 0.5)
    end
end

function SWEP:SecondaryAttack()
    return
end

function SWEP:OnRemove()
	timer.Stop( "medkit_ammo" .. self:EntIndex() )
end

function SWEP:Holster()
	return true
end

function SWEP:CustomAmmoDisplay()
	self.AmmoDisplay = self.AmmoDisplay or {} 
	self.AmmoDisplay.Draw = true

	if self.Primary.ClipSize > 0 then
		self.AmmoDisplay.PrimaryClip = self:Clip1()
		self.AmmoDisplay.PrimaryAmmo = self:Ammo1()
	end
	
	if self.Secondary.ClipSize > 0 then
		self.AmmoDisplay.SecondaryAmmo = self:Ammo2()
	end

	return self.AmmoDisplay
end
-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
