
--[[
	NOTE:
	All modification done by the client will be directly apply for him, and after for the server who reaply the new settings for all clients
]]

net.Receive("PLibs:Player:OpenConfig",function()
	local Frame = PLibs.CreateFrame(_,_,300,100,true,"Config Panel")
	local Libs = PLibs.GetLibsFile()
	local modify = false


	local Scroll = vgui.Create("DScrollPanel",Frame)
	Scroll:SetSize(PLibs.SizeToClient(970,390))
	Scroll:SetPos(PLibs.SizeToClient(15, 100))

	local List = vgui.Create("DIconLayout",Scroll)
	List:SetSize(PLibs.SizeToClient(970,440))
	List:SetSpaceX(0)
	List:SetSpaceY(10)

	local function DrawConfig(v,k)
		local config = List:Add("DPanel")
		local _,h = PLibs.SizeToClient(0,30)
		config:SetSize(List:GetWide(),h)
		config:SetToolTip(v.desc)
		config.Paint = function(self,w,h)
			surface.SetDrawColor(color_white)
			surface.DrawLine(5, h-1, w-5, h-1)
			local x = PLibs.SizeToClient(5)
			draw.SimpleText(v.name,"PLibs:Size:24",x,h/2,color_white,TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER)
		end

		local w,h = PLibs.SizeToClient(50,20)
		local x = PLibs.SizeToClient(5)
		PLibs.CreateButtons(config,config:GetWide()-w-x,config:GetTall()/2-h/2,w,h,"Reset",function()
			v.option = v.default
			Frame:Close()
			PLibs.EditLibsFile(Libs)
		end)

		local types = type(v.option)
		local option = vgui.Create("DTextEntry",config)
	
		if table.HasValue({"number", "string", "table"}, types) then
			option:SetSize(PLibs.SizeToClient(150, 20))

			local x,y = PLibs.SizeToClient(75,10)
			option:SetPos(config:GetWide()/2-x, config:GetTall()/2-y)

			if types == "string" then
				option:SetText(v.option)
			elseif types == "number" then
				option:SetText(v.option)
				option.AllowInput = function( self, stringValue )
					return tonumber(stringValue)==nil and true or false
				end
			elseif types == "table" then
				local remove = vgui.Create("DComboBox",config)
				remove:SetSize(PLibs.SizeToClient(150,20))
				local x = PLibs.SizeToClient(650)
				local ox, oy = option:GetPos()
				remove:SetPos(x,oy)
				for k,v in pairs(v.option) do
					if istable(v) then remove:AddChoice(v.name, k)
					else remove:AddChoice(v, k) end
				end

				local w,h = PLibs.SizeToClient(20,20)
				local x = PLibs.SizeToClient(805)
				PLibs.CreateButtons(config,x,oy,w,h,"-",function()
					local _,index = remove:GetSelected()
					table.remove(v.option, index)

					remove:Clear()

					for k,v in pairs(v.option) do
						if istable(v) then remove:AddChoice(v.name, k)
						else remove:AddChoice(v, k) end
					end

					modify = true
				end)

				if v.notDraw then option:SetVisible(false) return end

				local w,h = PLibs.SizeToClient(50,20)
				PLibs.CreateButtons(config,ox+option:GetWide()+PLibs.SizeToClient(5),oy,w,h,"+",function()
					local index = table.insert(v.option, option:GetText())
					remove:AddChoice(option:GetText(),index)
					option:SetText("")
					modify = true
				end)
				return
			end

			local ox,oy = option:GetPos()
			local w,h = PLibs.SizeToClient(50,20)
			PLibs.CreateButtons(config,ox+option:GetWide()+PLibs.SizeToClient(5),oy,w,h,"Done",function()
				v.option = option:GetText()
				modify = true
			end)
		elseif types == "bool" then
			option = vgui.Create("DCheckBox")
			option:SetPos(config:GetWide()/2, config:GetTall()/2)
			option:SetValue(v.option)
			option.OnChange = function(val)
				v.option = val
				modify = true
			end
		end
	end

	local function DrawLanguage(v,k,a)
		local config = List:Add("DPanel")
		local _,h = PLibs.SizeToClient(0,(30*table.Count(v))+20)
		config:SetSize(List:GetWide()/2,h)
		config.Paint = function(self,w,h)
			local x,y = PLibs.SizeToClient(5, 6)
			draw.SimpleText(k,"PLibs:Size:24",x,y,color_white,TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER)
			local y1 = 25
			for key,val in pairs(v) do
				local x,y = PLibs.SizeToClient(10, y1)
				draw.SimpleText(key,"PLibs:Size:22",x,y,color_white,TEXT_ALIGN_LEFT)
				y1 = y1 + 25
			end
		end

		local y1 = 25
		for langue,val in pairs(v) do
			local x,y = PLibs.SizeToClient(10, y1)
			local option = vgui.Create("DTextEntry",config)
			option:SetSize(PLibs.SizeToClient(250,20))
			option:SetPos(100, y1)
			option:SetText(val)

			local w,h = PLibs.SizeToClient(50,20)
			PLibs.CreateButtons(config,option:GetPos()+option:GetWide()+PLibs.SizeToClient(5),y1,w,h,"Done",function()
				v[langue] = option:GetText()
				modify = true
			end)
			y1 = y1 + 25
		end
	end

	local function REsizeFrame(y)
		Frame:SetSize(PLibs.SizeToClient(1000,y))
		Frame:Center()

		for _,v in pairs(Frame:GetChildren()) do -- This use to redifine the function of quit from the button, to apply all modification done to the server
			if v:GetName() == "quit" then
				v:SetPos(PLibs.SizeToClient(1000-23,3))
			end
		end
	end

	local x,y = PLibs.SizeToClient(20, 40)
	local w,h = PLibs.SizeToClient(100, 30)
	local lang = vgui.Create("DButton",Frame)
	local conf = vgui.Create("DButton",Frame)

	local Export = PLibs.CreateButtons(Frame,0,0,w,h,"Export",function()
		RunConsoleCommand("config_newaddons","export_language")
	end)
	Export:SetVisible(false)

	conf:Remove()
	conf = PLibs.CreateButtons(Frame,Frame:GetWide()/2-(w+x),y,w,h,"Config",function(self)
		List:Clear()
		Export:SetVisible(false)
		local c = 0
		for _,f in SortedPairs(Libs["config"]) do
			for k,v in SortedPairs(f) do
				c = c + 50
				DrawConfig(v,k)
			end
		end

		local y1 = 50+c
		if y1 > 500 then y1 = 500 end
		REsizeFrame(y1)

		local x1 = PLibs.SizeToClient(500)
		self:SetPos(x1-(w+x),y)
		lang:SetPos(x1+x,y)
	end)

	lang:Remove()
	lang = PLibs.CreateButtons(Frame,Frame:GetWide()/2+x,y,w,h,"Language",function(self)
		List:Clear()
		Export:SetVisible(true)
		local x2 = PLibs.SizeToClient(130)
		Export:SetPos(Frame:GetWide()-x2,y)

		local c = 0
		for _,f in SortedPairs(Libs["language"]) do
			for k,v in SortedPairs(f) do
				c = c + 50
				DrawLanguage(v,k,f)
			end
		end
		local y1 = 100+c
		if y1 > 500 then y1 = 500 end
		REsizeFrame(y1)

		local x1 = PLibs.SizeToClient(500)
		conf:SetPos(x1-(w+x),y)
		self:SetPos(x1+x,y)
	end)

	for _,v in pairs(Frame:GetChildren()) do -- This use to redifine the function of quit from the button, to apply all modification done to the server
		if v:GetName() == "quit" then
			v.DoClick = function()
				Frame:Close()
				if !modify then return end
				PLibs.EditLibsFile(Libs)
			end
		end
	end
end)