#include "ui/ui_extensions.lua"
#include "ui/components/eula/english.lua"
#include "ui/components/eula/chinese.lua"
#include "ui/components/eula/french.lua"
#include "ui/components/eula/italian.lua"
#include "ui/components/eula/german.lua"
#include "ui/components/eula/japanese.lua"
#include "ui/components/eula/polish.lua"
#include "ui/components/eula/russian.lua"
#include "ui/components/eula/spanish.lua"


function stringSplit(str, sep)
    sep = sep or "%s"

    local t = {}
    for s in string.gmatch(str, "([^"..sep.."]+)") do
        table.insert(t, s)
    end

    return t
end

EULA.Language =
{
	"english",
	"french",
	"spanish",
	"italian",
	"german",
	"chinese",
	"japanese",
	"russian",
	"polish"
}

EULA.Window = Ui.Window
{
	w = 800,
	h = 720,

	allowHide = false,

	animator = { playTime = 0 },

	Ui.ImageBox
	{
		w = 800,
		h = 720,

		path = "common/box-9.png",
		border = { width = 9, height = 9 },
		color = 0x100000099
	},

	Ui.Frame
	{
		w = 800,
		h = 720,

		Ui.VerticalLayout
		{
			hAlign = Ui.ContentLayout.Horizontal.Center,
			alignment = Ui.ContentLayout.Horizontal.Center,
			margin = { top = 30 },
			padding = 20,

			Ui.Text
			{
				id = "header",
				font = { "bold.ttf", 30 },
				text = "header",
				linesAlignment = Ui.TextAlignment.Center,
				wrapWidth = 715,
			},

			Ui.ScrollableArea
			{
				id = "eulaText",

				w = 720,
				h = 540,

				hAlign = Ui.ContentLayout.Horizontal.Center,

				Ui.VerticalLayout
				{
					id = "linesView",
					padding = 22
				},

				onPreDraw = function(self)
					local scrollSpeed = 5000
					local inputValue = InputValue("up") - InputValue("down")
					inputValue = math.abs(inputValue) < 0.1 and 0 or inputValue
					local scrollValue = inputValue * scrollSpeed * GetTimeStep()
					self.scrollBar:addOffset(scrollValue, LastInputDevice() == UI_DEVICE_GAMEPAD)
				end
			},

			onCreate = function(self)
				local lang = GetInt("options.language") + 1
				if lang == 6 or lang == 7 then
					self.alignment = Ui.ContentLayout.Horizontal.Left
				end
			end
		},

		Ui.HorizontalLayout
		{
			hAlign = Ui.ContentLayout.Horizontal.Center,
			vAlign = Ui.ContentLayout.Vertical.Bottom,

			margin = { bottom = 15 },
			padding = 20,

			Ui.Text
			{
				id = "acceptBtn",

				font = { "medium.ttf", 22 },
				colorFilter = { 1, 1, 1, 0.6 },

				onCreate = function(self)
					self.text = "[[menu:menu_accept;iconsize=42,42;color=255,255,255]]" .. GetTranslatedStringByKey("UI_EULA_AGREE")
				end,

				onPreDraw = function(self)
					if InputPressed("menu_accept") then
						self.root:hide()
					end
				end
			},

			Ui.Text
			{
				id = "scrollBtn",

				font = { "medium.ttf", 22 },
				colorFilter = { 1, 1, 1, 0.6 },

				onCreate = function(self)
					self.text = self:getIcon() .. GetTranslatedStringByKey("UI_TEXT_SCROLL")
				end,

				getIcon = function(self)
					if IsRunningOnPlaystation() or IsRunningOnXbox() then
						return "[[menu:gamepad_right_stick_y;iconsize=42,42;color=255,255,255]]"
					else
						return "[[menu:gamepad_right_stick_up;iconsize=42,42;color=255,255,255]][[menu:gamepad_right_stick_down;iconsize=42,42;color=255,255,255]]"
					end
				end
			}
		},

		onCreate = function(self)
			self.linesView = self:findById("linesView")
			self.header = self:findById("header")
			self.eulaText = self:findById("eulaText")

			-- do not fill the scrollbar with strings to save memory
			-- because this flag won't let the eula window to be shown anymore
			if GetBool("savegame.waseulashown") then
				for i=1,#EULA.Language do
					EULA[EULA.Language[i]] = nil
				end
				self.linesView:clear()
				-- collectgarbage() TODO: read text from registry. txt -> registry -> lua
				return
			end

			local lang = EULA.Language[GetInt("options.language") + 1]
			local text = EULA[lang]
			local strings = stringSplit(text, '\n')

			self.header.text = strings[1] .. "\n" .. strings[2]

			for i=3,#strings do
				local line = Ui.Text
				{
					font = { "medium.ttf", 22 },
					text = strings[i],
					wrapWidth = 715,
					colorFilter = { 1, 1, 1, 0.6 }
				}

				self.linesView:append(line)
			end
		end,

		onPostDraw = function(self)
			self.eulaText.h = self.h - self.header.h - 135
		end
	},

	onShow = function(self)
		self.acceptBtn = self:findById("acceptBtn")
		self.scrollBtn = self:findById("scrollBtn")
		self.eulaText = self:findById("eulaText")

		self.animator.playTime = 0.25

		local canvas = UiCanvasSize()

		self.x = canvas.w / 2 - self.w / 2
		self.y = canvas.h / 2 - self.h / 2
	end,

	onClose = function(self)
		SetBool("savegame.waseulashown", true)
		self.eulaText:clear()
	end
}