local zoom_report;
local zoom_table;

function CreateCockroach()
    createCompositionComp_1 = function()

        local compositionComp_1   = CreateCompositionSequence();
        local compositionDuration = 0.2;
        compositionComp_1:SetDuration(compositionDuration);
        compositionComp_1:SetCyclic(true);

        -- Layer Layer_32
        local element = CreateCompositionElement("data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/targan.png");
        element:SetName("Layer_32");
        compositionComp_1:AddElement(element);
        element:GetTexture():SetTextureRect(0,0,61,85);
        element:GetTexture():SetSpeed(30);
        element:GetTexture():SetFrames(6);
        element:GetTexture():Play();
        element:SetHotSpot(30.5, 42.5);
        element:SetParentElement(compositionComp_1);
        element:SetXY(512, 384);
        
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetAngle(0);
        element:SetOpacity(1);
    
        effector = element:GetEffector();
        effector:SetLifeTime(0, 0.2);
        
        effector:Play();
        compositionComp_1:Play();
        return compositionComp_1;
    end
    
    createCompositionzoom = function()
        local compositionzoom = CreateCompositionSequence();
        local compositionDuration = 7;
        compositionzoom:SetDuration(compositionDuration);
        compositionzoom:SetCyclic(true);
        compositionzoom:SetHotSpot(320, 240);
    
    
        -- Layer Comp_1
        local element = createCompositionComp_1();
        element:SetName("Comp_1");
        compositionzoom:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionzoom);
        element:SetScaleX(1);
        element:SetScaleY(1);
        element:SetOpacity(1);
            
        effector = element:GetEffector();
        effector:SetLifeTime(0, 2);
    
        effector:SetKey(_TRACK_X, 0, 674);
        effector:SetKey(_TRACK_Y, 0, 200);
        effector:SetKey(_TRACK_X, 0.83333333333333, 482.775800918929);
        effector:SetKey(_TRACK_Y, 0.83333333333333, 216.813352762958);
        effector:SetKey(_TRACK_X, 2.25, 286);
        effector:SetKey(_TRACK_Y, 2.25, -37);
        effector:SetKey(_TRACK_A, 0.25, -1.9722220525);
        effector:SetKey(_TRACK_A, 0.83333333333333, -1.0297442575);
        effector:SetKey(_TRACK_A, 1.91666666666667, -0.383972435);
    
        effector:Play();
    
    
        -- Layer Comp_2
        local element = createCompositionComp_1();
        element:SetName("Comp_2");
        compositionzoom:AddElement(element);
        element:SetHotSpot(512, 384);
        element:SetParentElement(compositionzoom);
        element:SetOpacity(1);
            
        effector = element:GetEffector();
        effector:SetLifeTime(2, 5);
    
        effector:SetKey(_TRACK_X, 2.08333333333333, 155);
        effector:SetKey(_TRACK_Y, 2.08333333333333, 507);
        effector:SetKey(_TRACK_X, 2.25, 172.99778346256);
        effector:SetKey(_TRACK_Y, 2.25, 449.594075799933);
        effector:SetKey(_TRACK_X, 2.91666666666667, 93);
        effector:SetKey(_TRACK_Y, 2.91666666666667, 356);
        effector:SetKey(_TRACK_X, 3.08333333333333, 110);
        effector:SetKey(_TRACK_Y, 3.08333333333333, 331);
        effector:SetKey(_TRACK_X, 3.75, 333.479103690899);
        effector:SetKey(_TRACK_Y, 3.75, 188.592985668377);
        effector:SetKey(_TRACK_X, 4.33333333333333, 548.649633541407);
        effector:SetKey(_TRACK_Y, 4.33333333333333, 213.36269279505);
        effector:SetKey(_TRACK_X, 4.66666666666667, 674);
        effector:SetKey(_TRACK_Y, 4.66666666666667, 216);
        effector:SetKey(_TRACK_SX, 2.08333333333333, 0.58);
        effector:SetKey(_TRACK_SY, 2.08333333333333, 0.58);
        effector:SetKey(_TRACK_SX, 2.91666666666667, 1);
        effector:SetKey(_TRACK_SY, 2.91666666666667, 1);
        effector:SetKey(_TRACK_A, 2.08333333333333, 0);
        effector:SetKey(_TRACK_A, 2.25, 0.2617993875);
        effector:SetKey(_TRACK_A, 2.33333333333333, -0.523598775);
        effector:SetKey(_TRACK_A, 2.83333333333333, -0.41887902);
        effector:SetKey(_TRACK_A, 3.08333333333333, 0.55850536);
        effector:SetKey(_TRACK_A, 3.75, 1.56069178723684);
        effector:SetKey(_TRACK_A, 4.66666666666667, 1.9024088825);
    
        effector:Play();
    
        compositionzoom:Play();
    
        return compositionzoom;
    end
    
    return createCompositionzoom();
end

function RunBreadOpenAnimation()
    local effector = CreateAnimationEffector();
    effector:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    effector:AttachTo( play_field:GetObject('br02') )
    effector:SetKey(_TRACK_X, 0, 692);
    effector:SetKey(_TRACK_Y, 0, 441);
    effector:SetKey(_TRACK_X, 0.33333333333333, 805);
    effector:SetKey(_TRACK_Y, 0.33333333333333, 486);
    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 0.33333333333333, 0.2617993875);
    effector:Play();
    
    effector = CreateAnimationEffector();
    effector:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    effector:AttachTo( play_field:GetObject('layers') )
    effector:SetKey(_TRACK_X, 0, 541);
    effector:SetKey(_TRACK_Y, 0, 437);
    effector:SetKey(_TRACK_X, 0.33333333333333, 469);
    effector:SetKey(_TRACK_Y, 0.33333333333333, 441);
    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 0.33333333333333, -0.06981317);
    effector:Play();
    
    effector = CreateAnimationEffector();
    effector:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    effector:AttachTo( play_field:GetObject('skriptum') )
    effector:SetKey(_TRACK_X, 0, 645);
    effector:SetKey(_TRACK_Y, 0, 434);
    effector:SetKey(_TRACK_X, 0.33333333333333, 584);
    effector:SetKey(_TRACK_Y, 0.33333333333333, 438);
    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 0.41666666666667, 0.174532925);
    effector:Play();
    
    effector = CreateAnimationEffector();
    effector:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    effector:AttachTo( play_field:GetObject('br01') )
    effector:SetKey(_TRACK_X, 0, 526);
    effector:SetKey(_TRACK_Y, 0, 424);
    effector:SetKey(_TRACK_X, 0.33333333333333, 454);
    effector:SetKey(_TRACK_Y, 0.33333333333333, 428);
    effector:SetKey(_TRACK_A, 0, 0);
    effector:SetKey(_TRACK_A, 0.33333333333333, -0.06981317);
    effector:Play();
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/05_Gandarmery_Wart_Cell2.lua', 'quest', 'data/Scenes/05_Gandarmery_Wart.lua')
    
    play_field:IgnoreOnTestPoint('Mask');
    SetToolHidden('05_lamp');
  
    RegisterMonologObject('area_window', 'C_05_CELL2_WINDOW_HINT');
    RegisterMonologObject('area_bed',    'C_05_CELL2_BED_HINT');
    
    zoom_report = CreateZoomZone(  'area_report1',
                                   'CloseZoneReport1',
                                   'Police_report_1', true);
    zoom_report.on_open = function()
--        ShowMonolog('C_05_REPORT1_HINT', 'C_05_REPORT1_HINT');
    end
    
    zoom_table = CreateZoomZone( 'area_table',
                                 'CloseZoneTible',
                                 'ZOOM_table',      true,
                                 'br02',            false,
                                 'br01',            false,
                                 'bread_full',      true,
                                 'layers',          false,
                                 'skriptum',        false,
                                 'area_bread',      true,
                                 'area_skriptum',   false);
    
    local zoom_table_back = play_field:GetObject('ZOOM_table')
    zoom_table.cockroach  = CreateCockroach();
    zoom_table.cockroach:AttachTo( zoom_table_back );
    zoom_table.cockroach:SetClippingRect(512-320, 384-240, 640, 480)
    zoom_table.cockroach:SetZ(1);
                                 
    zoom_table.on_open = function()
        zoom_table.cockroach:Play();
        
        if not zoom_table.IsObjectVisible('bread_full') then
            play_field:GetObject('layers'):SetOpacity(0.3);
            play_field:GetObject('br01'  ):SetOpacity(0.3);
            play_field:GetObject('br02'  ):SetOpacity(0.3);
        end
        
    end
    
    zoom_table.on_close = function()
        if zoom_table.IsObjectVisible('skriptum') then
            play_field:GetObject('skriptum'):SetOpacity(0.7);
        end
    end
    
    play_field:GetObject('br02'):SetClipping(0,0,833,768);
    
    play_field:GetObject('area_bread'   ):GetAnimation():SetColor('0x01FFFFFF');
    play_field:GetObject('area_skriptum'):GetAnimation():SetColor('0x01FFFFFF');
    
    RegisterToolApplyInfo('05_knife', 'area_bread', nil, nil,   function(tool_name)
                                                                    zoom_table.SetObjectVisible('area_bread', false);
                                                                    zoom_table.SetObjectVisible('area_skriptum', true);
                                                                    
                                                                    zoom_table.SetObjectVisible('bread_full', false);
                                                                    zoom_table.SetObjectVisible('br02',       true);
                                                                    zoom_table.SetObjectVisible('br01',       true);
                                                                    zoom_table.SetObjectVisible('layers',     true);
                                                                    zoom_table.SetObjectVisible('skriptum',   true);
                                                                    
                                                                    RunBreadOpenAnimation();
                                                                    PlaySound('05_CutBread');
                                                                end,
                                                                function(tool_name)
                                                                    zoom_table.SetObjectVisible('area_bread', false);
                                                                    zoom_table.SetObjectVisible('area_skriptum', true);
                                                                    
                                                                    zoom_table.SetObjectVisible('bread_full', false);
                                                                    zoom_table.SetObjectVisible('br02',       true);
                                                                    zoom_table.SetObjectVisible('br01',       true);
                                                                    zoom_table.SetObjectVisible('layers',     true);
                                                                    zoom_table.SetObjectVisible('skriptum',   true);
                                                                    
                                                                    play_field:GetObject('br02'):SetXY(805, 486)
                                                                    play_field:GetObject('layers'):SetXY(469, 441)
                                                                    play_field:GetObject('skriptum'):SetXY(584, 438)
                                                                    play_field:GetObject('br01'):SetXY(454, 428)
                                                                end);
                                                                
    RegisterToolFindInfo('05_scroll_retort', 'area_skriptum',   function(tool_name)
                                                                    zoom_table.Disable();
                                                                    
                                                                    AddNoteToNotebook('JAIL_CELLS_2');
                                                                end,
                                                                function(tool_name)
                                                                    zoom_table.Disable();
                                                                end);
    
    if HasGameState('ToolFound', '05_lamp') then
        play_field:GetObject('dark_cover'):SetVisible(false);
        game_state.lamp_founded = true;
    else
        play_field:GetObject('Lamp'):SetVisible(false);
        play_field:GetObject('Mask'):SetVisible(false);
        
        ShowMonolog('C_05_NEED_LAMP', 'C_05_NEED_LAMP');
    end
    
    UnRegisterQuestObjects('area_report1');
end

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

function OnUpdate(dt)
    if game_state.lamp_founded then
       -- if game_state.locker:GetNReferences() == 0 then
        if not game_state.active_zone then
            local mask = play_field:GetObject('Mask');
            local lamp = play_field:GetObject('Lamp');
            local mpos = GetMousePos();
            
            if currentCursor == 'CursorDown' then
                mpos.y = mpos.y - 40
            end
            
            mask:SetXY(mpos.x, mpos.y);
            lamp:SetXY(mpos.x + 50, mpos.y + 50);
            
            if not play_field:GetObject('Lamp'):IsVisible() then
                play_field:GetObject('Lamp'):SetVisible(true);
            end
        else
            if play_field:GetObject('Lamp'):IsVisible() then
                play_field:GetObject('Lamp'):SetVisible(false);
            end
        end
    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 == 'Police_report_1' then
        ShowMonolog('C_05_REPORT1_HINT', 'C_05_REPORT1_HINT');
    end
    
    if obj_name == 'area_bread' then
        ShowMonolog('C_05_CELL2_CUT_HINT', 'C_05_CELL2_CUT_HINT');
    end
    
    if not game_state.lamp_founded and not IsAboveReturnFromZoomZone() then
        ShowMonolog('C_05_NEED_LAMP', 'C_05_NEED_LAMP');
    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_cell2/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_report1',anim,711,556,101,0,10,6)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Police_report_1.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Police_report_1',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_window',anim,530,208,101,0,10,10)
	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_bed',anim,266,541,101,-30,2.3,1.3)
	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,631,448,101,0,10,10)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/ZOOM_table.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ZOOM_table',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/br01.png',0,0,250,247,1,0)
	anim:SetHotSpot(125,123)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('br01',anim,526,424,152,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/br02.png',0,0,212,215,1,0)
	anim:SetHotSpot(106,107)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('br02',anim,692,441,155,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/layers.png',0,0,160,109,1,0)
	anim:SetHotSpot(80,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('layers',anim,541,437,154,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/skriptum.png',0,0,244,78,1,0)
	anim:SetHotSpot(122,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('skriptum',anim,645,434,153,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_bread',anim,614,433,157,0,3,2)
	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_skriptum',anim,555,427,157,0,2,1)
	local anim = CreateAnimation('data/Texture/Scenes/Common/glow_mask.png',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xEAFFFFFF')
	screen:CreateObject('Mask',anim,577,402,149,0,3.2,3.2)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Animations/Lamp_anim.png',0,0,44,78,5,8)
	anim:SetHotSpot(22,39)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lamp',anim,512,384,401,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('0xEA000000')
	screen:CreateObject('dark_cover',anim,512,384,400,0,128,96)
	local anim = CreateAnimation('data/Texture/Scenes/05_Gendarmery/Zone_cell2/Zoom_bread/bread_full.png',0,0,395,217,1,0)
	anim:SetHotSpot(197,108)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bread_full',anim,598,441,156,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
