local zoom_report;
local zoom_vise;

function OnReportReaded()
    local comics_final = CreateComics('05_Final',
                            CreateComicsFrame('Dupen', 'C_05_FINAL_1_DUPEN',   'body', 'head1'),
                            CreateComicsFrame('Dupen', 'C_05_FINAL_1_DUPEN_A', 'body', 'head1'),
                            CreateComicsFrame('Dupen', 'C_05_FINAL_2_DUPEN',   'body', 'head2'));
    if comics_final then
        comics_final.on_finish = function()
            game_state.parent_scene = nil;
            AddNoteToNotebook('Witness_Gendarme');
            
            CustomToolApply('05_lamp');
            TimerDoString("CompleteLevel('Gandarmery')", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
        end
        comics_final.SetVisible(true)
    else
        game_state.parent_scene = nil;
        AddNoteToNotebook('Witness_Gendarme');
        
        CustomToolApply('05_lamp');
        CompleteLevel('Gandarmery');
    end
end

function OnScrollRetortPlaced()
    game_state.scroll_retort_placed = true;
    
    zoom_vise.SetObjectVisible('area_scroll_retort', false);
    zoom_vise.SetObjectVisible('area_lever', true);
    zoom_vise.SetObjectVisible('skriptum_zakr', true);
    zoom_vise.SetObjectVisible('skriptum_open', true);
    
    play_field:GetObject('sk_close'):SetVisible(true);
    play_field:GetObject('area_scroll_retort'):SetVisible(false);
end

function RegisterFretsawApply()
    RegisterToolApplyInfo('04_fretsaw', 'area_fretsaw', nil, nil,
                function(tool_name)
                    local time = 0.5;
                    
                    play_field:GetObject('skriptum_zakr'):ChangeOpacity(1., 0., time);
                    TimerDoString('OnSkriptumOpened()', time, play_field:GetTimer());
                    
                    play_field.sawSound = MakeSoundEntity('05_SawUse');
                    play_field.sawSound:Play(true);
                end,
                function(tool_name)
                    zoom_vise.Disable();
                    
                    play_field:GetObject('report_small'):SetXY(403, 564);
                    play_field:GetObject('report_small'):SetAngle(0.0174532925);
                    play_field:GetObject('report_small'):SetVisible(true);
                    play_field:GetObject('area_report2'):SetVisible(true);
                    play_field:GetObject('sk'      ):SetVisible(true);
                    play_field:GetObject('sk_close'):SetVisible(false);
                end)
end

function OnSkriptumOpened()
    if play_field.sawSound
    then
        play_field.sawSound:StopSound();
        play_field.sawSound = nil;
    end
    
    play_field:GetObject('sk'      ):SetVisible(true);
    play_field:GetObject('sk_close'):SetVisible(false);
    
    zoom_vise.Disable();
    
    if zoom_vise.IsVisible() then
        zoom_vise.SetVisible();
    end
    
    TimerDoString('RunLetterFallAnimation()', ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
end

function RunLetterFallAnimation()
    local report   = play_field:GetObject('report_small');
    local effector = CreateAnimationEffector();
    
    effector:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    effector:AttachTo( report );
    
    effector:SetKey(_TRACK_X, 0, 329);
    effector:SetKey(_TRACK_Y, 0, 355);
    effector:SetKey(_TRACK_X, 0.25, 368.199995503662);
    effector:SetKey(_TRACK_Y, 0.25, 375.800001754669);
    effector:SetKey(_TRACK_X, 0.41666666666667, 410);
    effector:SetKey(_TRACK_Y, 0.41666666666667, 363);
    effector:SetKey(_TRACK_X, 0.75, 372.388522036039);
    effector:SetKey(_TRACK_Y, 0.75, 453.749116981506);
    effector:SetKey(_TRACK_X, 0.91666666666667, 331);
    effector:SetKey(_TRACK_Y, 0.91666666666667, 456);
    effector:SetKey(_TRACK_X, 1.16666666666667, 353.587826406835);
    effector:SetKey(_TRACK_Y, 1.16666666666667, 514.139642746581);
    effector:SetKey(_TRACK_X, 1.41666666666667, 403);
    effector:SetKey(_TRACK_Y, 1.41666666666667, 564);
    effector:SetKey(_TRACK_SX, 0, 0.29);
    effector:SetKey(_TRACK_SY, 0, 1);
    effector:SetKey(_TRACK_SX, 0.41666666666667, 1);
    effector:SetKey(_TRACK_SY, 0.41666666666667, 1);
    effector:SetKey(_TRACK_SX, 0.75, 0.99);
    effector:SetKey(_TRACK_SY, 0.75, 0.46);
    effector:SetKey(_TRACK_SX, 0.91666666666667, 0.61);
    effector:SetKey(_TRACK_SY, 0.91666666666667, 1);
    effector:SetKey(_TRACK_SX, 1.16666666666667, 0.78);
    effector:SetKey(_TRACK_SY, 1.16666666666667, 1.46);
    effector:SetKey(_TRACK_SX, 1.41666666666667, 1);
    effector:SetKey(_TRACK_SY, 1.41666666666667, 1);
    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 0.41666666666667, -0.7853981625);
    effector:SetKey(_TRACK_A, 0.75, 0.0174532925);
    
    effector:Play();
    
    report:SetVisible(true);
    
    PlaySound('05_PaperFly');
    TimerDoString('OnLetterFalled()', 1.41666666666667, play_field:GetTimer());
end

function OnLetterFalled()
    play_field:GetObject('area_report2'):SetVisible(true);
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/05_Gandarmery_Wart_Cell3.lua', 'quest', 'data/Scenes/05_Gandarmery_Wart.lua')
    
    play_field:GetObject('body' ):SetOpacity(0.);
    play_field:GetObject('head1'):SetOpacity(0.);
    play_field:GetObject('head2'):SetOpacity(0.);
    play_field:GetObject('sk'          ):SetVisible(false);
    play_field:GetObject('sk_close'    ):SetVisible(false);
    play_field:GetObject('report_small'):SetVisible(false);
    play_field:GetObject('area_report2'):SetVisible(false);
    
    RegisterMonologObject('area_chair',  'C_05_CELL3_CHAIR_HINT')
    RegisterMonologObject('area_window', 'C_05_CELL3_WINDOW_HINT')
    RegisterMonologObject('area_bucket', 'C_05_CELL3_BUCKET_HINT')
    
    zoom_report = CreateZoomZone(  'area_report2',
                                   'CloseZoneReport2',
                                   'Police_report_2', true);
    zoom_report.on_open = function()
--        ShowMonolog('C_05_REPORT2_HINT', 'C_05_REPORT2_HINT');
        AddNoteToNotebook('JAIL_CELLS_3')
    end
    
    zoom_report.on_close = function()
        zoom_report.Disable();
        game_state.parent_scene = nil;
        
        if not HasGameState('ActionCompleted', game_state.scene_path..'(reportReaded)') then
            AddGameState('ActionCompleted', game_state.scene_path..'(reportReaded)')
            
            TimerDoString('OnReportReaded()', ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
        end
    end
    
    if HasGameState('ActionCompleted', game_state.scene_path..'(reportReaded)') then
        OnReportReaded()
    end
    
    zoom_vise = CreateZoomZone( 'area_vise',
                                'CloseZoneVise',
                                'ZOOM_vise',          true,
                                'skriptum_zakr',      false,
                                'skriptum_open',      false,
                                'layer_over',         true,
                                'layer_move',         true,
                                'lever_anim',         true,
                                'area_lever',         false,
                                'area_fretsaw',       false,
                                'area_scroll_retort', true);
                                
    zoom_vise.on_open = function()
    --[[
        if not game_state.scroll_retort_placed then
            ShowMonolog('C_05_CELL3_VISE_HINT', 'C_05_CELL3_VISE_HINT');
        elseif not game_state.lever_used then
            ShowMonolog('C_05_CELL3_HOLD_HINT', 'C_05_CELL3_HOLD_HINT');
        else
            ShowMonolog('C_05_CELL3_CUT_HINT', 'C_05_CELL3_CUT_HINT');
        end
    --]]
    end
    
    play_field:GetObject('area_lever'        ):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('area_fretsaw'      ):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('area_scroll_retort'):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('lever_anim'        ):GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    play_field:GetObject('lever_anim'        ):GetAnimation():Stop();
    
    RegisterToolApplyInfo('05_scroll_retort', 'area_scroll_retort', nil, nil,   function(tool_name)
                                                                                    OnScrollRetortPlaced();
                                                                                    
                                                                                    PlaySound('05_PlaceScrollRetort');
                                                                                    
--                                                                                  ShowMonolog('C_05_CELL3_HOLD_HINT', 'C_05_CELL3_HOLD_HINT');
                                                                                end,
                                                                                function(tool_name)
                                                                                    OnScrollRetortPlaced()
                                                                                end);
    
    RegisterActiveObject('area_lever',  function(obj_name)
                                            zoom_vise.SetObjectVisible('area_lever', false);
                                            zoom_vise.SetObjectVisible('area_fretsaw', true);
                                            
                                            local lever_anim = play_field:GetObject('lever_anim'):GetAnimation();
                                            local lm         = play_field:GetObject('layer_move');
                                            local time       = GetAnimationLength( lever_anim );
                                            
                                            lever_anim:Play();
                                            lm:Move(637 - lm:GetX(), 297 - lm:GetY(), time);
                                            
                                            RegisterFretsawApply();
                                            PlaySound('05_UseVise');
                                            
  --                                          ShowMonolog('C_05_CELL3_CUT_HINT', 'C_05_CELL3_CUT_HINT');
                                            game_state.lever_used = true;
                                        end,
                                        function(obj_name)
                                            zoom_vise.SetObjectVisible('area_lever', false);
                                            zoom_vise.SetObjectVisible('area_fretsaw', true);
                                            
                                            local lever_anim = play_field:GetObject('lever_anim'):GetAnimation();
                                            lever_anim:SetFrame(lever_anim:GetFrames() - 1);
                                            play_field:GetObject('layer_move'):SetXY(637, 297);
                                            
                                            game_state.lever_used = true;
                                            
                                            RegisterFretsawApply();
                                        end)
    
    SwayObjectByAngle('cepi_01', 2., 4.);
    SwayObjectByAngle('cepi_02', 1., 3.);
end

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

function OnUpdate(dt)
    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 == 'Police_report_2' then
        ShowMonolog('C_05_REPORT2_HINT', 'C_05_REPORT2_HINT');
    end
    
    if not game_state.scroll_retort_placed then
        if (obj_name == 'ZOOM_vise'
        or obj_name == 'area_scroll_retort'
        or obj_name == 'layer_over'
        or obj_name == 'layer_move'
        or obj_name == 'lever_anim')
        then
            ShowMonolog('C_05_CELL3_VISE_HINT', 'C_05_CELL3_VISE_HINT');
        end
    elseif not game_state.lever_used then
        if (obj_name == 'ZOOM_vise'
        or obj_name == 'layer_move')
        then
            ShowMonolog('C_05_CELL3_HOLD_HINT', 'C_05_CELL3_HOLD_HINT');
        end
    else
        if (obj_name == 'ZOOM_vise'
        or obj_name == 'layer_move'
        or obj_name == 'lever_anim')
        then
            ShowMonolog('C_05_CELL3_CUT_HINT', 'C_05_CELL3_CUT_HINT');
        end
    end
    
    
    if obj_name == 'area_fretsaw' then
        ShowMonolog('C_05_CELL3_CUT_HINT', 'C_05_CELL3_CUT_HINT');
    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/05_Gendarmery/Zone_cell3/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_report2',anim,410,549,102,0,15,10)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/Police_report_2.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Police_report_2',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/cepi_02.png',0,0,53,202,1,0)
	anim:SetHotSpot(26,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('cepi_02',anim,645,0,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/cepi_01.png',0,0,32,176,1,0)
	anim:SetHotSpot(16,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('cepi_01',anim,603,52,101,0,1,1)
	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,292,508,110,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,324,201,111,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,322,200,112,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_chair',anim,156,386,101,0,1.5,2.5)
	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_window',anim,519,182,101,0,10,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_bucket',anim,894,584,101,0,13,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_vise',anim,302,368,102,0,10,14)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Layers/ZOOM_vise.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_vise',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Layers/skriptum_zakr.png',0,0,46,48,1,0)
	anim:SetHotSpot(23,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('skriptum_zakr',anim,493,212,153,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Layers/skriptum_open.png',0,0,211,54,1,0)
	anim:SetHotSpot(105,27)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('skriptum_open',anim,586,215,152,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Layers/layer_over.png',0,0,94,209,1,0)
	anim:SetHotSpot(47,104)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_over',anim,611,316,154,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Layers/layer_move.png',0,0,210,201,1,0)
	anim:SetHotSpot(105,100)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layer_move',anim,621,284,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Zoom_vise/Animations/lever_anim.png',0,0,201,218,6,12)
	anim:SetHotSpot(100,109)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('lever_anim',anim,616,374,155,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_lever',anim,616,386,156,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_fretsaw',anim,549,216,156,0,1.01,0.7)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_scroll_retort',anim,574,215,157,0,1.5,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/sk.png',0,0,59,25,1,0)
	anim:SetHotSpot(29,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sk',anim,306,331,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/report_small.png',0,0,97,48,1,0)
	anim:SetHotSpot(48,24)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('report_small',anim,418,551,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell3/Layers/sk_close.png',0,0,59,25,1,0)
	anim:SetHotSpot(29,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('sk_close',anim,306,331,101,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
