--------------------------------------------------------------------------------
inputPlayerNameScreen = nil;

playerSelectScreen = nil;
playerDeleteScreen = nil;

--------------------------------------------------------------------------------
function ShowInputPlayerNameScreen(additionalMessageText, inputNameTextFieldDy)
    
    if not additionalMessageText then
        additionalMessageText = ''
    end
    if not inputNameTextFieldDy then
        inputNameTextFieldDy = 0
    end
    
    local screen = CreateScreen();
    screen:SetLayer(POPUP_WINDOWS_LAYER);

-- Square_dialog_back.png
    anim = screen:CreateAnimation("data/Texture/GUI/Square_dialog_back.png"); -- 618 474
    anim:SetHotSpot(309, 237);
    anim:SetPosXY(512, 350);
    anim:SetPosZ(0);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    
    --------
    local OKButton = MakeButton(446, 570, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
	OKButton:GetCaption():SetScale(0.8);
	OKButton:SetZ(2);
	
	OKButton:AttachTo(screen);
	OKButton:SetAction("InputPlayerNameConfirm");
	OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
	--OKButton:SetTitle("OK");
	OKButton:SetActionSound("Menu_button_click");
	OKButton:SetEnabled(false);
	screen.OKButton = OKButton;
	
	--------
	local cancelButton = MakeButton(578, 570, 100, 40);
    cancelButton:GetCaption():SetFont(BUTTONS_FONT);
	cancelButton:GetCaption():SetScale(0.8);
	cancelButton:SetZ(2);
	
	cancelButton:AttachTo(screen);
	cancelButton:SetAction("InputPlayerNameDiscard");
	cancelButton:SetSpriteUp("CancelButton");
	cancelButton:SetSpriteOver("CancelButtonOver");
	cancelButton:SetSpriteDown("CancelButtonDown");
	--cancelButton:SetTitle("Cancel");
	cancelButton:SetActionSound("Menu_button_click");
	
	if (GM:GetCurrPlayer() == nil)
	then
	    cancelButton:SetEnabled(false);
	end
    
    --------
    local inputNameTitle = CreateTextEntity(INGAME_MENU_FONT, "ENTERNAME");
    inputNameTitle:SetText(additionalMessageText..inputNameTitle:GetText());
	--textBox:SetHotSpot(298, 206);
	inputNameTitle:SetXY(512, 210);
	--inputNameTitle:SetScale(1.0);
	inputNameTitle:SetRect(265, 200, 490, 35);
	inputNameTitle:SetZ(2);
	inputNameTitle:SetBorderSize(490, 35);
	inputNameTitle:AttachTo(screen);
	
	--------
    screen.inputNameTextField = CreateTextField();
    screen.inputNameTextField:SetEditable(true);
	screen.inputNameTextField:SetRect(265, 360 + inputNameTextFieldDy, 490, 35);
	screen.inputNameTextField:SetZ(2);
	screen.inputNameTextField:AttachTo(screen);
	
	screen.inputNameTextField:SetMaxLength(10);
	screen.inputNameTextField:SetAcceptsSeparatorCharacters(false);
	screen.inputNameTextField:SetAttribute(INGAME_MENU_FONT, FONT_NAME_ATTRIBUTE);
	screen.inputNameTextField:SetAction("InputPlayerNameConfirm");
	
	NotificationCenter:AddObserver("InputPlayerNameChanged", "TextChanged", screen.inputNameTextField);
	
    inputPlayerNameScreen = screen;
    PopUpScreensMgr:Push(inputPlayerNameScreen);
    
    return screen
end

function ShowLoadPlayersError(message)

    local zOffset = 50;
    local screen = CreateScreen();
    screen:SetName("OKScreen " .. message);
    screen:SetLayer(POPUP_WINDOWS_LAYER);

    local bg = screen:CreateAnimation("data/Texture/GUI/Square_dialog_back.png"); -- 618 474
    bg:SetHotSpot(309, 237);
    bg:SetPosXY(512, 350);
    bg:SetPosZ(0);
    bg:SetScaleXY(1, 1);
    bg:SetAngle(0);
    
    local OKButton = MakeButton(512, 570, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
	OKButton:GetCaption():SetScale(0.8);
	OKButton:SetZ(5 + zOffset);
	
	OKButton:AttachTo(screen);
	OKButton:SetAction("PopCurrentPopUpScreen");
	OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
	--OKButton:SetTitle("OK");
	OKButton:SetActionSound("Menu_button_click");
	
	local textBox = CreateTextEntityWOLocalization(INGAME_MENU_FONT, message);
	--textBox:SetHotSpot(298, 206);
	textBox:SetXY(512, 360);
	textBox:SetZ(1 + zOffset);
	textBox:SetScale(1.0);
	textBox:SetRect(412, 264, 200, 200);
	textBox:AttachTo(screen);
	textBox:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
	textBox:SetBorderSize(490, 35);
	
	screen.textBox = textBox;
    
    return screen;
    
end

--------------------------------------------------------------------------------
function InputPlayerNameConfirm()

    local playerName = inputPlayerNameScreen.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
        
        PopUpScreensMgr:Pop();
        --inputPlayerNameScreen:ScreenDestroy();
        inputPlayerNameScreen = nil;
        
        if playerAlreadyExists then
            local alreadyExistsNotification = BuildOKScreen('PROFILE_ALREADY_EXISTS', "PopCurrentPopUpScreen")
            PopUpScreensMgr:Push(alreadyExistsNotification);
        end
        
        if (playerSelectScreen ~= nil)
        then
            playerSelectScreen:ReloadData();
        elseif not IsPresentExtrasScreenShown()
        then
            StartPresentExtrasScreen()
        end
    end

end

--------------------------------------------------------------------------------
function InputPlayerNameDiscard()

    PopUpScreensMgr:Pop();
    inputPlayerNameScreen = nil;
    
end

--------------------------------------------------------------------------------
function ShowPlayerSelectScreen()

    local screen = CreatePlayersScreen();
    screen:SetLayer(POPUP_WINDOWS_LAYER);
    
    anim = screen:CreateAnimation("data/Texture/GUI/Square_dialog_back.png"); -- 618 474
    anim:SetHotSpot(309, 237);
    anim:SetPosXY(512, 350);
    anim:SetPosZ(0);
    anim:SetScaleXY(1, 1);
    anim:SetAngle(0);
    
    --------
    local OKButton = MakeButton(512, 570, 100, 40);
    OKButton:GetCaption():SetFont(BUTTONS_FONT);
	OKButton:GetCaption():SetScale(0.8);
	OKButton:SetZ(5);
	
	OKButton:AttachTo(screen);
	OKButton:SetAction("PlayerSelectConfirm");
	OKButton:SetSpriteUp("OKButton");
	OKButton:SetSpriteOver("OKButtonOver");
	OKButton:SetSpriteDown("OKButtonDown");
	--OKButton:SetTitle("OK");
	OKButton:SetActionSound("Menu_button_click");
	
	--------
	local newButton = MakeButton(379, 570, 100, 40);
    newButton:GetCaption():SetFont(BUTTONS_FONT);
	newButton:GetCaption():SetScale(0.8);
	newButton:SetZ(5);
	
	newButton:AttachTo(screen);
	newButton:SetAction("PlayerSelectNew");
	newButton:SetSpriteUp("NewButton");
	newButton:SetSpriteOver("NewButtonOver");
	newButton:SetSpriteDown("NewButtonDown");
	--newButton:SetTitle("New");
	newButton:SetActionSound("Menu_button_click");
	
	--------
	local deleteButton = MakeButton(645, 570, 100, 40);
    deleteButton:GetCaption():SetFont(BUTTONS_FONT);
	deleteButton:GetCaption():SetScale(0.8);
	deleteButton:SetZ(5);
	
	deleteButton:AttachTo(screen);
	deleteButton:SetAction("PlayerSelectDelete");
	deleteButton:SetSpriteUp("DeleteButton");
	deleteButton:SetSpriteOver("DeleteButtonOver");
	deleteButton:SetSpriteDown("DeleteButtonDown");
	--deleteButton:SetTitle("Delete");
	deleteButton:SetActionSound("Menu_button_click");
    
    SetChangePlayerButtonVisible(false);
    
    playerSelectScreen = screen;
    PopUpScreensMgr:Push(playerSelectScreen, false);

end
--------------------------------------------------------------------------------
function InputPlayerNameChanged()
    inputPlayerNameScreen.OKButton:SetEnabled(inputPlayerNameScreen.inputNameTextField:GetText():len() > 0)
end
--------------------------------------------------------------------------------
function PlayerSelectConfirm()

    if (playerSelectScreen == nil)
    then
        return;
    end

    playerSelectScreen:PerformOK();
    
    UpdatePlayerWelcomeString();
    
    PopUpScreensMgr:Pop();
    playerSelectScreen = nil;
    
    SetChangePlayerButtonVisible(true)
    
    if not IsPresentExtrasScreenShown() then
        StartPresentExtrasScreen()
    end
end

--------------------------------------------------------------------------------
function PlayerSelectNew()

    if GM:GetProfilesCount() >= MAX_PROFILES
    then
        local scr = BuildOKScreen("MAX_PROFILES", "PopCurrentPopUpScreen");
        PopUpScreensMgr:Push(scr);
        return;
    end

    ShowInputPlayerNameScreen();

    --playerSelectScreen = nil;

end

--------------------------------------------------------------------------------
function PlayerSelectDelete()

    if (playerSelectScreen == nil)
    then
        return;
    end

    playerDeleteScreen = BuildYesNoScreen("REALLY_DELETE_PROFILE",
                                          "PlayerDeleteConfirm",
                                          "PlayerDeleteDiscard");
                                          
    playerDeleteScreen.playerToDelete = playerSelectScreen:GetSelectedPlayer();
    
    PopUpScreensMgr:Push(playerDeleteScreen);
    
    playerDeleteScreen.yesButton:Stop();
    playerDeleteScreen.noButton:Stop();
    
    TimerDo(GM:GetTimer(),
            0.5,
            function()
                if playerDeleteScreen then
                    playerDeleteScreen.yesButton:Go();
                    playerDeleteScreen.noButton:Go();
                end
            end)
end

--------------------------------------------------------------------------------
function PlayerDeleteConfirm()

    if (playerDeleteScreen == nil)
    then
        return;
    end
    
    GM:DeletePlayer(playerDeleteScreen.playerToDelete);
    GM:SavePlayers();
    
    if (GM:GetCurrPlayer() == nil)
    then
        PopUpScreensMgr:DestroyAll();
        
        inputPlayerNameScreen = nil;
        playerSelectScreen = nil;
        playerDeleteScreen = nil;
        
        ShowInputPlayerNameScreen();
        return;
    end
    
    if (playerSelectScreen ~= nil)
    then
        playerSelectScreen:ReloadData();
    end

    PopUpScreensMgr:Pop();
    playerDeleteScreen = nil;

end

--------------------------------------------------------------------------------
function PlayerDeleteDiscard()
    
    if (playerDeleteScreen == nil)
    then
        return;
    end
    
    PopUpScreensMgr:Pop();
    playerDeleteScreen = nil;

end

--------------------------------------------------------------------------------