local zoom_kettle;

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/21_Mill_Workshop_Main.lua', 'quest', 'data/Scenes/20_Mill_Inside_Main.lua')
    
    game_state.boar_wool_founded = HasGameState('ToolFound', '20_boar_wool');
    game_state.wolf_wool_founded = HasGameState('ToolFound', '20_wolf_wool');
    game_state.razor_founded     = HasGameState('ToolFound', '20_razor');
    
    play_field:GetObject('Layer_hay_open'    ):SetOpacity(0);
    play_field:GetObject('Layer_kettle_small'):SetOpacity(0);
    
    LinkScene('area_ho',
              'CursorGlass',
              'data/Scenes/21_Mill_Workshop_HO.lua',
              true);
    
    zoom_kettle = CreateZoomZone('area_kettle_zoom',
                                 'CloseZonekettle',
                                 'ZOOM_kettle',     true,
                                 'Layer_kettle',    true);

    RegisterToolFindInfo('21_comb', 'Layer_comb_small',                     function(tool_name)
                                                                                play_field:GetObject('Layer_comb_small'):SetVisible(false);
                                                                            end,
                                                                            function(tool_name)
                                                                                play_field:GetObject('Layer_comb_small'):SetVisible(false);
                                                                            end);
                                                                            
    RegisterToolApplyInfo('21_pitchfork', 'area_pitchfork_apply', nil, nil, function(tool_name)
                                                                                play_field:GetObject('Layer_hay_open'    ):ChangeOpacity(0, 1, 0.4);
                                                                                play_field:GetObject('Layer_kettle_small'):ChangeOpacity(0, 1, 0.4);
                                                                                
                                                                                play_field:GetObject('area_pitchfork_apply'):SetVisible(false);
                                                                                play_field:GetObject('area_kettle_zoom'    ):SetVisible(true);
                                                                                
                                                                                PlaySound('21_PitchforkApply');
                                                                            end,
                                                                            function(tool_name)
                                                                                play_field:GetObject('Layer_hay_open'    ):SetOpacity(1);
                                                                                play_field:GetObject('Layer_kettle_small'):SetOpacity(1);
                                                                                
                                                                                play_field:GetObject('area_pitchfork_apply'):SetVisible(false);
                                                                                play_field:GetObject('area_kettle_zoom'    ):SetVisible(true);
                                                                            end);
                                                                            
    RegisterToolFindInfo('21_kettle', 'Layer_kettle',                       function(tool_name)
                                                                                zoom_kettle.Disable();
                                                                                play_field:GetObject('Layer_kettle_small'):SetVisible(false);
                                                                                
                                                                                game_state.kettle_founded = true;
                                                                            end,
                                                                            function(tool_name)
                                                                                zoom_kettle.Disable();
                                                                                play_field:GetObject('Layer_kettle_small'):SetVisible(false);
                                                                                
                                                                                game_state.kettle_founded = true;
                                                                            end);
    
    SwayObjectByAngle('Lamp', 1, 5)
end

function RunFinalSceneDialog()
    CreateDupenAnimations(play_field, 'body', 'head1');
    
    local comics = CreateComics('21_SceneFinal',
                                    CreateComicsFrame('Dupen', 'C_21_FINAL_DUPLEIX1_1_DUPEN', 'body', 'head1'),
                                    CreateComicsFrame('Dupen', 'C_21_FINAL_DUPLEIX1_2_DUPEN', 'body', 'head1'));
    if comics then
        comics.on_finish = function()
            CompleteLevel('Mill');
        end
        comics.SetVisible(true);
    else
        CompleteLevel('Mill');
    end
end

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

function OnUpdate(dt)
    
    if not game_state.all_completed
        and game_state.boar_wool_founded
        and game_state.wolf_wool_founded
        and game_state.kettle_founded
        and game_state.razor_founded
        and game_state.locker:GetNReferences() == 0
        and game_state.fly_tools:GetNReferences() == 0
    then
        game_state.all_completed = true;
        
        play_field:TimerDoString("RunFinalSceneDialog()", 0.5);
    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 == 'area_pitchfork_apply' then
        ShowMonolog('C_21_HAY_HINT', 'C_21_HAY_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/21_Mill_workshop/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/21_Mill_workshop/Zone_main/Layers/Layer_hay_open.jpg',0,0,138,92,1,0)
	anim:SetHotSpot(69,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_hay_open',anim,715,604,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Layers/Layer_kettle_small.png',0,0,69,52,1,0)
	anim:SetHotSpot(34,26)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_kettle_small',anim,745,615,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Layers/Lamp.png',0,0,161,375,1,0)
	anim:SetHotSpot(80,0)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lamp',anim,585,12,102,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Zoom_kettle/ZOOM_kettle.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('ZOOM_kettle',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Zoom_kettle/Layer_kettle.png',0,0,501,480,1,0)
	anim:SetHotSpot(250,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('Layer_kettle',anim,511,384,151,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_kettle_zoom',anim,735,611,103,0,1,0.6)
	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_pitchfork_apply',anim,743,592,104,0,1.5,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_ho',anim,637,405,101,0,1.8,1.3)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Layers/Layer_comb_small.png',0,0,48,51,1,0)
	anim:SetHotSpot(24,25)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_comb_small',anim,216,559,101,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/21_Mill_workshop/Zone_main/Layers/Layer_comb_over.png',0,0,24,24,1,0)
	anim:SetHotSpot(12,12)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_comb_over',anim,215,582,102,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
