local druid = require("druid.druid")
local helper = require("druid.helper")

local RichText = require("druid.custom.rich_text.rich_text")


local function highlight_nodes(words)
	for index = 1, #words do
		local node = words[index].node
		local cloned = gui.clone(gui.get_node("highlight"))
		gui.set_color(cloned, vmath.vector4(math.random(), math.random(), math.random(), 0.4))
		gui.set_screen_position(cloned, gui.get_screen_position(node))
		gui.set_size(cloned, gui.get_size(node))
		gui.set_scale(cloned, gui.get_scale(node))
		gui.set_pivot(cloned, gui.get_pivot(node))
		gui.animate(cloned, "color.w", 0, gui.EASING_INOUTSINE, 3, 0, nil, gui.PLAYBACK_LOOP_PINGPONG)
		if words[index].text then
			local text_metrics = helper.get_text_metrics_from_node(node)
			gui.set_size(cloned, vmath.vector3(text_metrics.width, text_metrics.height, 0))
		end
	end
end


function init(self)
	self.druid = druid.new(self)

	self.druid:new_scroll("scroll_view", "scroll_content")

	self.rich_text = self.druid:new(RichText, "rich_text_1")
	self.rich_text:set_text("Here is basic Rich Text without any text settings")

	self.rich_text_2 = self.druid:new(RichText, "rich_text_2")
	self.rich_text_2:set_text("Here is example to compare <color=FF0000>Rich Text</color> posing with usual GUI Text Node.")
	local rich_text_2_root = self.rich_text_2.root
	gui.animate(rich_text_2_root, "color.w", 0, gui.EASING_LINEAR, 4, 0, nil, gui.PLAYBACK_LOOP_PINGPONG)
	gui.animate(gui.get_node("text_case_2"), "color.w", 1, gui.EASING_LINEAR, 4, 0, nil, gui.PLAYBACK_LOOP_PINGPONG)
	--highlight_nodes(words2)

	self.rich_text_3 = self.druid:new(RichText, "rich_text_3")
	local words3 = self.rich_text_3:set_text("This example highlight every text node in Rich Text")
	highlight_nodes(words3)

	self.druid:new(RichText, "rich_text_4_1"):set_text("Text with image <img=logo,48/>at center")
	self.druid:new(RichText, "rich_text_4_2"):set_text("Text with image <img=logo,48/>scaled in GUI")
	self.druid:new(RichText, "rich_text_4_3"):set_text("Text with image <img=logo,48,48/>with fixed height")

	-- Docs: For images vertical anchor takes from ImagePrefab
	self.druid:new(RichText, "rich_text_5_NW"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_N"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_NE"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_W"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_C"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_E"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_SW"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_S"):set_text("Example text for pivots <img=logo,32/>")
	self.druid:new(RichText, "rich_text_5_SE"):set_text("Example text for pivots <img=logo,32/>")

	--self.rich_text_3 = self.druid:new(RichText, "rich_text_3")
	--local words3 = self.rich_text_3:set_text("Energy is full. To restore")
	--highlight_nodes(words3)
	self.rich_text_6 = self.druid:new(RichText, "rich_text_6")
	self.rich_text_6:set_text("Example text with <customlink><color=FFFF00>clickable tagged</color></customlink> words")
	local tagged = self.rich_text_6:tagged("customlink")
	pprint(tagged)
	for index = 1, #tagged do
		---@type rich_text.word
		local word = tagged[index]
		self.druid:new_button(word.node, function()
			print("on click tagged")
		end)
	end
end


function final(self)
	self.druid:final()
end


function update(self, dt)
	self.druid:update(dt)
end


function on_message(self, message_id, message, sender)
	self.druid:on_message(message_id, message, sender)
end


function on_input(self, action_id, action)
	return self.druid:on_input(action_id, action)
end
