local FONT = tweak_data.menu.pd2_small_font
local FONT_SIZE = tweak_data.menu.pd2_small_font_size
local FONT_LARGE = tweak_data.menu.pd2_large_font
local FONT_SIZE_LARGE = tweak_data.menu.pd2_large_font_size
local TEXT_COLOR = tweak_data.screen_colors.text
local BG_COLOR = tweak_data.screen_colors.dark_bg
local MOUSEOVER_COLOR = tweak_data.screen_colors.button_stage_2
local BUTTON_COLOR = tweak_data.screen_colors.button_stage_3

function InfamyTreeGui:_setup()
	if alive(self._panel) then
		self._ws:panel():remove(self._panel)
	end

	managers.menu_scene:on_setup_infamy_menu()

	self._requested_textures = {}
	self._panel = self._ws:panel():panel({
		valign = "center",
		input = true,
		visible = true,
		layer = self._init_layer
	})
	local title_text = self._panel:text({
		vertical = "top",
		name = "infamytree_text",
		align = "left",
		text = utf8.to_upper(managers.localization:text("menu_infamytree")),
		h = tweak_data.menu.pd2_large_font_size,
		font_size = tweak_data.menu.pd2_large_font_size,
		font = tweak_data.menu.pd2_large_font,
		color = tweak_data.screen_colors.text
	})
	local title_bg_text = self._fullscreen_panel:text({
		name = "infamytree_text",
		vertical = "top",
		h = 90,
		alpha = 0.4,
		align = "left",
		blend_mode = "add",
		layer = 1,
		text = utf8.to_upper(managers.localization:text("menu_infamytree")),
		font_size = tweak_data.menu.pd2_massive_font_size,
		font = tweak_data.menu.pd2_massive_font,
		color = BUTTON_COLOR
	})
	local x, y = managers.gui_data:safe_to_full_16_9(title_text:world_x(), title_text:world_center_y())
	local td = tweak_data.infamy

	title_bg_text:set_world_left(x)
	title_bg_text:set_world_center_y(y)
	title_bg_text:move(-13, 9)
	MenuBackdropGUI.animate_bg_text(self, title_bg_text)

	self.main_display_panel = self._panel:panel({
		w = 400,
		h = 300
	})

	self.main_display_panel:set_world_center_x(self._panel:world_center_x())
	self.main_display_panel:set_world_center_y(self._panel:world_center_y() - 75)

	self.scroll_panel = self._panel:panel({
		h = 175,
		x = 0,
		y = self._panel:h() - 225,
		w = self._panel:w()
	})
	self.scroll = HorizontalScrollItemList:new(self.scroll_panel, {
		horizontal = true,
		scrollbar_padding = 10,
		padding = 0
	}, {})

	self.scroll:add_lines_and_static_down_indicator()
	self.scroll:set_selected_callback(callback(self, self, "update_detail_panels"))

	for i, item_id in pairs(td.tree) do
		local data = td.items[item_id]

		self:check_infamous_drop_parameter(data)
		self.scroll:add_item(InfamyTreeItem:new(self.scroll:canvas(), {
			tier_id = item_id,
			tier_data = data,
			rank_requirement = i,
			scroll_right = self.scroll_panel:w()
		}))
	end

	self.scroll:sort_items(function (lhs, rhs)
		return lhs.data.rank_requirement < rhs.data.rank_requirement
	end, nil, true)

	for i, item in ipairs(self.scroll:items()) do
		item:set_backlight(i % 2 == 0)
	end

	self.scroll:set_input_focus(true)

	self.detail_panel = self._panel:panel({
		w = 325,
		y = title_text:bottom() + 10,
		h = self.scroll:world_y() - (title_text:bottom() + 10) - 5
	})

	self.detail_panel:set_bottom(self.scroll_panel:top() - 5)
	self.detail_panel:set_right(self.scroll_panel:right())
	self.detail_panel:rect({
		color = BG_COLOR
	})
	BoxGuiObject:new(self.detail_panel, {
		sides = {
			1,
			1,
			1,
			1
		}
	})

	self.detail_panel_content = self.detail_panel:panel()
	self.infamous_panel = self._panel:panel({
		w = 350,
		x = title_text:left(),
		y = title_text:bottom() + 10,
		h = self.scroll_panel:top() - 10 - (title_text:bottom() + 10)
	})

	self.infamous_panel:rect({
		color = BG_COLOR
	})
	BoxGuiObject:new(self.infamous_panel, {
		sides = {
			1,
			1,
			1,
			1
		}
	})

	local infamy_panel_top = self.infamous_panel:panel({
		h = 200,
		x = 5,
		y = 5,
		w = self.infamous_panel:w() - 10
	})
	local infamy_top_panel_ratio = 0.72
	local infamy_subpanel_top_left = infamy_panel_top:panel({
		w = infamy_panel_top:w() * infamy_top_panel_ratio
	})
	local infamy_subpanel_top_right = infamy_panel_top:panel({
		x = infamy_panel_top:w() * infamy_top_panel_ratio,
		w = infamy_panel_top:w() * (1 - infamy_top_panel_ratio)
	})
	local infamy_panel_bottom = self.infamous_panel:panel({
		name = "infamy_panel_bottom",
		x = 5,
		y = infamy_panel_top:bottom() + 5,
		h = self.infamous_panel:h() - infamy_panel_top:bottom() - 10,
		w = self.infamous_panel:w() - 10
	})
	local is_level_cap = managers.experience:reached_level_cap()
	local reputation_progress_panel = infamy_subpanel_top_left:panel({
		x = 5,
		y = 5,
		w = infamy_subpanel_top_left:w() - 10,
		h = FONT_SIZE * 4
	})
	local margin = 5
	local max_width = reputation_progress_panel:w() - margin * 2

	reputation_progress_panel:text({
		vertical = "center",
		y = 0,
		layer = 3,
		text = managers.localization:text("menu_infamy_infamy_panel_reputation_level", {
			reputation = ""
		}),
		x = margin,
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = TEXT_COLOR
	})
	reputation_progress_panel:text({
		vertical = "center",
		layer = 3,
		text = managers.experience:current_level() .. "/100 LVL",
		x = margin,
		y = FONT_SIZE,
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = TEXT_COLOR
	})
	reputation_progress_panel:rect({
		x = margin,
		y = FONT_SIZE * 2,
		w = max_width,
		h = FONT_SIZE / 2,
		color = tweak_data.screen_color_grey
	})
	reputation_progress_panel:rect({
		x = margin,
		y = FONT_SIZE * 2,
		w = math.lerp(0, max_width, managers.experience:current_level() / 100),
		h = FONT_SIZE / 2,
		color = Color.white
	})

	local prestige_error_string = ""
	local prestige_availible = true

	if not is_level_cap then
		prestige_error_string = managers.localization:to_upper_text("menu_infamy_prestige_error_paused")
		prestige_availible = false
	elseif managers.experience:current_rank() < 1 then
		prestige_error_string = managers.localization:to_upper_text("menu_infamy_prestige_error_rank")
		prestige_availible = false
	end

	local prestige_progress_panel = infamy_subpanel_top_left:panel({
		x = 5,
		w = infamy_subpanel_top_left:w() - 10,
		y = reputation_progress_panel:bottom() + 5,
		h = FONT_SIZE * 4,
		alpha = prestige_availible and 1 or 0.5
	})
	local progress_margin = 5
	local max_width = prestige_progress_panel:w() - progress_margin * 2

	prestige_progress_panel:text({
		vertical = "center",
		y = 0,
		layer = 3,
		text = managers.localization:text("menu_infamy_infamy_panel_prestige_level"),
		x = margin,
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = TEXT_COLOR
	})
	prestige_progress_panel:text({
		vertical = "center",
		layer = 3,
		text = managers.experience:get_current_prestige_xp() .. "/" .. managers.experience:get_max_prestige_xp(),
		x = margin,
		y = FONT_SIZE,
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = TEXT_COLOR
	})
	prestige_progress_panel:rect({
		x = progress_margin,
		y = FONT_SIZE * 2,
		w = max_width,
		h = FONT_SIZE / 2,
		color = tweak_data.screen_color_grey
	})
	prestige_progress_panel:rect({
		x = progress_margin,
		y = FONT_SIZE * 2,
		w = math.lerp(0, max_width, managers.experience:get_prestige_xp_percentage_progress()),
		h = FONT_SIZE / 2,
		color = tweak_data.screen_colors.infamy_color,
		alpha = is_level_cap and 1 or 0.3
	})

	local prestige_error_text = infamy_subpanel_top_left:text({
		vertical = "center",
		wrap = true,
		layer = 3,
		text = prestige_error_string,
		x = prestige_progress_panel:x(),
		y = prestige_progress_panel:bottom() - FONT_SIZE,
		h = FONT_SIZE * 2,
		w = prestige_progress_panel:w(),
		font = FONT,
		font_size = FONT_SIZE,
		color = tweak_data.screen_colors.important_1,
		visible = not prestige_availible
	})

	infamy_subpanel_top_right:text({
		vertical = "center",
		y = 0,
		x = 0,
		layer = 3,
		text = managers.localization:text("menu_infamy_infamy_panel_infamy_level", {
			infamy = ""
		}),
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE - 1,
		color = TEXT_COLOR
	})

	local card_id, card_rect = managers.infamy:get_infamy_card_id_and_rect()
	local card_proportion = card_rect[3] / card_rect[4]
	local snap_to_width = card_proportion > infamy_subpanel_top_right:w() / infamy_subpanel_top_right:h()
	local card_w = snap_to_width and infamy_subpanel_top_right:w() or infamy_subpanel_top_right:h() * card_proportion
	local card_h = snap_to_width and infamy_subpanel_top_right:w() / card_proportion or infamy_subpanel_top_right:h()
	local card_panel = infamy_subpanel_top_right:panel({
		y = FONT_SIZE,
		w = card_w,
		h = card_h
	})
	local card_bitmap = card_panel:bitmap({
		layer = 2,
		w = card_panel:w(),
		h = card_panel:h(),
		texture = card_id,
		texture_rect = card_rect
	})

	card_panel:text({
		vertical = "center",
		align = "center",
		layer = 3,
		text = tostring(managers.experience:current_rank()),
		font = FONT_LARGE,
		font_size = FONT_SIZE_LARGE,
		color = Color.black
	})

	local error_string = ""
	local error_string_prestige = ""

	if managers.experience:current_level() < 100 then
		self._can_go_infamous = false
		error_string = managers.localization:to_upper_text("menu_infamy_go_infamous_error_level")
	elseif managers.money:offshore() < managers.money:get_infamous_cost(managers.experience:current_rank() + 1) then
		self._can_go_infamous = false
		error_string = managers.localization:to_upper_text("menu_infamy_go_infamous_error_money")
	else
		self._can_go_infamous = true
	end

	if not self._can_go_infamous then
		self._can_go_infamous_prestige = false
		error_string_prestige = error_string
	elseif managers.experience:get_current_prestige_xp() < managers.experience:get_max_prestige_xp() then
		self._can_go_infamous_prestige = false
		error_string_prestige = managers.localization:to_upper_text("menu_infamy_go_infamous_error_prestige_xp")
	else
		self._can_go_infamous_prestige = true
	end

	local go_inf_panel_main = infamy_panel_bottom:panel({
		y = 0,
		name = "go_infamous_button"
	})
	local go_inf_text_main = go_inf_panel_main:text({
		text = "Go Infamous:",
		align = "center",
		layer = 3,
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE
	})

	ExtendedPanel.make_fine_text(go_inf_text_main)
	go_inf_text_main:set_righttop(go_inf_panel_main:w() - 5, 0)

	local go_inf_panel_rep = go_inf_panel_main:panel({
		name = "go_infamous_rep_panel",
		y = FONT_SIZE * 2,
		h = FONT_SIZE
	})

	go_inf_panel_rep:rect({
		name = "go_infamous_rep_highlight",
		visible = false,
		alpha = 0.4,
		layer = 1,
		color = BUTTON_COLOR
	})

	local go_inf_text_rep = go_inf_panel_rep:text({
		name = "go_infamous_rep_text",
		align = "right",
		layer = 3,
		text = managers.localization:to_upper_text("menu_infamy_go_inf_rep") .. " [Y]",
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = self._can_go_infamous and BUTTON_COLOR or tweak_data.screen_color_grey
	})
	local error_text_rep = infamy_panel_bottom:text({
		wrap = true,
		align = "center",
		layer = 3,
		text = error_string,
		font = FONT,
		font_size = FONT_SIZE,
		color = tweak_data.screen_colors.important_1
	})

	ExtendedPanel.make_fine_text(error_text_rep)
	error_text_rep:set_righttop(go_inf_panel_main:w() - 5, go_inf_panel_rep:bottom())

	local go_inf_panel_prestige = go_inf_panel_main:panel({
		name = "go_infamous_prestige_panel",
		y = FONT_SIZE * 5,
		h = FONT_SIZE,
		alpha = prestige_availible and 1 or 0.5
	})

	go_inf_panel_prestige:rect({
		name = "go_infamous_prestige_highlight",
		visible = false,
		alpha = 0.4,
		layer = 1,
		color = BUTTON_COLOR
	})

	local go_inf_text_prestige = go_inf_panel_prestige:text({
		name = "go_infamous_prestige_text",
		align = "right",
		layer = 3,
		text = managers.localization:to_upper_text("menu_infamy_go_inf_prestige") .. " [X]",
		h = FONT_SIZE,
		font = FONT,
		font_size = FONT_SIZE,
		color = self._can_go_infamous_prestige and BUTTON_COLOR or tweak_data.screen_color_grey
	})
	local error_text_prestige = infamy_panel_bottom:text({
		wrap = true,
		align = "center",
		layer = 3,
		text = error_string_prestige,
		font = FONT,
		font_size = FONT_SIZE,
		color = tweak_data.screen_colors.important_1,
		visible = prestige_availible
	})

	ExtendedPanel.make_fine_text(error_text_prestige)
	error_text_prestige:set_righttop(go_inf_panel_main:w() - 5, go_inf_panel_prestige:bottom())

	if managers.menu:is_pc_controller() then
		local back_button = self._panel:text({
			vertical = "bottom",
			name = "back_button",
			align = "right",
			blend_mode = "add",
			text = utf8.to_upper(managers.localization:text("menu_back")),
			h = tweak_data.menu.pd2_large_font_size,
			font_size = tweak_data.menu.pd2_large_font_size,
			font = tweak_data.menu.pd2_large_font,
			color = BUTTON_COLOR
		})
		local _, _, w, h = back_button:text_rect()

		back_button:set_size(w, h)
		back_button:set_position(math.round(back_button:x()), math.round(back_button:y()))
		back_button:set_right(self._panel:w())
		back_button:set_bottom(self._panel:h())

		local bg_back = self._fullscreen_panel:text({
			name = "TitleTextBg",
			vertical = "bottom",
			h = 90,
			align = "right",
			alpha = 0.4,
			blend_mode = "add",
			layer = 1,
			text = back_button:text(),
			font_size = tweak_data.menu.pd2_massive_font_size,
			font = tweak_data.menu.pd2_massive_font,
			color = BUTTON_COLOR
		})
		local x, y = managers.gui_data:safe_to_full_16_9(back_button:world_right(), back_button:world_center_y())

		bg_back:set_world_right(x)
		bg_back:set_world_center_y(y)
		bg_back:move(13, -9)
		MenuBackdropGUI.animate_bg_text(self, bg_back)
	end
end

