local PATH_COMICS_26_ON_SHOWN           = 'data/Scenes/Comics/HelpersComicsReplic.lua'

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad(screen:GetName())

    local pf = play_field
    local level = pf:GetLevelScreen()
	---------------------------------------------------------------------------------------------
	game_state.hint_panel = CreateHewPanel(Vector(100, 100), screen)
	---------------------------------------------------------------------------------------------
	pf:GetObject('professor_1'):GetAnimation():SetColor('0xFFFFFFFF')
	pf:GetObject('professor_2'):GetAnimation():SetColor('0xFFFFFFFF')
	pf:GetObject('professor_1'):SetOpacity(0)
	game_state.chain_on_shown = RegisterComicsEx(function()
                                                    local comics = LoadComics(PATH_COMICS_26_ON_SHOWN, 'COMICS_26_ON_SHOWN')
                                                    if comics then
                                                        CreateOneSMS('COMICS_26_ON_SHOWN_1', 1)
                                                        CreateOneReplicProfessor('COMICS_26_ON_SHOWN_2', 2)
                                                        CreateOneReplicHeroes('COMICS_26_ON_SHOWN_3', 3)
                                                        CreateOneReplicProfessor('COMICS_26_ON_SHOWN_4', 4)
                                                        CreateOneReplicHeroes('COMICS_26_ON_SHOWN_5', 5)
                                                        CreateOneReplicProfessor('COMICS_26_ON_SHOWN_6', 6)
                                                     end
                                                 end )
    game_state.chain_on_shown.OnHidden = function()
        pf:GetObject('professor_1'):ChangeOpacity(0, 1, 0.2)
        pf:GetObject('professor_2'):ChangeOpacity(1, 0, 0.2)
    
        local time_move = 3
        local dist = 768
        MoveBackground(0, dist, time_move)
        pf:GetObject('atlanta_boy'):Move(0, dist, time_move)
        pf:GetObject('professor_1'):Move(0, dist, time_move)
        pf:GetObject('professor_2'):Move(0, dist, time_move)
        pf:GetObject('atlanta_layer_top'):Move(0, dist, time_move)
        
        TimerDo(pf:GetTimer(), time_move, function()
                                                local credits_entity = CreateCreditsText()
                                                credits_entity:AttachTo(screen)
                                                credits_entity:SetZ(180)
                                                credits_entity:SetTextPosXY(512, 600)
                                                game_state.credits_entity = credits_entity
                                                
                                                game_state.hint_panel:ChangeOpacity(1, 0, 0.3)
                                          end )
    end
    
    if game_state.chain_on_shown.played then
        pf:GetObject('bg'):Move(0, 768, 0)
        pf:GetObject('bg'):SetScaleX(1)
        pf:GetObject('bg'):SetScaleY(1)
        pf:GetObject('atlanta_boy'):SetOpacity(0)
        pf:GetObject('professor_1'):SetOpacity(0)
        pf:GetObject('professor_2'):SetOpacity(0)
        pf:GetObject('atlanta_layer_top'):SetOpacity(0)
        
        local credits_entity = CreateCreditsText()
        credits_entity:AttachTo(screen)
        credits_entity:SetZ(180)
        credits_entity:SetTextPosXY(512, 600)
        game_state.credits_entity = credits_entity
        
        game_state.hint_panel:SetOpacity(0)
    end
	---------------------------------------------------------------------------------------------
	level:GetPanel():Hide()
	level:GetPanel():Stop()
	level:GetPhone():Hide()
	level:GetPhone():Stop()
end

function MoveBackground(dx, dy, time)
    local obj = play_field:GetObject('bg')
    local time_scale = time * 0.7

    local af = CreateAnimationEffector()
    af:AttachTo( obj )
    af:SetKey(_TRACK_X, 0.0, obj:GetX())
    af:SetKey(_TRACK_Y, 0.0, obj:GetY())
    af:SetKey(_TRACK_X, time, obj:GetX() + dx)
    af:SetKey(_TRACK_Y, time, obj:GetY() + dy)
    af:SetKey(_TRACK_SX, 0.0, obj:GetScaleX())
    af:SetKey(_TRACK_SY, 0.0, obj:GetScaleY())
    af:SetKey(_TRACK_SX, time_scale - 0.01, obj:GetScaleX())
    af:SetKey(_TRACK_SY, time_scale - 0.01, obj:GetScaleY())
    af:SetKey(_TRACK_SX, time, 1)
    af:SetKey(_TRACK_SY, time, 1)
    af:Play()
end

function CreateHewPanel(pos, screen)
    -- Hint background
    local back = CreateAnimation('data/Texture/GUI/Panel/panel_mg.png',0,0,218,73,1,0)
    back:SetHotSpot(109,36)
	screen:CreateObject('hint_back', back, 107, 733, 450, 0, 1, 1)
	
	local hint_panel = screen:GetObject('hint_back')
	hint_panel:AttachTo(screen)
	-- Menu button load
	local mb_up = CreateAnimation('data/Texture/GUI/Panel/menu_bttn_panel.png',0,0,88,40,1,0)
	mb_up:SetHotSpot(44,20)
	local mb_over = CreateAnimation('data/Texture/GUI/Panel/menu_bttn_panel.png',0,40,88,40,1,0)
	mb_over:SetHotSpot(44,20)
	local mb_down = CreateAnimation('data/Texture/GUI/Panel/menu_bttn_panel.png',0,80,88,40,1,0)
	mb_down:SetHotSpot(44,20)
	-- Help button load
	local hb_up = CreateAnimation('data/Texture/GUI/Panel/help_bttn_panel.png',0,0,52,47,1,0)
	hb_up:SetHotSpot(26,24)
	local hb_over = CreateAnimation('data/Texture/GUI/Panel/help_bttn_panel.png',0,47,52,47,1,0)
	hb_over:SetHotSpot(26,24)
	local hb_down = CreateAnimation('data/Texture/GUI/Panel/help_bttn_panel.png',0,94,52,47,1,0)
	hb_down:SetHotSpot(26,24)
	-- Create buttons and attach to parent
	local menu_button = CreateButton(mb_up, mb_over, mb_down, 'ExitPanel', screen:GetLevelScreen())
	menu_button:SetHotKey(HGEK_ESCAPE)
	menu_button:SetXY(-42, 3)
	menu_button:SetZ(5)
	menu_button:AttachTo(hint_panel)
	hint_panel.menu_bttn = menu_button
	local help_button = CreateButton(hb_up, hb_over, hb_down, 'HelpPanel', screen:GetLevelScreen())
	help_button:SetXY(30, 3)
	help_button:SetZ(5)
	help_button:AttachTo(hint_panel)
	hint_panel.help_bttn = help_button
	
	return hint_panel
end

function OnSceneFree()
    FreeData()
end

function OnScreenShown(fade_out)
    if fade_out then
        game_state.chain_on_shown.Play()
    end
end

function OnHiddenComics()
end

function OnMinigameCompleted(mini_game)
end

function OnUpdate(dt)
    DefaultOnUpdate(dt)
end

function OnLose()
end

function OnExit()
end

function OnButtonMouseEnter(title)
end

function OnButtonMouseLeave(title)
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    if game_state.chain_on_shown.played and game_state.credits_entity then
        OnWinLevel()
        return
    end

    return is_wrong_click
end

function OnObjectMouseUp(object)
end

function OnObjectRightMouseDown(object)
end

function OnObjectRightMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
end

function OnToolApply(tool, object)
    DefaultOnToolApply(tool, object)
end

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnPhotographing()
	DefaultOnPhotographing()
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
    
    if title == 'ExitPanel' then
        CreateInGameMenu()
        
        return
    end
    
    if title == 'HelpPanel' then
        panel:SetAllowInput(false)
        
        local screen = load_screen('data/Lua/Screens/DialogHelp.lua')
        screen:SetLayer(LAYER_HELP)
        
        play_field:Stop()
        play_field:GetLevelScreen():Stop()
        return
    end
end

function OnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
end

function OnLostFocus()
end

function OnGainFocus()
end

function OnWin()
--    OnWinLevel()
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/Scenes/26_tractor_/Layers/atlanta_boy.png',0,0,142,94,1,0)
	anim:SetHotSpot(71,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('atlanta_boy',anim,938,339,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/26_tractor_/Layers/professor_1.png',0,0,126,254,1,0)
	anim:SetHotSpot(63,127)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('professor_1',anim,440,194,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/26_tractor_/Layers/professor_2.png',0,0,109,254,1,0)
	anim:SetHotSpot(54,127)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('professor_2',anim,434,199,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/26_tractor_/Layers/bg.jpg',0,0,1549,2046,1,0)
	anim:SetHotSpot(774,1023)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bg',anim,505,83,70,0,0.67,0.67)
	local anim = CreateAnimation('data/Texture/Scenes/26_tractor_/Layers/atlanta_layer_top.png',0,0,264,356,1,0)
	anim:SetHotSpot(132,178)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('atlanta_layer_top',anim,434,203,120,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
