dofile('data/Lua/Screens/DialogYesNo.lua')

function PostLoad(screen)
	game_state.title = CreateTextEntity("Calisto_MT_31", GetString("Q_ENTER_NAME_TITLE"))
	game_state.title:AttachTo(screen:GetObject('BG'))
	game_state.title:SetZ(90)
	game_state.title:SetY(-110)
	
	game_state.players_name = CreateTextEntity("SchoolbookBI31", "|")
	game_state.players_name:AttachTo(screen:GetObject('BG'))	
	game_state.players_name:SetScale(0.8)
	game_state.players_name:SetZ(90)
	game_state.players_name:SetY(-60)
	
	game_state.allowed_characters = {}
	local ac = game_state.allowed_characters
	--ac.insert()
end

function InitButtons(screen)
    local up = screen:GetObject("buttonleftup"):GetAnimationPtr()
	local over = screen:GetObject("buttonleftover"):GetAnimationPtr()	
	local down = screen:GetObject("buttonleftdown"):GetAnimationPtr()	
	game_state.exit_button = CreateButton(up, over, down, "Yes", screen)
	game_state.exit_button:AttachTo(screen)
	game_state.exit_button:SetXY(484, 417)
	game_state.exit_button:SetZ(140)
	
	local up = screen:GetObject("buttonrightup"):GetAnimationPtr()
	local over = screen:GetObject("buttonrightover"):GetAnimationPtr()	
	local down = screen:GetObject("buttonrightdown"):GetAnimationPtr()	
	game_state.resume_button = CreateButton(up, over, down, "No", screen)
	game_state.resume_button:AttachTo(screen)
	game_state.resume_button:SetXY(565, 414)
	game_state.resume_button:SetZ(140)
end


function OnUpdate(dt)
	local ch = GetChar()
	if not ch or ch == 0 then
	    return
	end
	local text = game_state.players_name:GetText()
	text = text:sub(1, text:len() - 1)
	local size = text:len()

	if ch == HGEK_BACKSPACE then
	   if size > 0 then
	       text = text:sub(1, text:len() - 1)
		   game_state.players_name:SetText(text)
	   end
	else
   	   if ch ~= 0 and size < 10 and ch ~= HGEK_ENTER and ch ~= HGEK_ESCAPE then
            text = text..string.char(ch)
	   end
    end
    game_state.players_name:SetText(text.."|")
end

function UpdateSound()
end

function UpdateMusic()
end

function OnExit()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
end

function OnObjectMouseUp(object)
    if dialog.destroying then
        return
    end
	game_state.active_object = nil
end


function OnToolFound(tool)
end

function OnToolApply(tool, object)
    ShowToolFailEffect()
end

function OnObjectReachPanel(object)
end

function OnButtonPress(title)
    dialog:SetAllowInput(false)
    
    local move_time = 0.1
    
    DefaultOnButtonPress(title)
    
    local arrow = dialog:GetObject('strelka')
	
	local af = CreateAnimationEffector()	
	af:AttachTo(arrow)
	af:SetMode(_ANIM_FWD + _ANIM_NOLOOP)
	af:SetKey(_TRACK_A, 0, arrow:GetAngle());
    
    DefaultOnButtonPress(title)
    
    local OnMoveFinish = function()    
        if game_state.exit_button ~= nil then
            game_state.exit_button:Stop()
        end
        if game_state.resume_button ~= nil then
            game_state.resume_button:Stop()
        end
        if title == "No" then
            if dialog.previous_screen then
                dialog.previous_screen:Go()
            end
            dialog:ScreenDestroy()
            dialog:GetObject('strelka'):SetAngle(M_PI_OVER_2 / 2);
            dialog.destroying = true
            if dialog.previous_screen then
                game_state = dialog.previous_screen.game_state
                dialog = previous_screen
                previous_screen = dialog.previous_screen
            end		
        end
        if title == "Yes" then
            if dialog.previous_screen then
                dialog.previous_screen:Go()
            end
            local text = game_state.players_name:GetText()		
            dialog:GetObject('strelka'):SetAngle(-M_PI_OVER_2 / 2);
            dialog.destroying = true
            dialog:ScreenDestroy()
            local new_profile_name = text:sub(1, text:len() - 1)
			if new_profile_name:len() ~= 0 then
			    local adding_result = GetProfileManager():AddProfile(new_profile_name)
				if adding_result == SUCCESS then	        
					GetProfileManager():SetCurrentProfile(new_profile_name)
				else
				    if adding_result == ERROR_MAX_PLAYERS_REACHED then
					    load_screen('data/Lua/Screens/DialogErrorMaxProfiles.lua')
					else
					    if adding_result == ERROR_PROFILE_EXISTS then
					        load_screen('data/Lua/Screens/DialogErrorProfileExists.lua')
					    end
					end
				end
			end	
        end
    end
    
    if title == "Yes" then
		af:SetKey(_TRACK_A, move_time, -45 / 180 * M_PI);
	end
	if title == "No" then
	    af:SetKey(_TRACK_A, move_time, 45 / 180 * M_PI);		
	end	
    
    af:Play()
	TimerDo(dialog:GetTimer(), move_time, OnMoveFinish, title)
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function CanBeHinted(obj)
    if locked_object[obj] ~= true then
        return true
    else
        return false
    end
end

-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function LoadData(screen)
	local anim = CreateAnimation('data/Texture/GUI/Options/front.png',0,0,597,186,1,0)
	anim:SetHotSpot(298,93)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('front',anim,524,472,136,0,1,1)	
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('pixel',anim,592,327,137,0,5,5)
	local anim = CreateAnimation('data/Texture/Scenes/Common/pixel.jpg',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x88FFFFFF')
	screen:CreateObject('bg',anim,512,384,88,0,128,96)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,0,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigup',anim,870,113,140,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,70,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigover',anim,864,159,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/buttonbig.png',0,140,166,70,1,0)
	anim:SetHotSpot(83,35)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonbigdown',anim,858,221,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,0,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftup',anim,522,48,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,72,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftover',anim,530,88,101,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/yes.png',0,144,88,72,1,0)
	anim:SetHotSpot(44,36)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonleftdown',anim,529,139,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,0,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightup',anim,866,51,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,68,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightover',anim,860,109,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/no.png',0,136,86,68,1,0)
	anim:SetHotSpot(43,34)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('buttonrightdown',anim,856,167,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Dialog/BG.png',0,0,584,379,1,0)
	anim:SetHotSpot(292,189)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG',anim,523,361,100,0,1,1)
	local anim = CreateAnimation('data/Texture/GUI/Options/strelka.png',0,0,62,153,1,0)
	anim:SetHotSpot(26,116)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('strelka',anim,521,466,134,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
