class 'l_InputNameScreen' (BaseScreen)

local inputNameTextField = nil;
local InputNameScreen = nil;
------------------------------------------------------------------------------
function l_InputNameScreen:__init(name, screen)
	BaseScreen.__init(self, name, screen)
	
	self.X_OFFSET = 40
	
	-- create button
    self.bttn_ok     = self:CreateButtonByName('Ok',     KEY_ENTER)
	self.bttn_cancel = self:CreateButtonByName('Cancel', KEY_ESCAPE)
	
	if GM:GetCurrPlayer() == nil then
	    self.bttn_cancel:SetEnabled(false)
	end
    
    -- create title field
    local title = CreateTextEntity(MENU_FONT, 'INPUT_NAME_TITLE')
    title:AttachTo(self.screen)
	title:SetXY(512, 220)
	title:SetZ(190)
	title:SetScale(1)
	title:SetTracking(-1)
	title:SetSpacing(0.9)
	title:SetBorderSize(450, 100)
	title:SetTextAlignment(HGETEXT_TOP + HGETEXT_CENTER)
	
    -- create input field    	
    do
        inputNameTextField = CreateTextField()
        inputNameTextField:SetEditable(true)
        inputNameTextField:SetRect(275, 370, 490, 35)
        inputNameTextField:SetZ(190)
        inputNameTextField:AttachTo(screen)
        inputNameTextField:SetMaxLength(9)
        inputNameTextField:SetScale(1.25)
        inputNameTextField:SetAcceptsSeparatorCharacters(false)
        inputNameTextField:SetAttribute(FONT_NAME_ATTRIBUTE, INPUT_PLAYER_NAME_FONT)
        self.inputNameTextField = inputNameTextField
	end
	
	InputNameScreen = screen
end

function l_InputNameScreen:OnUpdate(dt)
    if self.inputNameTextField:GetText():len() > 0 then
        self.bttn_ok:SetEnabled(true);
    else
        self.bttn_ok:SetEnabled(false);
    end
end

function l_InputNameScreen:OnAllItemsFound()

end

function l_InputNameScreen:OnObjectMouseEnter(object)

end

function l_InputNameScreen:OnObjectMouseLeave(object)

end

function l_InputNameScreen:OnScreenObjectMouseDown(object)

end

function l_InputNameScreen:OnObjectMouseDownProcessed(is_wrong_click)

end

function l_InputNameScreen:OnObjectMouseUp(object)

end

function l_InputNameScreen:OnObjectFound(object)

end

function l_InputNameScreen:OnToolFound(tool)

end

function l_InputNameScreen:OnToolApply(tool, object)

end

function l_InputNameScreen:OnObjectReachPanel(object)

end

function l_InputNameScreen:OnButtonPress(title)

end

function l_InputNameScreen:OnMouseMove(previous_pos, current_pos)

end

function l_InputNameScreen:OnSetTool(tool)

end

function l_InputNameScreen:OnLoaded()

end

function l_InputNameScreen:OnDestroy()

end

--------------------------------------------------------------------------------
function CreateInputNameScreen(hideActiveScreen)
    local screen = CreateScriptedScreen('data/Scripts/screens/InputNameScreen.lua', 'l_InputNameScreen');
    PopUpScreensMgr:Push(screen, hideActiveScreen, nil);
end

--------------------------------------------------------------------------------
function l_InputNameScreen:OnOkButtonClick()
    local playerName = inputNameTextField:GetText();
    if playerName:len() > 0 then
        local playerAlreadyExists = GM:GetPlayer(playerName) ~= nil;
        if not playerAlreadyExists then
            GM:AddPlayer(playerName);
            GM:SetCurrPlayer(playerName);
			GM:SavePlayers();
        end
        
		if not playerAlreadyExists then
		    InputNameScreen = nil;
			PopUpScreensMgr:Pop();
			if ProfilesScreen then
			    ProfilesScreen:GetAssociatedLuaObject():UpdatePlayers()
			    PopUpScreensMgr:Pop();
                ProfilesScreen = nil;
            end
		end
        
        if playerAlreadyExists then
            CreateOKScreen(GetString('PROFILE_ALREADY_EXISTS'), nil, true)
        end
    end -- if playerName:len() > 0 then
end
--------------------------------------------------------------------------------
function l_InputNameScreen:OnCancelButtonClick()
    PopUpScreensMgr:Pop();
end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_InputNameScreen:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/GUI/Profiles/ProfilesBackground.png',0,0,498,482,1,0)
	anim:SetHotSpot(249,241)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Background',anim,512,384,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Buttons/bttn_ok.png',0,0,162,104,3,0)
	anim:SetHotSpot(81,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_ok',anim,422,600,120,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/Buttons/bttn_cancel.png',0,0,162,104,3,0)
	anim:SetHotSpot(81,52)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_cancel',anim,602,600,120,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
