require "dialog"
require "preferenceGUI\\locals"

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

local folder=GetFolder(ProfileFolder)

local fname=folder.."\\chrome\\ad.css"

local d = create_dialog({
	width = width,
	height = height,
	caption = "Adfilter",
	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="IDadfilter",},
	{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 function refresh(hwnd)
	km.LoadCSS(folder.."\\chrome\\userContent.css")
end

local function readfile()
	local f = assert(io.open(fname,"r"))
	local css=f:read("*all")
	f:close()
	local g={}
	local i=1
	while(string.find(css,"src")) do
		local p=string.find(css,"src")
		css=string.sub(css,p+5)
		local p2=string.find(css,"]")
		g[i]=string.sub(css,2,p2-2)
		i=i+1
	end
	return g
end

local function writefile(g)
	local css=""
	for i,v in ipairs(g) do
		css=css.."\[src\*\=\""..g[i].."\"\]"..",\n"
	end
	css=css.."\[href\*\=\"ad4\.sina\.com\.cn\"\]".."\n"
	css=css.."\{ display: none \! important \}"
	local f=assert(io.open(fname,"w"))
	f:write(css)
	f:close()
end

function addpattern(hwnd)
	local g = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "ImageURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		writefile(g)
		refresh(hwnd)
	end
end


function addpattern2(hwnd)
	local g = readfile()
	local url1=GetGlobalVar(TYPE_STRING, "FrameURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		writefile(g)
		refresh(hwnd)
	end
end

function addcss3(hwnd)
	local g = readfile()
	local word = getclipboard()
	id("ID_EDIT_COPY")
	url1 = getclipboard()
	setclipboard(word)
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		writefile(g)
		refresh(hwnd)
	end
end


function addcss4(hwnd)
	local g = readfile()
	local word = getclipboard()
	local url1=GetGlobalVar(TYPE_STRING, "LinkURL")
	if url1 then
		if string.find(url1,"http://") then
			url1=string.sub(url1,string.find(url1,"http://")+7)
		end
		url1=prompt("block item in userContent.css?","Block item",url1)
		table.insert(g,url1)
		writefile(g)
		refresh(hwnd)
	end
end

local function sortlist()
	local g=readfile()
	table.sort(g)
	writefile(g)
end

function importlist1()
	local g=readfile()
	local listfname,ofn = GetOpenFileName(hwnd)

	if (listfname) then
		io.input(listfname)
		io.output("test1.txt")
		local count=0
		for line in io.lines() do
			if (string.find(line,"!") or string.find(line,"Adblock Plus") or string.find(line,"@@") or string.find(line,"|") or string.find(line,"#") or string.find(line,"\/\\")) then
			elseif string.len(line)>1 then 
				while string.find(line,"*") do
					local temp=string.find(line,"*")
					local a=string.sub(line,1,temp-1)
					local b=string.sub(line,temp+1)
					line=a..b
				end
				--while string.find(line,"##") do
				--	line=string.sub(line,1,string.find(line,"##")-1)
				--end
				if string.find(line,"$image") then					
					local temp=string.find(line,"$image")
					line=string.sub(line,1,temp-1)
				end
				if string.find(line,"$object") then					
					local temp=string.find(line,"$object")
					line=string.sub(line,1,temp-1)
				end
				if string.find(line,"$script") then					
					local temp=string.find(line,"$script")
					line=string.sub(line,1,temp-1)
				end
				if string.find(line,"$subdocument") then					
					local temp=string.find(line,"$subdocument")
					line=string.sub(line,1,temp-1)
				end
				if string.find(line,"$third-party") then					
					local temp=string.find(line,"$third-party")
					line=string.sub(line,1,temp-1)
				end
				--table.insert(g,line)
				io.write(line.."\n")
				count=count+1
			end
		end
		--writefile(g)
		alert(count.." filter added","Adfilter", ICON_INFO)
		--refresh(hwnd)
	end
end

local function importlist()
	local g=readfile()
	local listfname,ofn = GetOpenFileName(hwnd)
	if (listfname) then
		io.input(listfname)
		local count=0
		for line in io.lines() do
			table.insert(g,line)
			count=count+1
		end
		writefile(g)
		alert(count.." filter added","Adfilter", ICON_INFO)
		refresh(hwnd)
	end	
end

function property_adfilter()
	local g=readfile()
	r,rr=d("Adfilter",{
		IDadfilter={nil,g},		
ws	})
	if r=="IDADDBUTTON" then 
		local n=rr.IDadfilter
		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)
			writefile(g)
			refresh(hwnd)
		else
			alert("Filter rule already existed!","Adfilter", ICON_INFO)
		end
		property_adfilter()
	end
	if r=="IDREMOVEBUTTON" then
		local n=rr.IDadfilter
		for h,v in ipairs(g) do
			if g[h]==n then
				table.remove(g,h)
				writefile(g)
			end
		end
		property_adfilter()
	end
	if r=="IDLOADBUTTON" then
		importlist()
		refresh(hwnd)
		property_adfilter()
	end
	if r=="IDSORTBUTTON" then
		sortlist()
		property_adfilter()
	end
end




