require "preferenceGUI\\locals";

--[[ FIXME: Broken English


Proxy change sample

Before use this, you must add lines such below:

   proxy["gateway"] = {
       host = "192.168.0.1",
       port = 8080
   }
   proxy["local"] = {
       host = "127.0.0.1",
       port = 8080
   }

and add menu item into menu.cfg such below:

   luamacro(proxy.change(no), No Proxy)
   luamacro(proxy.change(gateway), Default Proxy)
   luamacro(proxy.change(local), Local Proxy)

]]

require "compat"
require "hook"
require "dialog"
require "sync"
require "setmenu"
require "kmplus"

proxy = {
	pref = "kmeleon.plugins.luamacro.proxy"
}

function proxy.check(set)
	if set then
		if proxy.current then
			setcheck("luamacro(proxy.change(" .. proxy.current .. ")", false)
		end
		proxy.current = set
		setpref(TYPE_STRING, proxy.pref, set)
		setcheck("luamacro(proxy.change(" .. set .. ")", true)
	end
end

function proxy.change(set)
	if set == "no" or set == "false" then
		setpref(TYPE_INT, "network.proxy.type", 0)
		proxy.check(set)
	elseif proxy[set] then
		setpref(TYPE_STRING, "network.proxy.ftp", proxy[set].host)
		setpref(TYPE_INT, "network.proxy.ftp_port", proxy[set].port)
		setpref(TYPE_STRING, "network.proxy.gopher", proxy[set].host)
		setpref(TYPE_INT, "network.proxy.gopher_port", proxy[set].port)
		setpref(TYPE_STRING, "network.proxy.http", proxy[set].host)
		setpref(TYPE_INT, "network.proxy.http_port", proxy[set].port)
		setpref(TYPE_STRING, "network.proxy.ssl", proxy[set].host)
		setpref(TYPE_INT, "network.proxy.ssl_port", proxy[set].port)
		setpref(TYPE_INT, "network.proxy.type", 1)
		proxy.check(set)
	end
end

if hook then
	hook.add(StartupHook, function ()
		proxy.check(getpref(TYPE_STRING, proxy.pref))
	end)
end



function SetProxy1()
	local proxy1 = prompt("Enter the Proxy 1", "Proxy 1")
	local port1 = prompt("Enter the Proxy 1 Port", "Proxy 1 Port")
	setpref(TYPE_STRING, "network.proxy1.http", proxy1)
	setpref(TYPE_INT, "network.proxy1.http_port", port1)
	SetStatusBarText("PROXY 1 " .. proxy1..":"..port1)
end

function SetProxy2()
	local proxy2 = prompt("Enter the Proxy 2", "Proxy 2")
	local port2 = prompt("Enter the Proxy 2 Port", "Proxy 2 Port")
	setpref(TYPE_STRING, "network.proxy2.http", proxy2)
	setpref(TYPE_INT, "network.proxy2.http_port", port2)
	SetStatusBarText("PROXY 2 " .. proxy2..":"..port2)
end

function SetProxy3()
	local proxy1 = prompt("Enter the Proxy 3", "Proxy 3")
	local port1 = prompt("Enter the Proxy 3 Port", "Proxy 3 Port")
	setpref(TYPE_STRING, "network.proxy3.http", proxy3)
	setpref(TYPE_INT, "network.proxy3.http_port", port3)
	SetStatusBarText("PROXY 3 " .. proxy3..":"..port3)
end


function proxy1 ()
	local proxy = getpref(TYPE_STRING, "network.proxy1.http");
	local port = getpref(TYPE_INT, "network.proxy1.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port)
	sync()
end


function proxy2 ()
	local proxy = getpref(TYPE_STRING, "network.proxy2.http");
	local port = getpref(TYPE_INT, "network.proxy2.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port);
	sync()
end


function proxy3 ()
	local proxy = getpref(TYPE_STRING, "network.proxy3.http");
	local port = getpref(TYPE_INT, "network.proxy3.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port);
	sync()
end

function proxy4 ()
	local proxy = getpref(TYPE_STRING, "network.proxy4.http");
	local port = getpref(TYPE_INT, "network.proxy4.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port)
	sync()
end

function proxy5 ()
	local proxy = getpref(TYPE_STRING, "network.proxy5.http");
	local port = getpref(TYPE_INT, "network.proxy5.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port)
	sync()
end

function proxy6 ()
	local proxy = getpref(TYPE_STRING, "network.proxy6.http");
	local port = getpref(TYPE_INT, "network.proxy6.http_port");
	setpref(TYPE_INT, "network.proxy.type", 1);
	setpref(TYPE_STRING, "network.proxy.http", proxy);
	setpref(TYPE_INT, "network.proxy.http_port", port);
	SetStatusBarText("Use Proxy " .. proxy..":"..port)
	sync()
end


function NoProxy ()
	setpref(TYPE_INT, "network.proxy.type", 0);
	SetStatusBarText("Do not use Proxy");
	sync()
end

local proxy=create_dialog({
	width=240,
	height=145,
	caption="Proxy",

	{
		type="button",
		style=w32dlg.BS_DEFPUSHBUTTON,
		x=65,
		y=105,
		width = 40,
		height = 15,
		id = "IDOK",
		caption = "OK"
	},
	
	{
		type="button",
		style=w32dlg.BS_DEFPUSHBUTTON,
		x=125,
		y=105,
		width = 40,
		height = 15,
		id = "IDCANCEL",
		caption = "CANCEL"
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 20,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT1",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 20,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME1",    -- ID
		caption = "NAME"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 33,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT2",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 33,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME2",    -- ID
		caption = "NAME"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 46,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT3",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 46,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME3",    -- ID
		caption = "NAME"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 59,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT4",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 59,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME4",    -- ID
		caption = "NAME"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 72,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT5",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 72,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME5",    -- ID
		caption = "NAME"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 30,            -- x position
		y = 85,            -- y position
		width = 120,        -- width
		height = 10,       -- height
		id = "IDEDIT6",    -- ID
		caption = "EDIT"   -- caption
	},

	{
		type = "edit",     -- control type
		style = w32dlg.WS_BORDER,
		x = 160,            -- x position
		y = 85,            -- y position
		width = 60,        -- width
		height = 10,       -- height
		id = "IDNAME6",    -- ID
		caption = "NAME"   -- caption
	},



	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 20,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC1",       -- ID
		caption = "1:"
	},

	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 33,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC2",       -- ID
		caption = "2:"
	},

	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 46,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC3",       -- ID
		caption = "3:"
	},

	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 59,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC4",       -- ID
		caption = "4:"
	},

	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 72,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC5",       -- ID
		caption = "5:"
	},

	{
		type = "static",        -- control type
		x = 20,                 -- x position
		y = 85,                -- y position
		width = 10,             -- width
		height = 10,            -- height
		id = "IDSTATIC6",       -- ID
		caption = "6:"
	},

	{
		type = "static",        -- control type
		x = 62,                 -- x position
		y = 7,                -- y position
		width = 80,             -- width
		height = 10,            -- height
		id = "IDSTATIC7",       -- ID
		caption = "Proxy and Port"
	},

	{
		type = "static",        -- control type
		x = 180,                 -- x position
		y = 7,                -- y position
		width = 60,             -- width
		height = 10,            -- height
		id = "IDSTATIC7",       -- ID
		caption = "Name"
	},

})

function proxy_dialog()
	local h={"none","none","none","none","none","none"}
	local n={"none","none","none","none","none","none"}
	for i,v in ipairs(h) do
		local address=getpref(TYPE_STRING,"network.proxy"..i..".http")
		local port=getpref(TYPE_INT,"network.proxy"..i..".http_port")
		local name=getpref(TYPE_STRING,"network.proxy"..i..".name")
		h[i]=address..":"..port
		n[i]=name
	end
	local r,rr=proxy("Proxy",{
				IDEDIT1=h[1],
				IDEDIT2=h[2],
				IDEDIT3=h[3],
				IDEDIT4=h[4],
				IDEDIT5=h[5],
				IDEDIT6=h[6],
				IDNAME1=n[1];
				IDNAME2=n[2];
				IDNAME3=n[3];
				IDNAME4=n[4];
				IDNAME5=n[5];
				IDNAME6=n[6];
	})
	if r=="IDOK" then
		local p={}
		local g={}
		local sep=string.find(rr.IDEDIT1,":")
		p[1]=string.sub(rr.IDEDIT1,1,sep-1)
		g[1]=string.sub(rr.IDEDIT1,sep+1)
		sep=string.find(rr.IDEDIT2,":")
		p[2]=string.sub(rr.IDEDIT2,1,sep-1)
		g[2]=string.sub(rr.IDEDIT2,sep+1)
		sep=string.find(rr.IDEDIT3,":")
		p[3]=string.sub(rr.IDEDIT3,1,sep-1)
		g[3]=string.sub(rr.IDEDIT3,sep+1)
		sep=string.find(rr.IDEDIT4,":")
		p[4]=string.sub(rr.IDEDIT4,1,sep-1)
		g[4]=string.sub(rr.IDEDIT4,sep+1)
		sep=string.find(rr.IDEDIT5,":")
		p[5]=string.sub(rr.IDEDIT5,1,sep-1)
		g[5]=string.sub(rr.IDEDIT5,sep+1)
		sep=string.find(rr.IDEDIT6,":")
		p[6]=string.sub(rr.IDEDIT6,1,sep-1)
		g[6]=string.sub(rr.IDEDIT6,sep+1)
		n[1]=rr.IDNAME1;n[2]=rr.IDNAME2;n[3]=rr.IDNAME3;n[4]=rr.IDNAME4;n[5]=rr.IDNAME5;n[6]=rr.IDNAME6;		
		for i,v in ipairs(p) do		
			setpref(TYPE_STRING,"network.proxy"..i..".http",p[i])
			setpref(TYPE_INT,"network.proxy"..i..".http_port",g[i])
			setpref(TYPE_STRING,"network.proxy"..i..".name",n[i])
		end
	proxy_menu()
	end				
end

function proxy_menu()
	local h={"none","none","none","none","none","none"}
	local p={}
	local g={}
	local n={"name","name","name","name","name","name"}
	for i,v in ipairs(h) do
		p[i]=getpref(TYPE_STRING,"network.proxy"..i..".http")
		g[i]=getpref(TYPE_INT,"network.proxy"..i..".http_port")
		h[i]=p[i]..":"..g[i]
		n[i]=getpref(TYPE_STRING,"network.proxy"..i..".name")
	end

	setmenu.modify("&Proxy", 1, _USEPROXY, {
			{n[1],"luamacro(proxy1)"},
			{n[2],"luamacro(proxy2)"},
			{n[3],"luamacro(proxy3)"},
			{n[4],"luamacro(proxy4)"},
			{n[5],"luamacro(proxy5)"},
			{n[6],"luamacro(proxy6)"},
	})

	if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy1.http") then SetCheck("luamacro(proxy1)",true) else SetCheck("luamacro(proxy1)",false) end
	if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy2.http") then SetCheck("luamacro(proxy2)",true) else SetCheck("luamacro(proxy2)",false) end
	if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy3.http") then SetCheck("luamacro(proxy3)",true) else SetCheck("luamacro(proxy3)",false) end
  if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy4.http") then SetCheck("luamacro(proxy4)",true) else SetCheck("luamacro(proxy4)",false) end 
	if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy5.http") then SetCheck("luamacro(proxy5)",true) else SetCheck("luamacro(proxy5)",false) end 
	if getpref(TYPE_INT, "network.proxy.type")~=0 and getpref(TYPE_STRING,"network.proxy.http")==getpref(TYPE_STRING,"network.proxy6.http") then SetCheck("luamacro(proxy6)",true) else SetCheck("luamacro(proxy6)",false) end 
end

