--[[
* The MIT License
* Copyright (C) 2014 Bacioiu "Zapa" Ciprian (bacioiu.ciprian@gmail.com).  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom th be Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
local textstyles = require "gui/textstyles"


function interface:initLobbyUi( )

	self._temporaryMapList = { }
	self._temporaryMapList[1] = "battledeploy"
	self._temporaryMapList[2] = "beanisland"
	self._temporaryMapList[3] = "firstfront"
	self._temporaryMapList[4] = "flanked"
	self._temporaryMapList[5] = "island1"

	local roots, widgets, groups = g:loadLayout(resources.getPath("multiLobby.lua"))

	if (nil ~= widgets.hostButton) then
		self._mlHOST = widgets.hostButton.window
		self._mlHOST:registerEventHandler(self._mlHOST.EVENT_BUTTON_CLICK, nil, _HostPressed)
	else
	end

	if (nil ~= widgets.joinButton) then
		self._mlJoin = widgets.joinButton.window
		self._mlJoin:registerEventHandler(self._mlJoin.EVENT_BUTTON_CLICK, nil, _joinPressed)
	else
	end

	if (nil ~= widgets.startButton) then
		self._mStart = widgets.startButton.window
		self._mStart:registerEventHandler(self._mStart.EVENT_BUTTON_CLICK, nil, _startPressed)
	else

	end

	if (nil ~= widgets.statusLabel) then
		self._mStatusLabel = widgets.statusLabel.window
		--self._mlJoin:registerEventHandler(self._mlJoin.EVENT_BUTTON_CLICK, nil, _joinPressed)
	else
	end

	if (nil ~= widgets.mapList) then
		self._mMapListW = widgets.mapList.window
		self._mMapListW:registerEventHandler(self._mMapListW.EVENT_WIDGET_LIST_SELECT, nil, _mapSelPressed)
		self._mMapListW:registerEventHandler(self._mMapListW.EVENT_WIDGET_LIST_UNSELECT, nil, _mapSelReleased)
	else

	end

	self._ipBox = widgets.mapList.ipInfo
	self._portBox = widgets.mapList.portInfo

	for i = 1, 5 do
		local row = self._mMapListW:addRow()

		-- The return from getCell is the widget created by setColumnWidget, so the normal
		-- functionality for the widget is available.
		if i < 5 then
			row:getCell(1):setText("Map: ")
			row:getCell(2):setText(""..self._temporaryMapList[i].."")
		end
	end
	self._mMapListW:_displayRows()
	
end

function interface:_returnMapListTable( )
	return self._temporaryMapList
end

function interface:_returnIPContent( )
	return self._ipBox
end


function interface:_returnLobbyMapListSelection( )
	local widgetList = self._mMapListW
	--local text = element.widgets.textBox1.window
	--if widgetList:getSelections() == 6 then
	local selection = widgetList:getSelections()
	return selection[1]
end

function interface:_setLobbyMapListSelection(_value)
	local widgetList = self._mMapListW
	widgetList:setSelection(_value)
end

function interface:setStatusLabel(_text)
	self._mStatusLabel:setText(_text)
end

function _HostPressed( )
	print("TIME TO HOSTA")
	multiplayer:createServer( )
end

function _joinPressed( )
	multiplayer:joinServer( )
	print("JOIN IN DA FUN")
end

function _mapSelPressed( )
	multiplayer:sendSelection(  )
end

function _startPressed( )
	multiplayer:broadcastStartGame( )
end