local zoom_safe;

function DoAfterStartDialog()
    AddNoteToNotebook('POLICE_STATION');
    
    RegisterMonologObject('area_map',    'C_05_MAP_HINT');
    RegisterMonologObject('area_chair', {'C_05_CHAIR_HINT_1', 'C_05_CHAIR_HINT_2'});
    RegisterMonologObject('area_graffiti', {'C_05_GRAFFITI_HINT_1', 'C_05_GRAFFITI_HINT_2'});
    
    LinkScene(  'area_table',
                'CursorGlass',
                'data/Scenes/05_Gendarmery_Table_HO.lua',
                true);
    
    RegisterActiveObject('area_door',                       function(obj_name)
                                                                play_field:GetObject('area_door' ):SetVisible(false);
                                                                play_field:GetObject('area_wart' ):SetVisible(true);
                                                                
                                                                play_field:GetObject('door2'):ChangeOpacity(0., 1., OPEN_DOOR_TIME);
                                                                play_field:GetObject('door0'):ChangeOpacity(1., 0., OPEN_DOOR_TIME);
                                                                PlaySound('OpenDoor');
                                                                
                                                                LinkScene(  'area_wart',
                                                                            'CursorLeft',
                                                                            'data/Scenes/05_Gandarmery_Wart.lua',
                                                                            false);
                                                            end,
                                                             
                                                            function(obj_name)
                                                                play_field:GetObject('area_door' ):SetVisible(false);
                                                                play_field:GetObject('area_wart' ):SetVisible(true);
                                                                
                                                                play_field:GetObject('door2'):SetOpacity(1.);
                                                                play_field:GetObject('door0'):SetOpacity(0.);
                                                                
                                                                LinkScene(  'area_wart',
                                                                            'CursorUpLeft',
                                                                            'data/Scenes/05_Gandarmery_Wart.lua',
                                                                            false);
                                                            end);
                         
    RegisterActiveObject('area_safe_down',                  function(obj_name)
                                                                play_field:GetObject(obj_name):SetVisible(false);
                                                                play_field:GetObject('area_matchbox' ):SetVisible(true);
                                                                play_field:GetObject('safe_door_down'):ChangeOpacity(0, 1, 0.5);
                                                                play_field:GetObject('matchbox'      ):ChangeOpacity(0, 1, 0.5);
                                                                
                                                                PlaySound('05_OpenSecondSafe');
                                                            end,
                                                            function(obj_name)
                                                                play_field:GetObject(obj_name):SetVisible(false);
                                                                play_field:GetObject('area_matchbox' ):SetVisible(true);
                                                                play_field:GetObject('safe_door_down'):SetOpacity(1);
                                                                play_field:GetObject('matchbox'      ):SetOpacity(1);
                                                            end);
    
    RegisterToolFindInfo('05_matchbox', 'area_matchbox',    function(tool_name)
                                                                play_field:GetObject('area_matchbox'):SetVisible(false);
                                                                play_field:GetObject('matchbox'     ):SetVisible(false);
                                                            end,
                                                            function(tool_name)
                                                                play_field:GetObject('area_matchbox'):SetVisible(false);
                                                                play_field:GetObject('matchbox'     ):SetVisible(false);
                                                            end);
end

function RegisterValveUse()
    zoom_safe.SetObjectVisible('area_use_valve', true);
    
    RegisterActiveObject('area_use_valve',
                         function(obj_name)
                            local knob  = play_field:GetObject('safe_knob');
                            local eff   = CreateAnimationEffector();
                            local time  = 2;
                            local angle = M_TWO_PI * 2;
                            local curAngle = knob:GetAngle();
                            
                            eff:AttachTo(knob);
                            eff:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
                            
                            eff:SetKey(_TRACK_A, 0.,        curAngle);
                            eff:SetKey(_TRACK_A, time*0.1,  curAngle + angle*0.05);
                            eff:SetKey(_TRACK_A, time*0.2,  curAngle + angle*0.15);
                            eff:SetKey(_TRACK_A, time*0.8,  curAngle + angle*0.85);
                            eff:SetKey(_TRACK_A, time*0.9,  curAngle + angle*0.95);
                            eff:SetKey(_TRACK_A, time,      curAngle + angle);
                            eff:Play();
                            
                            PlaySound('05_OpenSafe');
                            
                            TimerDoString('OnKnobRotated()', time, play_field:GetTimer());
                         end,
                         
                         function(obj_name)
                            zoom_safe.Disable();
                            
                            play_field:GetObject('safe_door'):SetOpacity(1.);
                            play_field:GetObject('mg_part'  ):SetOpacity(1.);
                            play_field:GetObject('area_safe'):SetVisible(false);
                            
                            RegisterMgPartFound();
                         end);
end

function OnKnobRotated()
    play_field:GetObject('area_safe'):SetVisible(false);
    
    if zoom_safe.IsVisible() then
        zoom_safe.SetVisible(false);
    end
    
    game_state.need_open_safe = true;
end

function RegisterMgPartFound()
    play_field:GetObject('area_mg_part'):SetVisible(true);
    
    RegisterToolFindInfo('05_lock_part', 'area_mg_part',    function(tool_name)
                                                                play_field:GetObject('area_mg_part'):SetVisible(false);
                                                                
                                                                local mg_part = play_field:GetObject('mg_part');
                                                                mg_part:ChangeOpacity(mg_part:GetOpacity(), 0, 0.3)
                                                                
                                                                AddNoteToNotebook('POLICE_STATION_2');
                                                            end,
                                                            function(tool_name)
                                                                play_field:GetObject('area_mg_part'):SetVisible(false);
                                                                play_field:GetObject('mg_part'):SetOpacity(0);
                                                            end);
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/05_Gendarmery_Main.lua', 'quest')
    
    play_field:GetObject('safe_knob_small'):SetOpacity(0.);
    play_field:GetObject('safe_door'      ):SetOpacity(0.);
    play_field:GetObject('door2'          ):SetOpacity(0.);
    play_field:GetObject('mg_part'        ):SetOpacity(0.);
    play_field:GetObject('safe_door_down' ):SetOpacity(0.);
    play_field:GetObject('matchbox'       ):SetOpacity(0.);
    play_field:GetObject('area_wart'      ):SetVisible(false);
    play_field:GetObject('area_mg_part'   ):SetVisible(false);
    play_field:GetObject('area_matchbox'  ):SetVisible(false);
    
    zoom_safe = CreateZoomZone( 'area_safe',
                                'CloseZoneSafe',
                                'ZOOM_safe',        true,
                                'safe_knob',        false,
                                'area_place_valve', true,
                                'area_use_valve',   false);
                                
    play_field:GetObject('area_place_valve'):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('area_use_valve'  ):GetAnimation():SetColor('0x01FFFFFF');
    
    RegisterToolApplyInfo('05_safe_valve', 'area_place_valve',  nil,
                                                                nil,
                                                                function(tool_name)
                                                                    zoom_safe.SetObjectVisible('area_place_valve', false);
                                                                    zoom_safe.SetObjectVisible('safe_knob', true);
                                                                    
                                                                    PlaySound('05_SafeValvePlaced')
                                                                    
                                                                    play_field:GetObject('safe_knob_small'):ChangeOpacity(0., 1., 0.5);
                                                                    RegisterValveUse();
                                                                end,
                                                                
                                                                function(tool_name)
                                                                    zoom_safe.SetObjectVisible('area_place_valve', false);
                                                                    zoom_safe.SetObjectVisible('safe_knob', true);
                                                                    
                                                                    play_field:GetObject('safe_knob_small'):SetOpacity(1.);
                                                                    RegisterValveUse();
                                                                end,
                                                                function()
                                                                    ShowMonolog('C_05_SAFE_HINT', 'C_05_SAFE_HINT');
                                                                end);
    
    local on_enter_comics = CreateComics('05_OnEnter',
                                CreateComicsFrame('Gendarme',   'C_05_DIALOG1_1_GANDARM',   'Gendarme'),
                                CreateComicsFrame('Dupen',      'C_05_DIALOG1_2_DUPEN',     'body', 'head2'),
                                CreateComicsFrame('Gendarme',   'C_05_DIALOG1_3_GANDARM',   'Gendarme'),
                                CreateComicsFrame('Gendarme',   'C_05_DIALOG1_3_GANDARM_A', 'Gendarme'),
                                CreateComicsFrame('Boy',        'C_05_DIALOG1_4_BOY',       'Boy', 'Boy_tear1'),
                                CreateComicsFrame('Gendarme',   'C_05_DIALOG1_5_GANDARM',   'Gendarme'),
                                CreateComicsFrame('Boy',        'C_05_DIALOG1_6_BOY',       'Boy', 'Boy_tear2'),
                                CreateComicsFrame('Dupen',      'C_05_DIALOG1_7_DUPEN',     'body', 'head2'));
    if on_enter_comics then
        play_field:GetObject('area_safe'):SetVisible(false);
        
        on_enter_comics.on_finish = function()
            DoAfterStartDialog();
            play_field:GetObject('area_safe'):SetVisible(true);
        end
        
        on_enter_comics.SetVisible( true );
    else
        DoAfterStartDialog();
    end
end

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

function OnUpdate(dt)

    if game_state.need_open_safe and (game_state.locker:GetNReferences() == 0) then
        zoom_safe.Disable();
        play_field:GetObject('safe_door'   ):ChangeOpacity(0., 1., 1);
        play_field:GetObject('mg_part'     ):ChangeOpacity(0., 1., 1);
        
        TimerDoString('RegisterMgPartFound()', 1, play_field:GetTimer());
        
        game_state.need_open_safe = false;
    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

    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_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/Characters/Gendarme/Gendarme.png',0,0,440,742,1,0)
	anim:SetHotSpot(220,371)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Gendarme',anim,772,399,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/door2.png',0,0,109,194,1,0)
	anim:SetHotSpot(54,97)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('door2',anim,559,276,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,275,508,110,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,111,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_table',anim,543,381,105,-12,40,12)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Zoom_safe/ZOOM_safe.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_safe',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Zoom_safe/safe_knob.png',0,0,118,118,1,0)
	anim:SetHotSpot(59,59)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('safe_knob',anim,495,370,151,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_place_valve',anim,501,378,152,0,17,17)
	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_use_valve',anim,500,378,153,0,17,17)
	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_safe',anim,152,356,105,0,15,15)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/safe_knob_small.png',0,0,40,40,1,0)
	anim:SetHotSpot(20,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('safe_knob_small',anim,162,346,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/safe_door.png',0,0,123,151,1,0)
	anim:SetHotSpot(61,75)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('safe_door',anim,158,359,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/door0.png',0,0,84,172,1,0)
	anim:SetHotSpot(42,86)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('door0',anim,542,271,103,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_door',anim,547,266,104,0,9,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_wart',anim,547,266,104,0,9,20)
	local anim = CreateAnimation('data/Texture/Characters/Boy/Boy.png',0,0,355,624,1,0)
	anim:SetHotSpot(177,312)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Boy',anim,768,456,111,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/mg_part.png',0,0,112,112,1,0)
	anim:SetHotSpot(56,56)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mg_part',anim,132,333,103,0,0.4,0.4)
	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_mg_part',anim,134,333,104,0,6,6)
	local anim = CreateAnimation('data/Texture/Characters/Boy/Boy_tear1.png',0,0,86,99,40,5)
	anim:SetHotSpot(43,49)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Boy_tear1',anim,729,363,112,0,1,1)
	local anim = CreateAnimation('data/Texture/Characters/Boy/Boy_tear2.png',0,0,83,93,40,5)
	anim:SetHotSpot(41,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Boy_tear2',anim,726,366,113,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_map',anim,305,194,101,5,40,22)
	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_chair',anim,835,464,100,0,20,20)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/safe_door_down.png',0,0,103,139,1,0)
	anim:SetHotSpot(51,69)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('safe_door_down',anim,159,500,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_main/Layers/matchbox.png',0,0,40,21,1,0)
	anim:SetHotSpot(20,10)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('matchbox',anim,140,488,102,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_safe_down',anim,157,487,104,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_matchbox',anim,140,482,103,0,7,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_graffiti',anim,862,242,101,0,30,20)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
