AddCSLuaFile()

SWEP.Base = "weapon_base"

if CLIENT then 
    SWEP.DrawWeaponInfoBox = false
	SWEP.BounceWeaponIcon = false
end

SWEP.PrintName = "Consumable"
SWEP.Author = "Helix"
SWEP.Purpose = "Base consumable SWEP with animations"
SWEP.Category = "Helix"

SWEP.ViewModelFOV = 70
SWEP.ViewModel = "models/weapons/v_hands.mdl"
SWEP.WorldModel = ""
SWEP.UseHands = true
SWEP.DrawCrosshair = false 

SWEP.Spawnable = false
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
SWEP.Slot = 5
SWEP.SlotPos = 0

SWEP.SwayScale = 0
SWEP.BobScale = 0

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
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.ItemHunger = 0
SWEP.ItemThirst = 0
SWEP.ItemAlcohol = 0
SWEP.ItemEmpty = false
SWEP.ItemType = "food" -- "food", "drink", "consumable"

function SWEP:Initialize()
    self:SetHoldType("slam")
	self.Consuming = false
end 

function SWEP:SetItemData(hunger, thirst, alcohol, empty, itemType, viewModel)
	self.ItemHunger = hunger or 0
	self.ItemThirst = thirst or 0
	self.ItemAlcohol = alcohol or 0
	self.ItemEmpty = empty or false
	self.ItemType = itemType or "food"
	
	-- Устанавливаем viewmodel, если указана
	if viewModel and viewModel != "" then
		self.ViewModel = viewModel
	end
	print(self.ViewModel)
end

function SWEP:Deploy()
    local owner = self:GetOwner() 
	
	if not IsValid(owner) or not owner:IsPlayer() then 
		return false 
	end
	
	-- Проигрываем idle анимацию
	local vm = owner:GetViewModel()
	if IsValid(vm) then
		local idleSeq = vm:LookupSequence("idle")
		if idleSeq > 0 then
			vm:SendViewModelMatchingSequence(idleSeq)
		end
	end
	
	-- Начинаем процесс употребления
	self.Consuming = true
	self:InitializeConsumable()
	
	return true
end

function SWEP:InitializeConsumable()
    local owner = self:GetOwner()
    if not IsValid(self) or not IsValid(owner) or not owner:IsPlayer() then return end

	-- Проигрываем анимацию use
	local vm = owner:GetViewModel()
	local sequenceDuration = 3
	
	if IsValid(vm) then
		local useSeq = vm:LookupSequence("use")
		if useSeq > 0 then
			vm:SendViewModelMatchingSequence(useSeq)
			sequenceDuration = vm:SequenceDuration(useSeq)
		end
	end
	
	-- Применяем эффекты в середине анимации
	timer.Simple(sequenceDuration * 0.5, function()
        if IsValid(self) and IsValid(owner) and owner:Alive() then
			self:ApplyEffects(owner)
        end
    end)

    -- Завершаем употребление и удаляем оружие
    timer.Simple(sequenceDuration, function()
        if IsValid(self) and IsValid(owner) and owner:Alive() then
            self.Consuming = false
			
			if SERVER then 
				owner:StripWeapon(self:GetClass())
				
				local prev = owner:GetPreviousWeapon() or ""
				if isentity(prev) and IsValid(prev) then
					owner:SelectWeapon(prev:GetClass())
				elseif isstring(prev) and prev != "" then
					owner:SelectWeapon(prev)
				end
			end
        end
    end)
end

function SWEP:ApplyEffects(owner)
	if not SERVER then return end
	if not IsValid(owner) then return end
	
	local char = owner:GetCharacter()
	if not char then return end
	
	-- Получаем текущие значения
	local hunger = char:GetData("hunger", 100)
	local thirst = char:GetData("thirst", 100)
	
	-- Применяем изменения (SetHunger и SetThirst автоматически обновляют состояние)
	owner:SetHunger(hunger + self.ItemHunger)
	owner:SetThirst(thirst + self.ItemThirst)
	
	-- Алкоголь
	if self.ItemAlcohol > 0 then
		owner:IncreaseDrunkLevel(self.ItemAlcohol)
	end
	
	-- Пустая тара
	if self.ItemEmpty then
		local inv = char:GetInventory()
		if inv then
			inv:Add(self.ItemEmpty)
		end
	end
	
	-- Звук употребления из STALKER 2 Consumables
	if self.ItemType == "drink" then
		owner:EmitSound("Stalker2.Drink")
	elseif self.ItemType == "food" then
		owner:EmitSound("Stalker2.Eat")
	elseif self.ItemType == "medical" then
		owner:EmitSound("Stalker2.Healing")
	end
end

function SWEP:PrimaryAttack()
	-- Ничего не делаем
end

function SWEP:SecondaryAttack()
	-- Ничего не делаем
end

function SWEP:Reload()
	-- Ничего не делаем
end

function SWEP:Think()
	-- Можно добавить дополнительную логику
end

function SWEP:Holster()
	-- Блокируем переключение во время употребления
	if self.Consuming then 
		return false 
	end
	return true
end

function SWEP:OnRemove()
	-- Очистка
end
