PLUGIN.captureData = PLUGIN.captureData or {}

net.Receive("ixCaptureSync", function()
	local plugin = ix.plugin.Get("capture")
	if (!plugin) then return end
	
	local point = net.ReadEntity()
	local progress = net.ReadFloat()
	local owner = net.ReadUInt(8)
	local capturing = net.ReadUInt(8)
	local players = net.ReadUInt(8)
	local blocked = net.ReadBool()
	
	if (IsValid(point)) then
		plugin.captureData[point] = {
			progress = progress,
			owner = owner,
			capturing = capturing,
			players = players,
			blocked = blocked
		}
	end
end)

local PANEL = {}

function PANEL:Init()
	self.progress = 0
	self.targetProgress = 0
	self.alpha = 0
	self.targetAlpha = 0
	self.blinkAlpha = 0
end

function PANEL:SetCaptureData(data)
	self.targetProgress = data.progress or 0
	self.owner = data.owner or 0
	self.capturing = data.capturing or 0
	self.players = data.players or 0
	self.blocked = data.blocked or false
	
	if (data.capturing and data.capturing > 0) then
		self.targetAlpha = 255
	else
		self.targetAlpha = 0
	end
end

function PANEL:Think()
	self.progress = Lerp(FrameTime() * 5, self.progress, self.targetProgress)
	self.alpha = Lerp(FrameTime() * 8, self.alpha, self.targetAlpha)
	
	if (self.blocked) then
		self.blinkAlpha = math.abs(math.sin(CurTime() * 5)) * 255
	else
		self.blinkAlpha = 0
	end
	
	local plugin = ix.plugin.Get("capture")
	if (plugin) then
		self:SetSize(plugin.hudWidth, plugin.hudHeight)
		self:SetPos(ScrW() - plugin.hudWidth - plugin.hudPosX, plugin.hudPosY)
	end
end

function PANEL:Paint(w, h)
	if (self.alpha < 1) then return end
	
	surface.SetAlphaMultiplier(self.alpha / 255)
	
	local barW = 40
	local barH = h
	local padding = 10
	local barX = (w - barW) / 2
	
	draw.RoundedBox(8, barX, 0, barW, barH, ColorAlpha(Color(20, 20, 20), self.alpha * 0.9))
	
	if (self.progress > 0) then
		local fillH = (barH - 4) * (self.progress / 100)
		local color = Color(100, 150, 255)
		
		if (self.capturing and self.capturing > 0) then
			if (self.capturing == FACTION_RUSSIAN) then
				color = Color(200, 50, 50)
			elseif (self.capturing == FACTION_UKRAINE) then
				color = Color(50, 100, 200)
			end
		end
		
		if (self.blocked) then
			color = ColorAlpha(color, self.blinkAlpha)
		end
		
		draw.RoundedBox(6, barX + 2, 2, barW - 4, fillH, ColorAlpha(color, self.alpha))
	end
	
	local text = string.format("%.0f%%", self.progress)
	if (self.players and self.players > 0) then
		text = text .. "\n(" .. self.players .. ")"
	end
	
	draw.SimpleText(text, "DermaDefault", w / 2, barH + 15, ColorAlpha(color_white, self.alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_TOP)
	
	surface.SetAlphaMultiplier(1)
end

vgui.Register("ixCaptureUI", PANEL, "DPanel")

function PLUGIN:HUDPaint()
	local ply = LocalPlayer()
	if (!IsValid(ply)) then return end
	
	for point, data in pairs(self.captureData) do
		if (IsValid(point) and ply:GetPos():Distance(point:GetPos()) <= self.captureRadius) then
			if (!IsValid(self.captureUI)) then
				self.captureUI = vgui.Create("ixCaptureUI")
			end
			
			self.captureUI:SetCaptureData(data)
			return
		end
	end
	
	if (IsValid(self.captureUI)) then
		self.captureUI:SetCaptureData({progress = 0, owner = 0, capturing = 0, players = 0, blocked = false})
	end
end

function PLUGIN:OnReloaded()
	if (IsValid(self.captureUI)) then
		self.captureUI:Remove()
		self.captureUI = nil
	end
end

