--***********************************************************
--**                    THE INDIE STONE                    **
--***********************************************************

CoopConnectPopup = ISPanel:derive("CoopConnectPopup")

function CoopConnectPopup:create()
	self.connectLabel = ISLabel:new(self.width / 2, 96, 50, "", 1, 1, 1, 1, UIFont.Medium, true)
	self.connectLabel:initialise()
	self.connectLabel:instantiate()
	self.connectLabel:setAnchorLeft(true)
	self.connectLabel:setAnchorRight(true)
	self.connectLabel:setAnchorTop(true)
	self.connectLabel:setAnchorBottom(false)
	self.connectLabel.center = true
	self:addChild(self.connectLabel)

	self.backBtn = ISButton:new((self.width - 70) / 2, self.height - 50, 70, 25, getText("UI_btn_back"), self, self.onBackButton)
	self.backBtn.internal = "BACK"
	self.backBtn:initialise()
	self.backBtn:instantiate()
	self.backBtn:setAnchorLeft(true)
	self.backBtn:setAnchorRight(true)
	self.backBtn:setAnchorTop(true)
	self.backBtn:setAnchorBottom(false)
	self:addChild(self.backBtn)

	Events.OnConnected.Add(CoopConnectPopup.OnConnected)
	Events.OnConnectFailed.Add(CoopConnectPopup.OnConnectFailed)
	Events.OnConnectionStateChanged.Add(CoopConnectPopup.OnConnectionStateChanged)
	Events.OnResetLua.Add(CoopConnectPopup.onResetLua)
end

function CoopConnectPopup:prerender()
	CoopConnectPopup.instance = self
	ISPanel.prerender(self)
	self:drawTextCentre(getText("UI_CoopConnectPopup_Title"), self.width / 2, 20, 1, 1, 1, 1, UIFont.Large)
	self.backBtn:setEnable(not self.connecting)
end

function CoopConnectPopup:connect(serverSteamID)
	self.connecting = true
	self.failMessage = nil
	self.connectLabel.name = getText("UI_servers_Connecting")
	serverConnectCoop(serverSteamID)
end

function CoopConnectPopup:onBackButton()
	self:setVisible(false)
	MainScreen.instance.bottomPanel:setVisible(true)
end

function CoopConnectPopup:new(x, y, width, height)
	local o = ISPanel:new(x, y, width, height)
	setmetatable(o, self)
	self.__index = self
	o:setUIName("CoopConnectPopup")
	CoopConnectPopup.instance = o
	return o
end

function CoopConnectPopup.OnConnected()
    -- ServerList and PublicServerList also have Events.OnConnectFailed callbacks.
    if not CoopConnectPopup.instance:getIsVisible() then return end
    CoopConnectPopup.instance.connecting = false
    CoopConnectPopup.instance:setVisible(false)
end

function CoopConnectPopup.OnConnectFailed(message)
    -- ServerList and PublicServerList also have Events.OnConnectFailed callbacks.
    if not CoopConnectPopup.instance:getIsVisible() then return end
    CoopConnectPopup.instance.connecting = false
    if not CoopConnectPopup.instance.failMessage then CoopConnectPopup.instance.failMessage = message end
    if not message then message = CoopConnectPopup.instance.failMessage end
    CoopConnectPopup.instance.connectLabel.name = message or getText("UI_servers_connectionfailed")
end

function CoopConnectPopup.OnConnectionStateChanged(state, message)
    if not CoopConnectPopup.instance:getIsVisible() then return end
    print(state .. ',' .. tostring(message))
    if state == "Disconnecting" then return end
    if state == "Failed" and message then CoopConnectPopup.instance.failMessage = getText('UI_servers_'..message) return end
    CoopConnectPopup.instance.connectLabel.name = state and getText('UI_servers_'..state) or "???"
end

function CoopConnectPopup.onResetLua(reason)
    if false and reason == "ConnectedToServer" then
        MainScreen.instance.bottomPanel:setVisible(false)
        CoopConnectPopup.instance:setVisible(false)
    end
end

