local zoom_valve;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/06_Laundry2_Washer.lua', 'quest', 'data/Scenes/06_Laundry2_Main.lua')
    
    game_state._06_button_founded    = HasGameState('ToolFound', '06_button');
    game_state._06_scoop_net_founded = HasGameState('ToolFound', '06_scoop_net');
    
    play_field:GetObject('body'):SetOpacity(0);
    play_field:GetObject('head1'):SetOpacity(0);
    
    play_field:GetObject('Key_anim'):GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    play_field:GetObject('Key_anim'):GetAnimation():Stop();
    
    play_field:GetObject('valve_close'):SetOpacity(0);
    play_field:GetObject('valve_open' ):SetOpacity(0);
    play_field:GetObject('ball'):SetOpacity(0);
    
    local Water_1 = play_field:GetObject('Water_1');
    local Water_2 = play_field:GetObject('Water_2');
    local Water_3 = play_field:GetObject('Water_3');
    
    Water_1:GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    Water_2:GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_LOOP);
    Water_3:GetAnimation():SetMode(HGEANIM_FWD + HGEANIM_NOLOOP);
    Water_1:GetAnimation():Stop();
    Water_2:GetAnimation():Stop();
    Water_3:GetAnimation():Stop();
    Water_1:SetVisible(false);
    Water_2:SetVisible(false);
    Water_3:SetVisible(false);
    
    zoom_valve = CreateZoomZone('area_valve',
                                'CloseZoneValve',
                                'ZOOM_valve',       true,
                                'Key_anim',         false,
                                'area_valve_apply', true);
    
    play_field:GetObject('area_valve_apply'):GetAnimation():SetColor('0x01FFFFFF');
    
    RegisterToolApplyInfo('06_valve', 'area_valve_apply', nil, nil,   function(tool_name)
                                                                        zoom_valve.SetObjectVisible('area_valve_apply', false);
                                                                        zoom_valve.SetObjectVisible('Key_anim', true);
                                                                        
                                                                        play_field:GetObject('valve_close'):ChangeOpacity(0, 1, 0.2);
                                                                        
                                                                        PlaySound('06_PlaceValve');
                                                                        
                                                                        game_state.valve_applied = true;
                                                                    end,
                                                                    function(tool_name)
                                                                        zoom_valve.SetObjectVisible('area_valve_apply', false);
                                                                        zoom_valve.SetObjectVisible('Key_anim', true);
                                                                        
                                                                        play_field:GetObject('valve_close'):SetOpacity(1);
                                                                        
                                                                        game_state.valve_applied = true;
                                                                    end);
    
    RegisterActiveObject('Key_anim',    function(obj_name)
                                            play_field:IgnoreOnTestPoint(obj_name);
                                            
                                            local anim      = play_field:GetObject('Key_anim'):GetAnimation();
                                            local anim_time = GetAnimationLength(anim);
                                            
                                            play_field:GetObject('valve_close'):ChangeOpacity(1, 0, anim_time);
                                            play_field:GetObject('valve_open' ):ChangeOpacity(0, 1, anim_time);
                                            
                                            PlaySound('06_UseValve')
                                            
                                            anim:Play();
                                            TimerDoString("OnValveRotated();", anim_time, play_field:GetTimer());
                                        end,
                                        function(obj_name)
                                            OnValveRotated();
                                        end);
    
    zoom_valve.on_open = function()
    --[[
        if not game_state.tool then
            if game_state.valve_applied then
                ShowMonolog('C_06_VALVE_HINT_2', 'C_06_VALVE_HINT_2');
            else
                ShowMonolog('C_06_VALVE_HINT_1', 'C_06_VALVE_HINT_1');
            end
        end
    --]]
    end
end

function OnValveRotated()
    local state_value = game_state.scene_path.."(water_down)";
    
    play_field:GetObject('valve_close'):SetOpacity(0);
    play_field:GetObject('valve_open' ):SetOpacity(1);
    
    if not HasGameState('ActionCompleted', state_value) then
        if zoom_valve.IsVisible() then
            zoom_valve.SetVisible(false);
        end
        
        CreateWaterDownAnimation()
        PlayWaterAnim(2.88);
        
        PlaySound('06_WaterFall');
        
        AddGameState('ActionCompleted', state_value);
    else
        play_field:GetObject('v1'):SetOpacity(0);
        play_field:GetObject('v2'):SetOpacity(0);
        play_field:GetObject('v3'):SetOpacity(0);
        
        play_field:GetObject('ball'):SetOpacity(1);
    end
    
    RegisterToolFindInfo('06_ball', 'ball', function(tool_name)
                                                AddNoteToNotebook('LAUNDROMAT_3');
                                                
                                                play_field:GetObject('ball'):SetVisible(false);
                                                game_state._06_ball_founded = true
                                            end,
                                            function(tool_name)
                                                play_field:GetObject('ball'):SetVisible(false);
                                                game_state._06_ball_founded = true
                                            end);
    
    zoom_valve.Disable();
end

function PlayWaterAnim(time)
    local anim1 = play_field:GetObject('Water_1');
    local anim2 = play_field:GetObject('Water_2');
    local anim3 = play_field:GetObject('Water_3');
    
    local t1 = GetAnimationLength(anim1:GetAnimation());
    local t3 = GetAnimationLength(anim3:GetAnimation());
    
    local time_sum_13 = GetAnimationLength(anim1:GetAnimation()) + GetAnimationLength(anim2:GetAnimation());
    local middle_time = (time < time_sum_13) and 0 or (time - time_sum_13);
    local total_time  = time_sum_13 + middle_time;
    
    anim1:SetVisible(true);
    anim1:GetAnimation():Play();
    
    TimerDoString(  "play_field:GetObject('Water_1'):SetVisible(false);"..
                    "play_field:GetObject('Water_2'):SetVisible(true);"..
                    "play_field:GetObject('Water_2'):GetAnimation():Play();",
                    GetAnimationLength(anim1:GetAnimation()),
                    play_field:GetTimer());
    
    TimerDoString(  "play_field:GetObject('Water_2'):SetVisible(false);"..
                    "play_field:GetObject('Water_3'):SetVisible(true);"..
                    "play_field:GetObject('Water_3'):GetAnimation():Play();",
                    GetAnimationLength(anim1:GetAnimation()) + middle_time,
                    play_field:GetTimer());
    
    TimerDoString(  "OnWaterStoped();",
                    total_time,
                    play_field:GetTimer());
end

function OnWaterStoped()
    local state_value = game_state.scene_path.."(water_down)";
    
    play_field:GetObject('Water_3'):SetVisible(false);
end

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

function OnUpdate(dt)
    if game_state.fly_tools:GetNReferences() == 0
      and game_state.locker:GetNReferences() == 0
      and game_state._06_ball_founded
      and game_state._06_button_founded
      and game_state._06_scoop_net_founded
      and not game_state.final
    then
        TimerDoString("ShowFinalDialog();", 0.5, play_field:GetTimer());
        game_state.final = true;
    end
    
    DefaultOnUpdate(dt)
end

function ShowFinalDialog()
    local comics = CreateComics('06_Final', CreateComicsFrame('Dupen', 'C_06_FINAL_DUPEN', 'body', 'head1'));
    
    if comics then
        comics.on_finish = function()
            game_state.parent_scene = nil;
            TimerDoString("CompleteLevel('Laundry');", ZOOM_ZONE_APPEARING_TIME, play_field:GetTimer());
        end
        comics.SetVisible( true );
    else
        game_state.parent_scene = nil;
        CompleteLevel('Laundry');
    end
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_valve'
    or obj_name == 'area_valve_apply'
    then
        if game_state.valve_applied then
            ShowMonolog('C_06_VALVE_HINT_2', 'C_06_VALVE_HINT_2');
        else
            ShowMonolog('C_06_VALVE_HINT_1', 'C_06_VALVE_HINT_1');
        end
    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

function CreateWaterDownAnimation()
    local v1 = play_field:GetObject('v1');
    local v2 = play_field:GetObject('v2');
    local v3 = play_field:GetObject('v3');
    
    local eff_v1 = CreateAnimationEffector();
    local eff_v2 = CreateAnimationEffector();
    local eff_v3 = CreateAnimationEffector();
    
    eff_v1:AttachTo( v1 );
    eff_v2:AttachTo( v2 );
    eff_v3:AttachTo( v3 );
    
    eff_v1:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    eff_v2:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    eff_v3:SetMode(_ANIM_FWD + _ANIM_NOLOOP);
    
    eff_v1:SetKey(_TRACK_Y, 0, 428);
    eff_v1:SetKey(_TRACK_Y, 2.88, 514);
    eff_v1:SetKey(_TRACK_O, 0, 1);
    eff_v1:SetKey(_TRACK_O, 0.88, 0.76);
    eff_v1:SetKey(_TRACK_O, 1.92, 0.76);
    eff_v1:SetKey(_TRACK_O, 2.84, 0);
    
    eff_v2:SetKey(_TRACK_Y, 0, 449);
    eff_v2:SetKey(_TRACK_Y, 2.88, 535);
    eff_v2:SetKey(_TRACK_O, 0, 1);
    eff_v2:SetKey(_TRACK_O, 0.88, 0.76);
    eff_v2:SetKey(_TRACK_O, 1.6, 0.76);
    eff_v2:SetKey(_TRACK_O, 2.56, 0);
    
    eff_v3:SetKey(_TRACK_Y, 0, 522.5);
    eff_v3:SetKey(_TRACK_Y, 2.88, 608.5);
    eff_v3:SetKey(_TRACK_O, 0, 1);
    eff_v3:SetKey(_TRACK_O, 0.88, 0.76);
    eff_v3:SetKey(_TRACK_O, 1.48, 0.12);
    
    eff_v1:Play();
    eff_v2:Play();
    eff_v3:Play();
    
    play_field:GetObject('ball'):ChangeOpacity(0, 1, 1.48);
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/06_Laundry2/Zone_washer/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_circle.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_valve',anim,302,612,108,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Zoom_valve/Layers/ZOOM_valve.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_valve',anim,512,384,150,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('0x00FFFFFF')
	screen:CreateObject('area_valve_apply',anim,512,285,154,0,2,2)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Zoom_valve/Animations/Key_anim.png',0,0,253,178,3,12)
	anim:SetHotSpot(126,89)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Key_anim',anim,506,289,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Animations/Water_1.png',0,0,246,160,22,15)
	anim:SetHotSpot(123,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_1',anim,174,688,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Animations/Water_2.png',0,0,248,160,22,15)
	anim:SetHotSpot(124,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_2',anim,173,688,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Animations/Water_3.png',0,0,243,160,22,15)
	anim:SetHotSpot(121,80)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_3',anim,170,688,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/mask1.png',0,0,38,97,1,0)
	anim:SetHotSpot(19,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mask1',anim,493,387,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/mask2.png',0,0,534,510,1,0)
	anim:SetHotSpot(267,255)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mask2',anim,504,456,104,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/mask3.png',0,0,372,254,1,0)
	anim:SetHotSpot(186,127)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('mask3',anim,553,583,107,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/v1.png',0,0,271,208,1,0)
	anim:SetHotSpot(135,104)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('v1',anim,606,428,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/v2.png',0,0,211,237,1,0)
	anim:SetHotSpot(105,118)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('v2',anim,431,449,103,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/v3.png',0,0,338,158,1,0)
	anim:SetHotSpot(169,79)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('v3',anim,549,522,106,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/valve_close.png',0,0,69,56,1,0)
	anim:SetHotSpot(34,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('valve_close',anim,309,615,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/valve_open.png',0,0,63,63,1,0)
	anim:SetHotSpot(31,31)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('valve_open',anim,308,612,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_main/Animations/Water_anim1.png',0,0,632,88,24,10)
	anim:SetHotSpot(316,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_anim1',anim,769,287,101,30,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_main/Animations/Water_anim2.png',0,0,138,268,24,10)
	anim:SetHotSpot(69,134)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_anim2',anim,69,541,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_main/Animations/Water_anim3.png',0,0,255,94,24,10)
	anim:SetHotSpot(127,47)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Water_anim3',anim,217,721,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/Washer_layer.png',0,0,341,297,1,0)
	anim:SetHotSpot(170,148)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Washer_layer',anim,671,224,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/06_Laundry2/Zone_washer/Layers/ball.png',0,0,63,56,1,0)
	anim:SetHotSpot(31,28)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('ball',anim,528,517,105,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,245,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,278,203,111,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
