ENT.Type = "anim"
ENT.PrintName = "Точка захвата"
ENT.Category = "[FT] Захват точек"
ENT.Spawnable = true
ENT.AdminOnly = true
ENT.RenderGroup = RENDERGROUP_BOTH
ENT.bNoPersist = true

function ENT:SetupDataTables()
	self:NetworkVar("Float", 0, "CaptureProgress")
	self:NetworkVar("Int", 0, "OwnerFaction")
	self:NetworkVar("Int", 1, "FlagSkin")
end

if SERVER then
	function ENT:Initialize()
		self:SetModel("models/flag/ua_flag_rework.mdl")
		self:PhysicsInit(SOLID_VPHYSICS)
		self:SetMoveType(MOVETYPE_NONE)
		self:SetSolid(SOLID_VPHYSICS)
		self:SetUseType(SIMPLE_USE)
		
		local phys = self:GetPhysicsObject()
		if (IsValid(phys)) then
			phys:EnableMotion(false)
		end
		
		-- Установка начального скина (никто не захватил)
		self:SetFlagSkin(2)
		self:SetSkin(2)
		self:SetOwnerFaction(0)
		
		local plugin = ix.plugin.Get("capture")
		if (plugin) then
			plugin:RegisterCapturePoint(self)
		end
	end
	
	-- Функция для установки владельца с автоматической сменой скина
	function ENT:SetOwnerFactionID(factionID)
		self:SetOwnerFaction(factionID or 0)
		
		local skinID = 2 -- По умолчанию никто
		
		if factionID == FACTION_RUSSIAN then
			skinID = 1 -- РФ - скин 1
		elseif factionID == FACTION_UKRAINE then
			skinID = 0 -- ВСУ - скин 0
		end
		
		self:SetFlagSkin(skinID)
		self:SetSkin(skinID)
	end
	
	function ENT:Use(activator, caller)
	end
else
	function ENT:Draw()
		self:DrawModel()
		
		-- Синхронизация скина с NetworkVar
		local skinID = self:GetFlagSkin()
		if skinID and self:GetSkin() ~= skinID then
			self:SetSkin(skinID)
		end
		
		--local plugin = ix.plugin.Get("capture")
		--if (!plugin or !plugin.captureData[self]) then return end
		--
		--local data = plugin.captureData[self]
		--local pos = self:GetPos() + Vector(0, 0, plugin.labelOffsetZ)
		--local ang = EyeAngles()
		--ang:RotateAroundAxis(ang:Forward(), 90)
		--ang:RotateAroundAxis(ang:Right(), 90)
		
		--cam.Start3D2D(pos, Angle(0, ang.y, 90), plugin.labelScale)
		--	if (data.capturing and data.capturing > 0) then
		--		local barW = 400
		--		local barH = 60
		--		local x, y = -barW / 2, -barH / 2
		--		
		--		draw.RoundedBox(8, x, y - 80, barW, barH, Color(20, 20, 20, 230))
		--		
		--		local fillW = (barW - 8) * (data.progress / 100)
		--		local color = Color(100, 150, 255)
		--		
		--		if (data.capturing == FACTION_RUSSIAN) then
		--			color = Color(200, 50, 50)
		--		elseif (data.capturing == FACTION_UKRAINE) then
		--			color = Color(50, 100, 200)
		--		end
		--		
		--		draw.RoundedBox(6, x + 4, y - 76, fillW, barH - 8, color)
		--		
		--		local text = string.format("%.0f%%", data.progress)
		--		draw.SimpleText(text, "DermaLarge", 0, -80, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		--	end
		--	
		--	local ownerText = "Никем не захвачена"
		--	local ownerColor = Color(150, 150, 150)
		--	
		--	if (data.owner and data.owner > 0) then
		--		local faction = ix.faction.Get(data.owner)
		--		if (faction) then
		--			ownerText = faction.name
		--			if (data.owner == FACTION_RUSSIAN) then
		--				ownerColor = Color(200, 50, 50)
		--			elseif (data.owner == FACTION_UKRAINE) then
		--				ownerColor = Color(50, 100, 200)
		--			end
		--		end
		--	end
		--	
		--	draw.SimpleText(ownerText, "DermaLarge", 0, 0, ownerColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		--cam.End3D2D()
	end
end
