_G.ROBBEDACOP_PT = _G.ROBBEDACOP_PT or {}
ROBBEDACOP_PT.ModPath = ModPath
ROBBEDACOP_PT.SavePath = SavePath .. "ROBBEDACOP_PT.txt"
ROBBEDACOP_PT.settings = {}
ROBBEDACOP_PT.text = "ROBBEDACOP: "

function ROBBEDACOP_PT:Save()
	local file = io.open(self.SavePath, "w+")
	if file then
		file:write(json.encode(self.settings))
		file:close()
	end
end

function ROBBEDACOP_PT:Load()
	local file = io.open(self.SavePath, "r")
	if file then
		for i, j in pairs(json.decode(file:read("*all")) or {}) do
			ROBBEDACOP_PT.settings[i] = j
		end
		file:close()
	else
		self.settings = {
            display_mode = 1,
            counter_x = 598,
            counter_y = 37.50,
            font_size = 24,
			color_r = 255,
			color_g = 255,
			color_b = 0,
			hide_text = false
		}
		self:Save()
	end
end

Hooks:Add("LocalizationManagerPostInit","ROBBEDACOP_PTLocalizationManager",function(...)
	LocalizationManager:add_localized_strings({
		menu_robbedacop_counter = "ROBBEDACOP Progress Tracker",
        desc_robbedacop_counter = "ROBBEDACOP settings",
        racpt_kills_display = "Kills display mode",
        racpt_kills_display_desc = "Kills left: remaining kills to ROBBEDACOP \n Total Kills: total kills made (stops counting when completing ROBBEDACOP)",
		racpt_hide_text = "Hide text",
		racpt_hide_text_desc = "Hides the ROBBEDACOP text and only displays the number of kills",
        racpt_kills_left = "Kills Left",
        racpt_total_kills = "Total Kills",
        racpt_counter_x = "Text horizontal position",
        racpt_counter_y = "Text vertical position",
		racpt_font_size = "Font size",
		racpt_color_r = "Red amount",
		racpt_color_g = "Green amount",
		racpt_color_b = "Blue amount"
	})
end)

Hooks:Add("MenuManagerInitialize","ROBBEDACOP_PTMenuManagerInitialize",function(...)
    ROBBEDACOP_PT:Load()
end)

Hooks:Add("MenuManagerBuildCustomMenus","ROBBEDACOP_PTMenuManagerBuildCustomMenus",function(menu_manager, nodes)
	local menu_id = "robbedacop_options"
	local RACCS = ROBBEDACOP_PT.settings
	MenuHelper:NewMenu(menu_id)

	local items_kills_display = {
        "racpt_kills_left",
		"racpt_total_kills"
	}

	function MenuCallbackHandler:set_racpt_kills_display_callback(item)
		ROBBEDACOP_PT.settings.display_mode = tonumber(item:value())
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddMultipleChoice({
		id = "racpt_kills_display",
		title = "racpt_kills_display",
		desc = "racpt_kills_display_desc",
		callback = "set_racpt_kills_display_callback",
		items = items_kills_display,
		value = ROBBEDACOP_PT.settings.display_mode or 1,
		menu_id = menu_id,
		priority = 10,
	})

	function MenuCallbackHandler:set_racpt_hide_text_callback(item)
		ROBBEDACOP_PT.settings.hide_text = not ROBBEDACOP_PT.settings.hide_text
		if ROBBEDACOP_PT.settings.hide_text == true then
			ROBBEDACOP_PT.text = ""
		elseif ROBBEDACOP_PT.settings.hide_text == false then
			ROBBEDACOP_PT.text = "ROBBEDACOP: "
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddToggle({
		id = "racpt_hide_text",
		title = "racpt_hide_text",
		desc = "racpt_hide_text_desc",
		callback = "set_racpt_hide_text_callback",
		value = ROBBEDACOP_PT.settings.hide_text or false,
		menu_id = menu_id,
		priority = 9
	})

	MenuHelper:AddDivider({
		id = "racpt_divider",
		size = 16,
		menu_id = menu_id,
        priority = 8,
	})

    function MenuCallbackHandler:set_racpt_counter_x_callback(item)
		ROBBEDACOP_PT.settings.counter_x = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:set_x(ROBBEDACOP_PT.settings.counter_x)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddSlider({
		id = "racpt_counter_x",
		title = "racpt_counter_x",
		callback = "set_racpt_counter_x_callback",
		value = ROBBEDACOP_PT.settings.counter_x or 640,
		max = 1920,
		min = 0,
		step = 5,
		show_value = true,
		menu_id = menu_id,
		priority = 7
	})

    function MenuCallbackHandler:set_racpt_counter_y_callback(item)
		ROBBEDACOP_PT.settings.counter_y = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:set_y(ROBBEDACOP_PT.settings.counter_y)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddSlider({
		id = "racpt_counter_y",
		title = "racpt_counter_y",
		callback = "set_racpt_counter_y_callback",
		value = ROBBEDACOP_PT.settings.counter_y or 30,
		max = 1080,
		min = 0,
		step = 5,
		show_value = true,
		menu_id = menu_id,
		priority = 6
	})

	function MenuCallbackHandler:set_racpt_font_size_callback(item)
		ROBBEDACOP_PT.settings.font_size = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:child("ROBBEDACOP_PT_counter"):set_font_size(ROBBEDACOP_PT.settings.font_size)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddDivider({
		id = "racpt_divider",
		size = 16,
		menu_id = menu_id,
        priority = 5,
	})

	MenuHelper:AddSlider({
		id = "racpt_font_size",
		title = "racpt_font_size",
		callback = "set_racpt_font_size_callback",
		value = ROBBEDACOP_PT.settings.font_size or 24,
		max = 48,
		min = 10,
		step = 1,
		show_value = true,
		menu_id = menu_id,
		priority = 4
	})

	function MenuCallbackHandler:set_racpt_color_r_callback(item)
		ROBBEDACOP_PT.settings.color_r = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:child("ROBBEDACOP_PT_counter"):set_color(Color(255, RACCS.color_r, RACCS.color_g, RACCS.color_b) / 255)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddDivider({
		id = "racpt_divider",
		size = 16,
		menu_id = menu_id,
        priority = 3,
	})

	MenuHelper:AddSlider({
		id = "racpt_color_r",
		title = "racpt_color_r",
		callback = "set_racpt_color_r_callback",
		value = ROBBEDACOP_PT.settings.color_r or 255,
		max = 255,
		min = 0,
		step = 1,
		show_value = true,
		menu_id = menu_id,
		priority = 2
	})

	function MenuCallbackHandler:set_racpt_color_g_callback(item)
		ROBBEDACOP_PT.settings.color_g = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:child("ROBBEDACOP_PT_counter"):set_color(Color(255, RACCS.color_r, RACCS.color_g, RACCS.color_b) / 255)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddSlider({
		id = "racpt_color_g",
		title = "racpt_color_g",
		callback = "set_racpt_color_g_callback",
		value = ROBBEDACOP_PT.settings.color_g or 255,
		max = 255,
		min = 0,
		step = 1,
		show_value = true,
		menu_id = menu_id,
		priority = 1
	})

	function MenuCallbackHandler:set_racpt_color_b_callback(item)
		ROBBEDACOP_PT.settings.color_b = tonumber(item:value())
		if alive(ROBBEDACOP_PT.panel) then
			ROBBEDACOP_PT.panel:child("ROBBEDACOP_PT_counter"):set_color(Color(255, RACCS.color_r, RACCS.color_g, RACCS.color_b) / 255)
		end
		ROBBEDACOP_PT:Save()
	end

	MenuHelper:AddSlider({
		id = "racpt_color_b",
		title = "racpt_color_b",
		callback = "set_racpt_color_b_callback",
		value = ROBBEDACOP_PT.settings.color_b or 0,
		max = 255,
		min = 0,
		step = 1,
		show_value = true,
		menu_id = menu_id,
		priority = 0
	})

	nodes[menu_id] = MenuHelper:BuildMenu(menu_id)
	MenuHelper:AddMenuItem(nodes["blt_options"], menu_id, "menu_robbedacop_counter", "desc_robbedacop_counter")
end)
