local druid = require("druid.druid")


local function create_element(self, data, index, data_list)
	-- Here we will create elements without druid components, just nodes
	local instance = gui.clone_tree(self.prefab_square)
	gui.set_enabled(instance["prefab_square"], true)
	gui.set_text(instance["text_square"], data)
	return instance["prefab_square"]
end


local function init_data_list_with_static_grid(self)
	local scroll_simple = self.druid:new_scroll("data_list_simple", "data_list_simple_content")
		:set_vertical_scroll(false)

	--- The 999999 number in row - is kind of hack for single line static grid
	local grid_simple = self.druid:new_static_grid("data_list_simple_content", "prefab_square", 999999)

	self.data_list_simple = self.druid:new_data_list(scroll_simple, grid_simple, create_element)
	self.data_list_simple:set_data(self.data_set)

	self.slider_simple = self.druid:new_slider("grid_scroll_pin", vmath.vector3(200, 0, 0))
	-- Disable slider input, now we can not use slider with data list
	self.slider_simple:set_input_enabled(false)
	self.data_list_simple.on_scroll_progress_change:subscribe(function(_, progress)
		self.slider_simple:set(progress, true)
	end)

	self.druid:new_button("button_simple_first/button", function()
		self.data_list_simple:scroll_to_index(1)
	end)
	self.druid:new_button("button_simple_hundred/button", function()
		self.data_list_simple:scroll_to_index(100)
	end)
	self.druid:new_button("button_simple_last/button", function()
		self.data_list_simple:scroll_to_index(#self.data_set)
	end)
end


local function init_data_list_with_dynamic_grid(self)
	local scroll_dynamic = self.druid:new_scroll("data_list_dynamic", "data_list_dynamic_content")
		:set_vertical_scroll(false)

	local grid_dynamic = self.druid:new_dynamic_grid("data_list_dynamic_content")

	self.data_list_dynamic = self.druid:new_data_list(scroll_dynamic, grid_dynamic, create_element)
	self.data_list_dynamic:set_data(self.data_set)

	self.slider_dynamic = self.druid:new_slider("grid_scroll_dynamic_pin", vmath.vector3(200, 0, 0))
	-- Disable slider input, now we can not use slider with data list
	self.slider_dynamic:set_input_enabled(false)
	self.data_list_dynamic.on_scroll_progress_change:subscribe(function(_, progress)
		self.slider_dynamic:set(progress, true)
	end)

	self.druid:new_button("button_dynamic_first/button", function()
		self.data_list_dynamic:scroll_to_index(1)
	end)
	self.druid:new_button("button_dynamic_hundred/button", function()
		self.data_list_dynamic:scroll_to_index(100)
	end)
	self.druid:new_button("button_dynamic_last/button", function()
		self.data_list_dynamic:scroll_to_index(#self.data_set)
	end)
end


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

	self.data_set = {}
	for i = 1, 999 do
		table.insert(self.data_set, i)
	end

	self.prefab_square = gui.get_node("prefab_square")
	gui.set_enabled(self.prefab_square, false)


	init_data_list_with_static_grid(self)
	init_data_list_with_dynamic_grid(self)
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
