require "screen"
require "button"
require "animbutton"
require "image"
require "uianim"

-- TODO: strings, commands in group

KeyScreen = Class(Screen, function(self, buttons, texthalign, additionaltext, textsize, timeout)
	Screen._ctor(self, "KeyScreen")

	--darken everything behind the dialog
	self.blackoverlay = self:AddChild(Image("data/images/square.tex"))
	local w, h = self.blackoverlay:GetSize()

	--throw up the background
    self.bg = self:AddChild(Image("data/images/map.tex"))
    self.bg:SetVRegPoint(ANCHOR_MIDDLE)
    self.bg:SetHRegPoint(ANCHOR_MIDDLE)
    self.bg:SetVAnchor(ANCHOR_MIDDLE)
    self.bg:SetHAnchor(ANCHOR_MIDDLE)
    self.bg:SetScaleMode(SCALEMODE_FILLSCREEN)
	
	self.root = self:AddChild(Widget("ROOT"))
    self.root:SetVAnchor(ANCHOR_MIDDLE)
    self.root:SetHAnchor(ANCHOR_MIDDLE)
    self.root:SetPosition(0,0,0)
    self.root:SetScaleMode(SCALEMODE_PROPORTIONAL)

	-- menu
	local button_w = 200
	local space_between = 20
	local spacing = button_w + space_between
	
	self.menu = self:AddChild(Widget("menu"))
	local total_w = #buttons*button_w
	if #buttons > 1 then
		total_w = total_w + space_between*(#buttons-1) 
	end
	
	self.menu:SetHAnchor(ANCHOR_MIDDLE)
	self.menu:SetVAnchor(ANCHOR_MIDDLE)
	self.menu:SetPosition(-(total_w / 2) + button_w/2, -120,0) 

	-- buttons
	local pos = Vector3(400,-210,0)
	for k,v in ipairs(buttons) do
		local button = self.menu:AddChild(AnimButton("button"))
	    button:SetPosition(pos)
	    button:SetText(v.text)
	    if v.nopop == true then
	    	button:SetOnClick( function() v.cb() end )
	    else
	    	button:SetOnClick( function() TheFrontEnd:PopScreen(self) v.cb() end )
	    end
		button.text:SetColour(0,0,0,1)
	    button:SetFont(BUTTONFONT)
	    button:SetTextSize(40)    
	    pos = pos + Vector3(spacing, 0, 0)  
	end

	if timeout then
		self.timeout = timeout
	end
	
	self.buttons = buttons
	
	-- footer text
	local footer = self:AddChild(Text(NUMBERFONT, 28))
	footer:SetHAnchor(ANCHOR_MIDDLE)
	footer:SetVAnchor(ANCHOR_BOTTOM)
    footer:SetPosition(0, 150, 0)
    footer:SetString("汉化: 男心慢慢 （3DM饥荒论坛）.")
	
	-- commands text
	local commands = self:AddChild(Text(DIALOGFONT, 22))
	commands:SetHAnchor(ANCHOR_MIDDLE)
	commands:SetVAnchor(ANCHOR_BOTTOM)
	commands:SetRegionSize(1000, 100)
	commands:EnableWordWrap(true)
    commands:SetPosition(0, 275, 0)
    commands:SetString("注: 这是目前饥荒最好用的一个修改器， 本次汉化权归原汉化作者所有, 他汉化了70%的文本, 我是在他的汉化基础上进行了完整的汉化. 本人不推存过度使用修改器, 请放弃使用或酌情使用以免失去游戏乐趣. （3DM饥荒论坛）")
	
	local spawning = {}
	table.insert(spawning, {"Ctrl + X = 生成指定道具"})
	table.insert(spawning, {"Ctrl + T = 生成指定食物"})
	table.insert(spawning, {"Shift + Z = 生成指定怪物"})
	table.insert(spawning, {"Shift + R = 生成指定物品"})
	table.insert(spawning, {"Shift + X/T = 鼠标位置生成 食物\道具"})
	
	local toggles = {}
	table.insert(toggles, {"Ctrl + C = 解锁科技并无消耗"})
	table.insert(toggles, {"Ctrl + R = 加速"})
	table.insert(toggles, {"Ctrl + G = 上帝模式"})
	table.insert(toggles, {"Ctrl + V = 世界加速 "})
	table.insert(toggles, {"Ctrl + I = 天气切换"})	
	
	local misc = {}
	table.insert(misc, {"Shift + C = 换人物"})
	table.insert(misc, {"Shift+ G = 下一个时间"})
	table.insert(misc, {"Shift + F = 下一天"})
	table.insert(misc, {"Ctrl + Z = 切换夏天冬天"})
	table.insert(misc, {"Shift + B = 删去鼠标所指生物或者物体"})	
	table.insert(misc, {"Alt+ R = 开全图"})	
	table.insert(misc, {"Alt + T = 关全图"})	
	
	local teleport = {}
	table.insert(teleport, {"Ctrl + Shift + 鼠标左键 = 瞬移到鼠标所点"})
	table.insert(teleport, {"Ctrl + 鼠标右键 = 向后闪避"})
	table.insert(teleport, {"Shift + 鼠标右键 = 远程跳？"})
	
	local spawningPos = {-450, 200, 0}
	self:AddGroups(spawning, spawningPos, "生成")
	
	local togglesPos = {-200, 200, 0}
	self:AddGroups(toggles, togglesPos, "转换")	
		
	local miscPos = {50, 200, 0}
	self:AddGroups(misc, miscPos, "杂项")
	
	local teleportPos = {400, 200, 0}
	self:AddGroups(teleport, teleportPos, "移动")	
end)

function KeyScreen:AddGroups(data, offset, header)
	local position = { 0, 0, 0 }
	if offset then
		position[1] = position[1] + offset[1]
		position[2] = position[2] + offset[2]
		position[3] = position[3] + offset[3]
	end

	local master = self:AddChild(Text(TITLEFONT, 34, header))
	master:SetVAnchor(ANCHOR_MIDDLE)
	master:SetHAnchor(ANCHOR_MIDDLE)
	master:SetPosition(position[1], position[2], position[3])
	
	local group = master:AddChild( Widget( "TestGroup" ) )
	group:SetPosition(0, -20, 0) 
	
	local fontSize = 22
	local label_width = 275
	
	for index,entry in ipairs(data) do
		local text = group:AddChild( Text(DEFAULTFONT, fontSize, entry[1]))
		text:SetPosition( 0, index * -25, 0 )
		--text:SetRegionSize(label_width, 40)
		--text:EnableWordWrap(true)
	end
end

function KeyScreen:OnUpdate( dt )
	if self.timeout then
		self.timeout.timeout = self.timeout.timeout - dt
		if self.timeout.timeout <= 0 then
			self.timeout.cb()
		end
	end
	return true
end

function KeyScreen:OnKeyUp( key )
	if key == KEY_ENTER or key == KEY_ESCAPE then
		if self.buttons[1] then
			TheFrontEnd:PopScreen(self) self.buttons[1].cb()
		end
	end
end