require "preferenceGUI\\locals";

-- CCF Groups lua macros version 0.1

require "dialog"
require "compat"

local g_pref1="kmeleon.groups.name"
local g_pref2="kmeleon.groups."
local g_pref3="kmeleon.groups.startup"
local g_pref4="kmeleon.startupgroups.enabled"
local g_pref5="kmeleon.groups.last"

local names=""

local path = BROWSERPATH.."pref\\"


local d = create_dialog({
	width = 180,         -- width
	height = 165,        -- height
	caption = "dialog",  -- caption
	{
		type = "button",    -- control type
		style = w32dlg.BS_DEFPUSHBUTTON,
		x = 10,              -- x position
		y = 130,              -- y position
		width = 40,         -- width
		height = 10,        -- height
		id = "IDBUTTONOK",    -- ID
		caption = _GOPEN      -- caption
	},
	{
		type = "button",    -- control type
		style = w32dlg.BS_DEFPUSHBUTTON,
		x = 60,              -- x position
		y = 130,              -- y position
		width = 40,         -- width
		height = 10,        -- height
		id = "IDBUTTONCANCEL",    -- ID
		caption = _GCANCEL      -- caption
	},
	{
		type = "button",    -- control type
		style = w32dlg.BS_DEFPUSHBUTTON,
		x = 110,              -- x position
		y = 130,              -- y position
		width = 60,         -- width
		height = 10,        -- height
		id = "IDBUTTONREMOVE",    -- ID
		caption = _GREMOVE      -- caption
	},
	{
		type = "listbox",          -- control type
		style = w32dlg.WS_BORDER +
		        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
		        w32dlg.LBS_NOTIFY,
		x = 20,                  -- x position
		y = 5,                 -- y position
		width = 140,             -- width
		height = 120,            -- height
		id = "IDLIST",         -- ID
	},
})


function gaddname(gname)
	local names=getpref(TYPE_STRING,g_pref1)
	names=names..gname.."\t"
	setpref(TYPE_STRING,g_pref1,names)
end

function gcreategroup()
	gname=prompt("Please input group name:","Group name","")
	if gname then
	gaddname(gname)
	--pluginmsg("utils","write",path..gname..".group","")
	local f = assert(io.open(path..gname..".group","w"))
	f:write("")
	f:close()
	end
	group_menu()
end

local function gg()
	local names=getpref(TYPE_STRING,g_pref1)
	local len=string.len(names)
	local i=1
	local groups={}
	local j=1
	while (i<len) do
		local p=string.find(names,"\t")
		groups[j]=string.sub(names,1,p-1)
		names=string.sub(names,p+1)
		i=i+p
		j=j+1
	end
	local idx,gn=choose("Group names","Groups",unpack(groups))
	return idx,gn
end


function gsplit()
	local idx,gn=gg()
	return idx,gn
end

function gdelete()
	local names=getpref(TYPE_STRING,g_pref1)
	local idx,del=gg()
	if idx then
		local p=string.find(names,del)
		local len=string.len(del)
		names=string.sub(names,1,p-1)..string.sub(names,p+len+1)
		setpref(TYPE_STRING,g_pref1,names)
		--pluginmsg("utils","unlink",path..del..".group")
		os.remove(path..del..".group")
	end
	group_menu()
end

function gaddurl()
	local title, url = GetDocInfo(hwnd)
	local idx,gn=gg()
	names=getpref(TYPE_STRING,g_pref1)	
	if idx then
		local fname=path..gn..".group"
		--local url1=pluginmsgex("utils","read",fname,TYPE_STRING)
		local f = assert(io.open(fname,"r"))
		url1=f:read("*all")
		f:close()
		if url1 then
			url1=url1..title..":\t"..url.."\r\n"
		else
			url1=title..":\t"..url.."\r\n"
		end
		--pluginmsg("utils", "write", fname,url1)
		local f = assert(io.open(fname,"w"))
		f:write(url1)
		f:close()
	end
end

local function read(gn)
	local groups={title={},url={}}
	local fname=path..gn..".group"
	--local url1=pluginmsgex("utils","read",fname,TYPE_STRING)
	local f = assert(io.open(fname,"r"))
	url1=f:read("*all")
	f:close()
	if url1 then	
		local len=string.len(url1)
		local i=1
		local j=1
		while (i<len) do
			local p=string.find(url1,":\t")
			local p1=string.find(url1,"\r\n")
			local temp=string.sub(url1,1,p1+1)
			local len=string.len(temp)
			groups.title[j]=string.sub(url1,1,p-1)
			groups.url[j]=string.sub(url1,p+2,p1-1)
			url1=string.sub(url1,p1+2)
			i=i+len
			j=j+1
		end
		return groups,j-1
	else 
		alert("The group is empty")
		return nil,0
	end
end

local function refresh(groups,j,gn)
	local i=1
	local url1=""
	while (i<=j) do
		url1=url1..groups.title[i]..":\t"..groups.url[i].."\r\n"
		i=i+1
	end
	local fname=path..gn..".group"
	--pluginmsg("utils","write",fname,url1)
	local f = assert(io.open(fname,"w"))
	f:write(url1)
	f:close()
end


function gload()
	local idx,gn=gg()
	if idx then
		local i=1
		local groups,j=read(gn)
		while (i<=j) do
			NavigateTo(groups.url[i],OPEN_BACKGROUNDTAB)
			i=i+1
		end
	end
end

local function addurl(gn,title,url)
	local fname=path..gn..".group"
	--pluginmsg("utils","append",fname,title..":\t"..url.."\r\n")
	local f = assert(io.open(fname,"a"))
	f:write(title..":\t"..url.."\r\n")
	f:close()
end


function gaddall()
	local idx,gn=gg()	
	if idx then		
		local title, url = GetDocInfo(hwnd)
		addurl(gn,title,url)
		local url_saved=url
		id("ID_TAB_NEXT")
		title,url=GetDocInfo(hwnd)
		while (url~=url_saved) do
			addurl(gn,title,url)
			id("ID_TAB_NEXT")
			title,url=GetDocInfo(hwnd)
		end
	end
end

function greplaceall()
	local idx,gn=gg()	
	if idx then
		local fname=path..gn..".group"
		--pluginmsg("utils","write",fname,"")
		local f = assert(io.open(fname,"w"))
		f:write("")
		f:close()	
		local title, url = GetDocInfo(hwnd)
		addurl(gn,title,url)
		local url_saved=url
		id("ID_TAB_NEXT")
		title,url=GetDocInfo(hwnd)
		while (url~=url_saved) do
			addurl(gn,title,url)
			id("ID_TAB_NEXT")
			title,url=GetDocInfo(hwnd)
		end
	end
end			
	
function gsetstartup()
	local idx,gn=gg()	
	if idx then
		setpref(TYPE_STRING,g_pref3,gn)
		setpref(TYPE_BOOL,g_pref4,true)
	end
end

function gstartup()
	local sg=getpref(TYPE_STRING,g_pref3)
	local groups,j=read(sg)
	local i=1
	while (i<=j) do
		if (i==1) then 
			open(groups.url[i]) 
		else 
			opennew(groups.url[i]) 
		end
		i=i+1
	end
	id("ID_TAB_NEXT")
end

function glist2(gn,groups,j)
		local r,rr=d("Groups",{
				IDLIST={nil,groups.title},
		})
		local i=rr.IDLIST[1]
		if r=="IDBUTTONOK" or r=="IDLIST" then
			opennew(groups.url[i])
		elseif r=="IDBUTTONCANCEL" then
		elseif r=="IDBUTTONREMOVE" then
			groups.title[i]=nil
			groups.url[i]=nil
			while (i<=j) do
				groups.title[i]=groups.title[i+1]
				groups.url[i]=groups.url[i+1]
				i=i+1
			end
			refresh(groups,j-1,gn)
			glist2(gn,groups,j-1)
		end
end

function glist()
	local idx,gn=gg()
	names=getpref(TYPE_STRING,g_pref1)	
	if idx then
		local groups,j=read(gn)
		if groups then
			glist2(gn,groups,j)
		end
	end
end

function nostartup()
	setpref(TYPE_BOOL,g_pref4,false)
	sync()
end

--[[ function OnCloseWindow()
	gn="last"
	local fname=path..gn..".group"
	pluginmsg("utils","write",fname,"")
	local title, url = GetDocInfo(hwnd)
	local url_saved = url
	if url~="about:blank" then
		addurl(gn,title,url)
	end
	id("ID_TAB_NEXT")
	title,url=GetDocInfo(hwnd)
	while (url~=url_saved) do
		if url~="about:blank" then
			addurl(gn,title,url)
		end
		id("ID_TAB_NEXT")
		title,url=GetDocInfo(hwnd)
	end
end --]]

function CloseAllOthers()
	local title, url = GetDocInfo(hwnd)
	local url_saved = url
	id("ID_TAB_NEXT")
	title,url=GetDocInfo(hwnd)
	while (url~=url_saved) do
		id("ID_CLOSE_TAB")
		id("ID_TAB_NEXT")
		title,url=GetDocInfo(hwnd)
	end
end

function CloseAllSame()
	local title, url = GetDocInfo(hwnd)
	local url_saved = url
	id("ID_TAB_NEXT")
	title,url=GetDocInfo(hwnd)
	while (url==url_saved) do
		id("ID_CLOSE_TAB")
		title,url=GetDocInfo(hwnd)
	end
end

function quickgroupmenu()
	local names=getpref(TYPE_STRING,g_pref1)
	local len=string.len(names)
	local i=1
	local groups={}
	local j=1
	while (i<len) do
		local p=string.find(names,"\t")
		groups[j]=string.sub(names,1,p-1)
		names=string.sub(names,p+1)
		i=i+p
		j=j+1
	end
	local idx=popupmenu(unpack(groups))
	if idx~=nil and idx>0 then
		local i=1
		local groups,j=read(groups[idx])
		while (i<=j) do
			--alert(groups.url[i])
			-- NavigateTo(groups.url[i],OPEN_BACKGROUNDTAB)		
			NavigateTo(groups.url[i],OPEN_NEWTAB)	
			i=i+1
		end
	end
end


local function loadgroup(gname)
	local i=1
	local groups,j=read(gname)
	while (i<=j) do
		NavigateTo(groups.url[i],OPEN_BACKGROUNDTAB)
		i=i+1
	end
end


local function groupnames()
	local names=getpref(TYPE_STRING,g_pref1)
	local len=string.len(names)
	local i=1
	local groups={}
	local j=1
	while (i<len) do
		local p=string.find(names,"\t")
		groups[j]=string.sub(names,1,p-1)
		names=string.sub(names,p+1)
		i=i+p
		j=j+1
	end
	return groups,j
end


function gl1()
	local groups,j=groupnames()
	loadgroup(groups[1])		
end

function gl2()
	local groups,j=groupnames()
	if j>2 then 
		loadgroup(groups[2])
	end		
end

function gl3()
	local groups,j=groupnames()
	if j>3 then 
		loadgroup(groups[3])	
	end	
end

function gl4()
	local groups,j=groupnames()
	if j>4 then 
		loadgroup(groups[4])
	end		
end

function gl5()
	local groups,j=groupnames()
	if j>5 then 
		loadgroup(groups[5])
	end		
end

function gl6()
	local groups,j=groupnames()
	if j>6 then 
		loadgroup(groups[6])
	end		
end

function gl7()
	local groups,j=groupnames()
	if j>7 then 
		loadgroup(groups[7])
	end		
end

function gl8()
	local groups,j=groupnames()
	if j>8 then 
		loadgroup(groups[8])
	end		
end

function gl9()
	local groups,j=groupnames()
	if j>9 then 
		loadgroup(groups[9])
	end		
end

function group_menu()
	local names=getpref(TYPE_STRING,g_pref1)
	local len=string.len(names)
	local i=1
	local groups={}
	local j=1
	while (i<len) do
		local p=string.find(names,"\t")
		groups[j]=string.sub(names,1,p-1)
		names=string.sub(names,p+1)
		i=i+p
		j=j+1
	end
	i=1
	g={}
	while (i<j) do
		g.i={}
		g.i={groups[i],"luamacro(quickgroupmenu)"}
		i=i+1
	end
	setmenu.modify("Groups", 1, _GLOAD, {
				{groups[1], "luamacro(gl1)"},
				{groups[2], "luamacro(gl2)"},
				{groups[3], "luamacro(gl3)"},
				{groups[4], "luamacro(gl4)"},
				{groups[5], "luamacro(gl5)"},
				{groups[6], "luamacro(gl6)"},
				{groups[7], "luamacro(gl7)"},
				{groups[8], "luamacro(gl8)"},
				{groups[9], "luamacro(gl9)"},

	})
end	

	