include("shared.lua")

function ENT:Draw()
	self:DrawModel()
	
	local pos = self:GetPos() + Vector(0, 0, 80)
	local ang = LocalPlayer():EyeAngles()
	ang:RotateAroundAxis(ang:Forward(), 90)
	ang:RotateAroundAxis(ang:Right(), 90)
	
	if LocalPlayer():GetPos():DistToSqr(self:GetPos()) <= 60000 then
		cam.Start3D2D(pos, ang, 0.05)
			draw.SimpleTextOutlined("Торговец", "DermaLarge", 0, 0, Color(25, 175, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
			draw.SimpleTextOutlined("Медикаменты", "DermaDefault", 0, 30, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, color_black)
		cam.End3D2D()
	end
end

net.Receive("MedicTerminalNPC_Menu", function()
	local npc = net.ReadEntity()
	
	if not IsValid(npc) then return end
	
	local frame = vgui.Create("DFrame")
	frame:SetTitle("Аптека")
	frame:SetSize(700, 500)
	frame:Center()
	frame:MakePopup()
	frame.Paint = function(self, w, h)
		draw.RoundedBox(4, 0, 0, w, h, Color(30, 30, 30, 240))
		draw.RoundedBox(0, 0, 0, w, 24, Color(20, 20, 20, 200))
	end
	
	local scroll = vgui.Create("DScrollPanel", frame)
	scroll:Dock(FILL)
	
	local layout = vgui.Create("DIconLayout", scroll)
	layout:Dock(FILL)
	layout:SetSpaceX(10)
	layout:SetSpaceY(10)
	
	local function AddItem(class, name, price, modelPath)
		local pnl = layout:Add("DPanel")
		pnl:SetSize(210, 160)
		pnl.Paint = function(self, w, h)
			draw.RoundedBox(4, 0, 0, w, h, Color(45, 45, 45, 200))
			draw.SimpleText(name, "DermaDefaultBold", w/2, 10, color_white, TEXT_ALIGN_CENTER)
			if ix and ix.currency then
				draw.SimpleText(ix.currency.Get(price), "DermaDefaultBold", w/2, 135, Color(100, 255, 100), TEXT_ALIGN_CENTER)
			else
				draw.SimpleText(price .. "$", "DermaDefaultBold", w/2, 135, Color(100, 255, 100), TEXT_ALIGN_CENTER)
			end
		end
		
		local mdl = vgui.Create("DModelPanel", pnl)
		mdl:SetSize(100, 100)
		mdl:SetPos(55, 25)
		mdl:SetModel(modelPath or "models/error.mdl")
		
		if IsValid(mdl.Entity) then
			local mn, mx = mdl.Entity:GetRenderBounds()
			local size = 0
			size = math.max(size, math.abs(mn.x) + math.abs(mx.x))
			size = math.max(size, math.abs(mn.y) + math.abs(mx.y))
			size = math.max(size, math.abs(mn.z) + math.abs(mx.z))
			
			mdl:SetFOV(45)
			mdl:SetCamPos(Vector(size, size, size))
			mdl:SetLookAt((mn + mx) * 0.5)
		end
		
		local btn = vgui.Create("DButton", pnl)
		btn:SetSize(210, 160)
		btn:SetText("")
		btn.Paint = function(self, w, h)
			if self:IsHovered() then
				draw.RoundedBox(4, 0, 0, w, h, Color(255, 255, 255, 15))
			end
		end
		btn.DoClick = function()
			net.Start("MedicTerminalNPC_Buy")
				net.WriteEntity(npc)
				net.WriteString(class)
			net.SendToServer()
		end
	end
	
	local items = npc:GetShopItems()
	
	if items then
		-- Сортировка по алфавиту имен
		local sortedKeys = {}
		for k, v in pairs(items) do table.insert(sortedKeys, k) end
		table.sort(sortedKeys, function(a, b) return (items[a].name or "") < (items[b].name or "") end)
		
		for _, class in ipairs(sortedKeys) do
			local data = items[class]
			AddItem(class, data.name, data.price, data.model)
		end
	end
end)
