local zoom_clock;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/17_Bedroom_Main.lua', 'quest', 'data/Scenes/14_Corridor_Main.lua')
    
    AddNoteToNotebook('MADAME_ROOM')
    
    zoom_clock = CreateZoomZone('area_clock',
                                'CloseZoneClock',
                                'ZOOM_clock',   true);
                                
    LinkScene(  'area_ho_fireplace',
                'CursorGlass',
                'data/Scenes/17_Bedroom_Fireplace_HO.lua',
                true);
    
    LinkScene(  'area_ho_mirror',
                'CursorGlass',
                'data/Scenes/17_Bedroom_Mirror_HO.lua',
                true);
    
    LinkScene(  'area_ho_under_the_bed',
                'CursorGlass',
                'data/Scenes/17_Bedroom_UnderTheBed_HO.lua',
                true);
    
    LinkScene(  'area_ho_above_the_bed',
                'CursorGlass',
                'data/Scenes/17_Bedroom_AboveTheBed_HO.lua',
                true);

    LinkScene(  'area_microscope',
                'CursorGlass',
                'data/Scenes/17_Bedroom_Microscope.lua',
                false);
    
    game_state.minigame_completed = HasGameState('MiniGameCompleted', '17_Bedroom_Microscope');
    game_state.flock_found        = HasGameState('ToolFound', '13_flock');
    
    game_state.all_tool_founded   = HasGameState('ToolFound', '17_glass')
                                    and HasGameState('ToolFound', '17_microscope')
                                    and HasGameState('ToolFound', '17_pipette')
                                    and HasGameState('ToolFound', '17_seashell')
    
    if game_state.all_tool_founded then
        play_field:GetObject('area_microscope'):SetVisible(false);
    end
    
    if game_state.minigame_completed then 
        local comics = nil;
        
        if game_state.flock_found then
            comics = CreateComics('17_OnFinish', CreateComicsFrame('Dupen', 'C_17_FINAL_DUPEN',  'body', 'head1'));
        else
            comics = CreateComics('17_OnFinish', CreateComicsFrame('Dupen', 'C_17_FINAL2_DUPEN', 'body', 'head1'));
        end
        
        if comics then
            comics.on_finish = function()
                if game_state.flock_found then
                    game_state.parent_scene = nil;
                end
                
                play_field:TimerDoString("On17FinalComicsCompleted();", 0.5);
            end
            comics.SetVisible(true);
        else
            if game_state.flock_found then
                game_state.parent_scene = nil;
            end
            
            play_field:TimerDoString("On17FinalComicsCompleted();", 1);
        end
    end
    
    UnRegisterQuestObjects('area_clock');
end

function On17FinalComicsCompleted()
    AddGameState('ActionCompleted', '17_BedroomQuestCompleted');
    
    if game_state.flock_found then
        CompleteLevel('Atelier');
    end
end

function OnSceneFree(screen)
    DefaultOnSceneFree()
    FreeData(screen)
end

function OnUpdate(dt)
    if game_state.all_tool_founded
        and game_state.locker:GetNReferences() == 0
        and game_state.fly_tools:GetNReferences() == 0
        and not game_state.all_tools_found_comics_shown
    then
        local comics = CreateComics('17_AllHOCompleted',
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_1',  'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_2',  'body', 'head2'),
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_3',  'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_4',  'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_5',  'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_17_ALL_HO_COMPLETED_DUPEN_6',  'body', 'head2'));
        if comics then
            comics.on_finish = function()
                play_field:GetObject('area_microscope'):SetVisible(true);
                
                if HasGameState('SceneVisited', 'data/Scenes/17_Bedroom_Microscope.lua')
                then
                    RemoveGameState('SceneVisited', 'data/Scenes/17_Bedroom_Microscope.lua')
                    game_state.ignore_on_highlight['area_microscope'] = nil;
                end
                
                AddNoteToNotebook('MADAME_ROOM_ALL_FOUND');
            end
            comics.SetVisible(true);
        else
            play_field:GetObject('area_microscope'):SetVisible(true);
            AddNoteToNotebook('MADAME_ROOM_ALL_FOUND');
        end
        
        game_state.all_tools_found_comics_shown = true;
    end
    DefaultOnUpdate(dt)
end

function OnAllItemsFound()
    DefaultOnAllItemsFound()
end

function OnLose()
    DefaultOnLose()
end

function OnExit()
    DefaultOnExit()
end

function OnClose()
    DefaultOnClose()
end

function OnObjectMouseEnter(object)
    DefaultOnObjectMouseEnter(object)
end

function OnObjectMouseLeave(object)
    DefaultOnObjectMouseLeave(object)
end

function OnObjectMouseDown(object)
    local is_wrong_click = not DefaultOnObjectMouseDown(object)
    local obj_name = object:GetName()
    local pf = play_field
    
    if obj_name == 'ZOOM_clock' then
        ShowMonolog('C_17_HINT_CLOCK_ZOOM', 'C_17_HINT_CLOCK_ZOOM');
    end
        
    DefaultOnObjectMouseDownProcessed(is_wrong_click);
end

function OnObjectMouseUp(object)
    DefaultOnObjectMouseUp(object)
end

function OnObjectFound(object)
    DefaultOnObjectFound(object)
end

function OnToolFound(tool)
    DefaultOnToolFound(tool)
end

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

function OnObjectReachPanel(object)
    DefaultOnObjectReachPanel(object)
end

function OnButtonPress(title)
    DefaultOnButtonPress(title)
end

function OnMouseMove(previous_pos, current_pos)
    DefaultOnMouseMove(previous_pos, current_pos)
end

function OnSetTool(tool)
    DefaultOnSetTool(tool)
end

function LevelLoaded(level)
end

function OnSceneDestroy(playfield)
    DefaultOnSceneDestroy(playfield)
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/17_Bedroom/Zone_main/Layers/background.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('background',anim,512,384,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho_fireplace',anim,155,500,101,0,15,20)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho_mirror',anim,707,380,101,0,15,15)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho_under_the_bed',anim,561,556,101,0,20,7)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho_above_the_bed',anim,522,196,101,0,25,7)
	local anim = CreateAnimation('data/Texture/Scenes/17_Bedroom/Zone_main/Zoom_clock/Layers/ZOOM_clock.png',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ZOOM_clock',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_clock',anim,94,311,100,0,15,10)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_microscope',anim,962,490,101,0,15,12)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/body.png',0,0,376,520,1,0)
	anim:SetHotSpot(188,260)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('body',anim,275,508,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/head1.png',0,0,185,244,1,0)
	anim:SetHotSpot(92,122)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('head1',anim,308,202,121,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Dupen/head2.png',0,0,214,252,1,0)
	anim:SetHotSpot(107,126)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('head2',anim,307,200,122,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
