--[[ 
	ONEMenu
	Application, themes and files manager.
	
	Licensed by Creative Commons Attribution-ShareAlike 4.0
	http://creativecommons.org/licenses/by-sa/4.0/
	
	Designed By Gdljjrod & DevDavisNunez.
	Collaborators: BaltazaR4 & Wzjk.
]]

function favorites_manager()

	local srcn = newScroll(apps,15)
	buttons.interval(16,5)

	if __FAV == 1 then fav = STRINGS_APP_YES else fav = STRINGS_APP_NO end
	while true do
		buttons.read()

		if theme.data["list"] then theme.data["list"]:blit(0,0) end

		screen.print(480,15,STRINGS_FAVORITES_SECTION,1,theme.style.TITLECOLOR,theme.style.TXTBKGCOLOR,__ACENTER)
		screen.print(950,15,STRINGS_COUNT + srcn.maxim,1,theme.style.COUNTCOLOR,theme.style.TXTBKGCOLOR,__ARIGHT)

		if srcn.maxim > 0 then

			if buttons.up or buttons.analogly < -60 then srcn:up() end
			if buttons.down or buttons.analogly > 60 then srcn:down() end

			local y = 70
			for i=srcn.ini,srcn.lim do
				if i == srcn.sel then draw.fillrect(10,y-2,930,23,theme.style.SELCOLOR) end

				screen.print(20,y,string.format("%02d",i)+' ) '+ apps[i],1.0,theme.style.TXTCOLOR,theme.style.TXTBKGCOLOR,__ALEFT)

				y+=26
			end

			if theme.data["buttons2"] then
				theme.data["buttons2"]:blitsprite(5,515,0)--select
			end
			screen.print(40,520,STRINGS_FAVORITES_CLEAN,1.0,theme.style.TXTCOLOR,theme.style.TXTBKGCOLOR,__ALEFT)

			if theme.data["buttons2"] then
				theme.data["buttons2"]:blitsprite(960-30,515,1)--start
			end
			screen.print(960-40,520,STRINGS_FAVORITES_SCANNING..fav,1.0,theme.style.TXTCOLOR,theme.style.TXTBKGCOLOR,__ARIGHT)

		else
			screen.print(480,272,STRINGS_APP_EMPTY,1.0,theme.style.TXTCOLOR,theme.style.TXTBKGCOLOR,__ACENTER)
		end
		screen.flip()

		if buttons[cancel] then break end
		shortcuts()

		if srcn.maxim > 0 then

			if buttons.start then
				if __FAV == 1 then
					__FAV,fav = 0,STRINGS_APP_NO
					write_config()
				else
					if srcn.maxim > 0 then
						__FAV,fav = 1,STRINGS_APP_YES
						write_config()
					else
						os.message(STRINGS_FAVORITES_ACTIVED)
					end
				end
			end

			if buttons.select then
				if os.message(STRINGS_FAVORITES_EMPTY,1) == 1 then
					apps={}
					srcn:set(apps,15)
					if __FAV == 1 then __FAV,fav = 0,STRINGS_APP_NO end
					write_favs(__PATH_FAVS)
					write_config()

					for i=1, #appman do
						for j=1, #appman[i].list do
							if appman[i].list[j].fav then appman[i].list[j].fav = false end
						end
					end
				end
			end

			if buttons.square then
				if os.message(STRINGS_FAVORITES_REMOVE,1) == 1 then
					for i=1, #appman do
						for j=1, #appman[i].list do
							if appman[i].list[j].id == apps[srcn.sel] then appman[i].list[j].fav = false end
						end
					end
					table.remove(apps,srcn.sel)
					srcn:set(apps,15)

					write_favs(__PATH_FAVS)

					if srcn.maxim <=0 then
						if __FAV == 1 then __FAV,fav = 0,STRINGS_APP_NO end
					end
					write_config()
				end
			end

		end

	end
	os.delay(15)
	if theme.data["list"] then theme.data["list"]:blit(0,0) end
end
