local druid = require("druid.druid")

local empty_style = require("druid.styles.empty.style")
local default_style = require("druid.styles.default.style")

local main_page = require("example.page.main_page")
local text_page = require("example.page.texts_page")
local button_page = require("example.page.button_page")
local scroll_page = require("example.page.scroll_page")
local slider_page = require("example.page.slider_page")
local input_page = require("example.page.input_page")
local grid_page = require("example.page.grid_page")

local pages = {
	"main_page",
	"texts_page",
	"button_page",
	"scroll_page",
	"slider_page",
	"input_page",
	"grid_page",
}

local function on_control_button(self, delta)
	self.page = self.page + delta
	self.page = math.max(1, self.page)
	self.page = math.min(self.page, #pages)

	self.header:translate(pages[self.page])
	local node = gui.get_node("C_Anchor")
	gui.animate(node, "position.x", (self.page-1) * -600, gui.EASING_OUTSINE, 0.2)
end


local function init_top_panel(self)
	self.druid:new_blocker("panel_top")
	self.druid:new_button("button_left/button", on_control_button, -1)
	self.druid:new_button("button_right/button", on_control_button, 1)
	self.header = self.druid:new_lang_text("text_header", "main_page")
end


local function init_swipe_control(self)
	self.druid:new_swipe("root", function(_, side)
		if side == "left" then
			on_control_button(self, 1)
		end
		if side == "right" then
			on_control_button(self, -1)
		end
	end)
end


local function on_window_callback(self, event, data)
	druid.on_window_callback(event, data)
end


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

	window.set_listener(on_window_callback)

	init_swipe_control(self)

	self.page = 1
	main_page.setup_page(self)
	text_page.setup_page(self)
	button_page.setup_page(self)
	scroll_page.setup_page(self)
	slider_page.setup_page(self)
	input_page.setup_page(self)
	grid_page.setup_page(self)

	init_top_panel(self)

	-- Refresh state
	on_control_button(self, 0)
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)
	self.druid:on_input(action_id, action)
end
