local druid = require("druid.druid")


local function create_function(self, data, index, data_list)
	-- In data list create funtion we made all init stuff for this node
	local nodes = gui.clone_tree(self.prefab)
	local root = nodes["prefab"]
	gui.set_enabled(root, true)
	gui.set_enabled(nodes["checkbox"], data.is_checked)
	gui.set_text(nodes["text"], "Record " .. data.value)

	-- Since we have only 1 component, we can return this one
	-- If we have several components, we should create custom component to
	-- union them all
	local button = self.druid:new_button(root, function()
		data.is_checked = not data.is_checked
		gui.set_enabled(nodes["checkbox"], data.is_checked)
	end)
	button:set_click_zone(data_list.scroll.view_node)

	-- We should return in this function root node and optionally - Druid component
	return root, button
end


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

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

	local data = {}

	for i = 1, 999 do
		table.insert(data, {
			is_checked = false,
			value = i
		})
	end

	self.scroll = self.druid:new_scroll("data_list_view", "data_list_content")
	self.scroll:set_horizontal_scroll(false)
	self.grid = self.druid:new_static_grid("data_list_content", "prefab", 1)

	-- Pass already created scroll and grid components to data_list:
	self.data_list = self.druid:new_data_list(self.scroll, self.grid, create_function)

	self.data_list:set_data(data)
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
