_G.ExcludableMenuBackgrounds = _G.ExcludableMenuBackgrounds or {}
ExcludableMenuBackgrounds.ModPath = ModPath
ExcludableMenuBackgrounds.AssetsPath = ExcludableMenuBackgrounds.ModPath .. "Assets/"
ExcludableMenuBackgrounds.SavePath = SavePath .. "ExcludableMenuBackgrounds.txt"
ExcludableMenuBackgrounds.Options = ExcludableMenuBackgrounds.Options or {
	backgrounds_set = "Default"
} 
ExcludableMenuBackgrounds.Sets = ExcludableMenuBackgrounds.Sets or {} 
ExcludableMenuBackgrounds.options_menu = "ExcludableMenuBackgrounds_options"
function ExcludableMenuBackgrounds:load_sets()
	self.Sets = {}
	for _, set in pairs(SystemFS:list(self.AssetsPath, true)) do
		table.insert(self.Sets, set)
	end
end
function ExcludableMenuBackgrounds:load_textures()
	self._movies = {}
	if not table.contains(self.Sets, self.Options.backgrounds_set) then
		self.Options.backgrounds_set = self.Sets[1]
		self:Save()
	end
	local ids_strings = {}
	local set_path = self.AssetsPath .. self.Options.backgrounds_set .. "/"
	for _, file in pairs(SystemFS:list(set_path)) do
		local path = set_path .. file
		local in_path = "guis/textures/backgrounds/" .. file:gsub(".png", ""):gsub(".dds", ""):gsub(".texture", ""):gsub(".tga", ""):gsub(".movie", "")
		table.insert(ids_strings, Idstring(in_path))
		local is_movie = file:match("movie")
		DB:create_entry(Idstring(is_movie and "movie" or "texture"), Idstring(in_path), path)
		if is_movie then
			self._movies[in_path] = path 
		end
	end		
	Application:reload_textures(ids_strings)
end
function ExcludableMenuBackgrounds:Save()
	local file = io.open(self.SavePath, "w+")
	if file then
		file:write(json.encode(self.Options))
		file:close()
	end
end
function ExcludableMenuBackgrounds:Load()
	local file = io.open(self.SavePath, "r")
	if file then
		self.Options = json.decode(file:read("*all"))
		file:close()
	else
		self:Save()
	end
end
function ExcludableMenuBackgrounds:UpdateSetsItem()
	local item = managers.menu:active_menu().logic:get_item("backgrounds_set")
	if item then
		item:clear_options()
		for k, set in pairs(self.Sets) do
			table.insert(item._all_options, CoreMenuItemOption.ItemOption:new({value = set, text_id = set, localize = false}))
		end		
		item._options = item._all_options
		if not table.contains(self.Sets, self.Options.backgrounds_set) then
			item:set_value(self.Sets[1])	
			MenuCallbackHandler:menubg_clbk(item)	
		else
			item:set_value(self.Options.backgrounds_set)
		end			
	end
end
function ExcludableMenuBackgrounds:Update()
	ExcludableMenuBackgrounds:load_sets()
	self:UpdateSetsItem()
	self:load_textures()
	managers.menu_scene:SetBackground()
end
Hooks:PostHook(MenuManager, "_node_selected", "ExcludableMenuBackgroundsNodeSelected", function(self, name, node)
	if node and node:parameters().menu_id == ExcludableMenuBackgrounds.options_menu then
		ExcludableMenuBackgrounds:UpdateSetsItem()
	end
end)
Hooks:Add("LocalizationManagerPostInit", "ExcludableMenuBackgrounds_loc", function(loc)
	LocalizationManager:add_localized_strings({
	    ["ExcludableMenuBackgrounds_options_title"] = "Menu Backgrounds (TouhouCrud)",
	    ["ExcludableMenuBackgrounds_options_desc"] = "Change the way the mod acts",
	    ["menubg_backgrounds_set_title"] = "Backgrounds Set",
	    ["menubg_backgrounds_set_desc"] = "Choose the backgrounds set the mod will use(default : standard)",
	    ["menubg_use_standard_for_all_title"] = "Use standard texture for all menus",
	    ["menubg_use_standard_for_all_desc"] = "If enabled the mod will use the standard texture for all menus(default : false)",
	    ["menubg_refresh_button_title"] = "Refresh",
  	})
end)

Hooks:Add("MenuManagerSetupCustomMenus", "MenuManagerSetupCustomMenusExcludableMenuBackgrounds", function(menu_manager, nodes)
	MenuHelper:NewMenu(ExcludableMenuBackgrounds.options_menu)
end)
Hooks:Add("MenuManagerPopulateCustomMenus", "MenuManagerPopulateCustomMenusExcludableMenuBackgrounds", function(menu_manager, nodes)
	--World:effect_manager():set_rendering_enabled(Global.load_level)
	ExcludableMenuBackgrounds:Load()
	ExcludableMenuBackgrounds:load_sets()
	ExcludableMenuBackgrounds:load_textures()
 	function MenuCallbackHandler:menubg_clbk(item)
		ExcludableMenuBackgrounds.Options.backgrounds_set = item:value()		
		ExcludableMenuBackgrounds:Save()	
		ExcludableMenuBackgrounds:Update()
    end
	function MenuCallbackHandler:menubg_toggle_clbk(item)
		ExcludableMenuBackgrounds.Options[item._parameters.name] = item:value() == "on"
		ExcludableMenuBackgrounds:Save()
	end			
	function MenuCallbackHandler:menubg_refresh(item)
		ExcludableMenuBackgrounds:Update()
	end
    MenuHelper:AddMultipleChoice({
        id = "backgrounds_set",
        title = "menubg_backgrounds_set_title",
        desc = "menubg_backgrounds_set_desc",
        callback = "menubg_clbk",
        items = ExcludableMenuBackgrounds.Sets,
 		priority = 3,
        menu_id = ExcludableMenuBackgrounds.options_menu,
    })
--[[
	MenuHelper:AddToggle({
		id = "use_standard_for_all",
		title = "menubg_use_standard_for_all_title",
		desc = "menubg_use_standard_for_all_desc",
		callback = "menubg_toggle_clbk",
		menu_id = ExcludableMenuBackgrounds.options_menu,
		priority = 2, 		
		value = ExcludableMenuBackgrounds.Options.use_standard_for_all,
	})
--]]
	MenuHelper:AddButton({
		id = "menubg_refresh_button",
		title = "menubg_refresh_button_title",
		callback = "menubg_refresh",
		priority = 1, 
		menu_id = ExcludableMenuBackgrounds.options_menu,
	})
end)
Hooks:Add("MenuManagerBuildCustomMenus", "MenuManagerBuildCustomMenusExcludableMenuBackgrounds", function(menu_manager, nodes)
	nodes[ExcludableMenuBackgrounds.options_menu] = MenuHelper:BuildMenu(ExcludableMenuBackgrounds.options_menu)
	MenuHelper:AddMenuItem(MenuHelper.menus.lua_mod_options_menu, ExcludableMenuBackgrounds.options_menu, "ExcludableMenuBackgrounds_options_title", "ExcludableMenuBackgrounds_options_desc", 1)
end)
