require "dialog"
require "preferenceGUI\\locals"

local width = 360
local height = 170
local ffont="Arial"
local fpoint=10

local folder=GetFolder(ProfileFolder)

local fname=folder.."\\adblockplus\\patterns.ini"

local d = create_dialog({
	width = width,
	height = height,
	caption = "Adblock+",
	style = w32dlg.DS_CENTER,
	font = ffont, point = fpoint,
	{type="combobox",style=w32dlg.WS_BORDER+w32dlg.WS_VSCROLL+w32dlg.WS_HSCROLL+w32dlg.CBS_SIMPLE,height=130,width=280,x=5,y=5,id="IDadblock",},
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=5,width=50,height=12,id="IDADDBUTTON",caption=_ADADD}, 
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=25,width=50,height=12,id="IDREMOVEBUTTON",caption=_ADREMOVE},
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=45,width=50,height=12,id="IDLOADBUTTON",caption="Load Filter"}, 
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=65,width=50,height=12,id="IDSORTBUTTON",caption="Sort Filter"}, 
	{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=5,y=135,width=40,height=12,id="IDOK",caption="Done"},   	
})

--[[local d = {

	{
		width = width,
		height = height,
		caption = _FILTERRULE,
		style = w32dlg.DS_CENTER, -- style 
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDadblock",  
		},  
		{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=5,width=80,height=15,id="IDADDBUTTON",caption=_ADADD}, 
		{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=25,width=80,height=15,id="IDREMOVEBUTTON",caption=_ADREMOVE},
		{type="button",style=w32dlg.BS_DEFPUSHBUTTON,x=300,y=45,width=80,height=15,id="IDLOADBUTTON",caption="Load Filter"},              
	},

	{
		width = width,
		height = height,
		caption = _WHITELIST,
		style = w32dlg.DS_CENTER, -- style 
		font = ffont, point = fpoint, -- font 
		{
			type = "combobox",  
			style = w32dlg.WS_BORDER +
			        w32dlg.WS_VSCROLL + w32dlg.WS_HSCROLL +
			        w32dlg.CBS_SIMPLE,
			height=130,
			width=280,			
			x = 5,  
			y = 5,  
			id = "IDwhite",  
		}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON+w32dlg.WS_GROUP,x=300,y=5,width=80,height=10,id="IDRADIO1",caption="add"}, 
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=15,width=80,height=10,id="IDRADIO2",caption="remove"},
		{type="button",style=w32dlg.BS_RADIOBUTTON,x=300,y=25,width=80,height=10,id="IDRADIO3",caption="keep"},              
	},

} --]]

local function refresh(hwnd)
	 km.NavigateTo("abp://",OPEN_NORMAL)
end

--[[ local function readfile()
	--local f = assert(io.open(fname))
	--local patterns = f:read("*all")
	--local p=string.find(patterns,"[User patterns]")
	--patterns=string.sub(patterns,p)	
	--f:write(patterns)
	--f:close()
	io.input(fname)
	local g={}
	for line in io.lines() do
		if string.find(line,"[Pattern]") or string.find(line,"text=") or string.find(line,"type=") or string.find(line,"regexp=") or string.find(line,"shortcut=") or string.find(line,"disabled=") or string.find(line,"domain=") or string.find(line,"selector=") or string.len(line)<2 then
		else
			table.insert(g,line)
		end
	end
	return g
end --]]

local function readfile()
	local f = assert(io.open(fname,"r"))
	local patterns = f:read("*all")
	local patterns_backup=""
	local ad
	f:close()	
	local g={}
	local w={}
	local p=string.find(patterns,"User patterns")
	patterns_backup=string.sub(patterns,1,p+14)
	patterns=string.sub(patterns,p+14)	
	--alert(patterns)
	--alert(patterns_backup)
	local i=1
	local j=1
	while(string.find(patterns,"\n")) do
		p=string.find(patterns,"\n")
		patterns=string.sub(patterns,p+1)
		p=string.find(patterns,"\n")
		if (p) then
			ad=string.sub(patterns,1,p-1)
			--g[i]=string.sub(patterns,1,p-1)
			if (string.len(ad)>1) then
				--if (string.find(ad,"@@")) then
					--w[j]=ad
					--j=j+1
				--if (string.find(ad,"!")==nil) then
				--else
					g[i]=ad
					i=i+1
				--end
			end			
		--else
			--p=string.find(patterns,"\n")
			--g[i]=string.sub(patterns,1,p-1)
			--i=i+1
		end
	end
	--alert(i)
	return g,patterns_backup
end 

local function write_list(g,patterns_backup)
	local patterns=""
	for i,v in ipairs(g) do
		--patterns=patterns.."|"..g[i].."\n"
		patterns=patterns..g[i].."\n"
	end
	--for j,v in ipairs(w) do
	--	patterns=patterns..w[j].."\n"
	--end
	patterns=patterns.."\n"
	patterns=patterns_backup..patterns
	--alert(patterns)
	local f=assert(io.open(fname,"w"))
	if f then
		--alert("ok")
		f:write(patterns)
		f:close()
		refresh()
	end
	
end

--[[ local function write_list(g)
	f = assert(io.open(fname,"w+"))
	f:write("[User patterns]\n")
	for i,v in ipairs(g) do
		f:write(g[i].."\n")
	end
	f:close()
	refresh()
end --]]
		

--[[local function addlist(line)
	f = assert(io.open(fname,"a"))
	f:write(line,'\n')
	f:close()
	refresh()
end--]]
	

--local p = create_propertysheet(d)

function property_adblockplus()
	local g,patterns_backup=readfile()
	--alert(patterns_backup)
	r,rr=d("Adblock plus",{
		IDadblock={nil,g},	
		--{IDwhite={nil,w},IDRADIO3=1},	
	})
	
	if r=="IDADDBUTTON" then 
		local n=rr.IDadblock
		local found=false
		for h,v in ipairs(g) do
			if g[h]==n then found=true end
		end
		if (found==false) then
			table.insert(g,n)
			write_list(g,patterns_backup)
			--refresh(hwnd)
			--addlist(n)
		else
			alert("Filter rule already existed!","Adblock plus", ICON_INFO)
		end
		property_adblockplus()
	end
	
	if r=="IDREMOVEBUTTON" then
		local n=rr.IDadblock
		for h,v in ipairs(g) do
			if g[h]==n then
				--alert(g[h])
				table.remove(g,h)
				write_list(g,patterns_backup)
			end
		end
		property_adblockplus()
	end
	
	if r=="IDLOADBUTTON" then
		importlist()
		property_adblockplus()
	end
	
	if r=="IDSORTBUTTON" then
		sortlist()
		property_adblockplus()
	end
	
	--alert(rr.IDADDBUTTON)
	--alert(r)
	
	--local n=rr[1].IDadblock
	--local m=rr[2].IDwhite
	--[[ if rr[1].IDRADIO1==1 then
		table.insert(g,n)
		--alert(n)
		write_list(g,w,patterns_backup)
		refresh(hwnd)
 	elseif rr[1].IDRADIO2==1 then
		for h,v in ipairs(g) do
			if g[h]==n then
				table.remove(g,h)
				write_list(g,w,patterns_backup)
				refresh(hwnd)
			end
		end
	end
	if rr[2].IDRADIO1==1 then
		table.insert(w,m)
		write_list(g,w,patterns_backup)
	elseif rr[2].IDRADIO2==1 then
		for h,v in ipairs(w) do
			if w[h]==m then
				table.remove(w,h)
				write_list(g,w,patterns_backup)
				refresh(hwnd)
			end
		end
	end --]]
end

function addpattern(hwnd)
	local g,patterns_backup = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "ImageURL")
	local found=false
	if url1 then
		url1=prompt(_Adblock1,_Adblock2,url1)
		for h,v in ipairs(g) do
			if g[h]==url1 then found=true end
		end
		if found==false then
			--alert(url1)
			table.insert(g,url1)
			write_list(g,patterns_backup)
			--addlist(url1)
			--refresh(hwnd)
		else
			alert("Filter rule already existed!","Adblock plus", ICON_INFO)
		end
	end
end

function addpattern2(hwnd)
	local g,patterns_backup = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "FrameURL")
	local found=false
	if url1 then
		url1=prompt(_Adblock3,_Adblock4,url1)
		for h,v in ipairs(g) do
			if g[h]==url1 then found=true end
		end
		if found==false then
			table.insert(g,url1)
			write_list(g,patterns_backup)
			--addlist(url1)
			--refresh(hwnd)
		else
			alert("Filter rule already existed!","Adblock plus", ICON_INFO)
		end
	end
end

function importlist()
	local listfname,ofn = GetOpenFileName(hwnd)
	if (listfname) then
	io.input(listfname)
	f = assert(io.open(fname,"a"))
	local count=0
	local found=false
	if string.find(io.read(),"Adblock") then
	for line in io.lines() do
		if string.find(line,"Adblock") or string.find(line,"!") or string.len(line)<2 then		
		else		
			f:write(line,'\n')
			count=count+1			
		end
	end
	f:close()
	alert(count.." filter added","Adblock plus", ICON_INFO)
	refresh()
	end
	end
end

function sortlist()
	local g,patterns_backup=readfile()
	table.sort(g)
	write_list(g,patterns_backup)
end

-- NOTE: addpattern / addpattern2 (the right-click "Adblock Image" / "Adblock
-- Frame" actions) are defined in lua/autoload/adblock_context.lua now - they
-- write to the kplugin's <profile>\adblock.txt and the kplugin file watcher
-- makes the rule live immediately. The legacy versions that lived here wrote
-- to <profile>\adblockplus\patterns.ini, which our kplugin never reads.
