dofile('data/Scenes/23_Theatre_play.lua')

local zone_mech
local zone_scheme

function LinkDoor()
	LinkScene('area_door', 'CursorDoorLeftUp', 'data/Scenes/24_Dolls_Main.lua', false)
	
	if HasGameState('ToolFound', '24_pimpa') and
	   HasGameState('ToolFound', '24_runa') and
	   HasGameState('ToolFound', '24_chain')
	then
		game_state.ignore_on_highlight['area_door'] = true
	end
end

function LinkHO()
	LinkScene('area_ho', 'CursorGlass', 'data/Scenes/22_Backstage_HO.lua', true)
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/22_Backstage_Main.lua', 'quest')

	--RegisterQuestObjects('Lever_1', 'Lever_2', 'Lever_3')
	game_state.ignore_on_highlight['Lever_1'] = true
	game_state.ignore_on_highlight['Lever_2'] = true
	game_state.ignore_on_highlight['Lever_3'] = true

	LinkScene('area_back', 'CursorDoorDown', 'data/Scenes/19_Corridor_Main.lua', false)

	play_field:GetObject('area_monolog_sheme'):SetMaxOpacity(0.001)
	zone_scheme = CreateZoomZone('area_zoom_scheme',
                                 'CloseZoneScheme',
                                 'BG_Scheme', true,
								 'area_monolog_sheme', true)

	RegisterMonologObject('area_monolog_sheme', 'C_22_SCHEME')
	RegisterMonologObject('Layer_boy_nods', 'C_22_DOLLS', true)
	RegisterMonologObject('Layer_girl_back', 'C_22_DOLLS', true)
	RegisterMonologObject('Layer_girl_front', 'C_22_BIG_DOLL', true)
	RegisterMonologObject('Layer_Doll_Right', 'C_22_DOLL_SITTING', true)

	zone_mech = CreateZoomZone('area_zoom_machine',
                               'CloseZoneMech',
                               'BG_Mashine', true,
                               'Lever_1', true,
                               'Lever_2', false,
                               'Lever_3', true,
							   'area_lever', true,
							   'area_install_lever', true,
							   'Layer_1', true,
							   'Layer_2', true,
							   'Layer_2_3', true)

	play_field:GetObject('area_install_lever'):SetMaxOpacity(0.001)
	RegisterMonologObject('area_install_lever', 'C_22_NO_LEVER')
	RegisterMonologObject('area_lever', 'C_22_NO_LEVER')

	play_field:GetObject('Lever_Small'):Hide()
	play_field:GetObject('area_lever'):SetMaxOpacity(0.001)
	local on_lever_apply = function()
		zone_mech.SetObjectVisible('area_lever', false)
		zone_mech.SetObjectVisible('Lever_2', true)
		zone_mech.SetObjectVisible('area_install_lever', false)
		play_field:GetObject('Lever_2'):ChangeOpacity(0., 1., 0.3)
		UnregisterActiveObject('area_install_lever')
		UnregisterActiveObject('area_lever')
		ShowMonolog('C_22_ON_LEVER_INSTALL', 'C_22_ON_LEVER_INSTALL')
		play_field:GetObject('Lever_Small'):Show()
		MakeSoundEntity('22_Put_lever'):Play(false);
	end
	local on_load_when_lever_applied = function()
		zone_mech.SetObjectVisible('area_lever', false)
		zone_mech.SetObjectVisible('Lever_2', true)
		zone_mech.SetObjectVisible('area_install_lever', false)
		UnregisterActiveObject('area_install_lever')
		UnregisterActiveObject('area_lever')
		play_field:GetObject('Lever_Small'):Show()
	end
	RegisterToolApplyInfo('21_lever', 'area_lever', nil, nil, on_lever_apply, on_load_when_lever_applied)

	local levers_state = {}

	local start_angle = 5 * M_PI / 180.
	local dangle = 0.3
	
	local get_level_state_name = function(obj_name)
	    return '22:'..obj_name
	end

	local update_lever_position = function(obj_name)
		local state = levers_state[obj_name]
		if state == 'left' then
			play_field:GetObject(obj_name):SetAngle(start_angle - dangle)
		elseif state == 'center' then
			play_field:GetObject(obj_name):SetAngle(start_angle)
		else
			play_field:GetObject(obj_name):SetAngle(start_angle + dangle)
		end
	end

	local on_lever_click = function(obj_name)
		local state = levers_state[obj_name]
		if state == 'left' then
			play_field:GetObject(obj_name):Rotate(dangle, 0.2)
			levers_state[obj_name] = 'center'
		elseif state == 'center' then
			play_field:GetObject(obj_name):Rotate(dangle, 0.2)
			levers_state[obj_name] = 'right'
		else
			play_field:GetObject(obj_name):Rotate(-2. * dangle, 0.2)
			levers_state[obj_name] = 'left'
		end
		SetGameState(get_level_state_name(obj_name), levers_state[obj_name])
		if levers_state['Lever_1'] == 'right' and
		   levers_state['Lever_2'] == 'left' and
		   levers_state['Lever_3'] == 'center'
		then
			AddGameState('TaskComplete', '22_mechanism')
			TimerDoString('CloseZoneMech()', 0.5, play_field:GetTimer())
			TimerDoString('StartMech()', 1.5, play_field:GetTimer())
			UnregisterActiveObject('Lever_1')
			UnregisterActiveObject('Lever_2')
			UnregisterActiveObject('Lever_3')
			zone_mech.Disable()
			game_state.ignore_on_highlight['area_zoom_scheme'] = true
		end

		PlaySound('22_SwitchLever');
	end
	RegisterActiveObject('Lever_1', on_lever_click, on_load_when_lever_clicked, true)
	RegisterActiveObject('Lever_2', on_lever_click, on_load_when_lever_clicked, true)
	RegisterActiveObject('Lever_3', on_lever_click, on_load_when_lever_clicked, true)

	levers_state['Lever_1'] = 'left'
	levers_state['Lever_2'] = 'center'
	levers_state['Lever_3'] = 'center'

    local init_lever_position = function(obj_name)
        if HasGameState(get_level_state_name(obj_name)) then
            levers_state[obj_name] = GetGameState(get_level_state_name(obj_name)):Get(0)
        else
            AddGameState(get_level_state_name(obj_name), levers_state[obj_name])
        end
    end

	init_lever_position('Lever_1')
	init_lever_position('Lever_2')
	init_lever_position('Lever_3')

	update_lever_position('Lever_1')
	update_lever_position('Lever_2')
	update_lever_position('Lever_3')

	play_field:GetObject('Layer_boy_nods'):SetOpacity(0.)
	play_field:GetObject('Layer_girl_back'):SetOpacity(0.)
	play_field:GetObject('Layer_Door_Light'):SetOpacity(0.)
	play_field:GetObject('Layer_Door_Open'):SetOpacity(0.)

	if HasGameState('TaskComplete', '22_mechanism') then
		zone_mech.Disable()
		play_field:GetObject('Layer_boy_nods'):SetOpacity(1.)
		play_field:GetObject('Layer_girl_back'):SetOpacity(1.)
		play_field:GetObject('Layer_girl_front'):SetOpacity(0.)
		play_field:GetObject('Layer_boy_straight'):SetOpacity(0.)
		play_field:GetObject('Layer_Zanaves'):SetOpacity(0.)
		play_field:GetObject('Layer_Door_Light'):SetOpacity(1.)
		play_field:GetObject('Layer_Door_Open'):SetOpacity(1.)
		LinkDoor()
		LinkHO()
		game_state.ignore_on_highlight['area_zoom_scheme'] = true
	end

	LoadSound('22_SwitchLever', 'data/Sound/22_Backstage/Switch_lever.ogg');
	LoadSound('22_Put_lever', 'data/Sound/22_Backstage/Put_lever.ogg');
	LoadSound('23_Rolik_Zal', 'data/Sound/22_Backstage/23_Rolik_Zal.ogg');
end

function CloseZoneMech()
	zone_mech.SetVisible(false)
end

function StartMech()
    StopAmbientMusic(true);
    MakeSoundEntity('23_Rolik_Zal'):Play(false);
                  
	play_field:GetObject('Layer_Zanaves'):Move(0., -100., 2.)
	TimerDoString("local movie = BuildTheatrePlayMovieScreen(); movie:AttachTo(play_field);", 1., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_Zanaves'):SetOpacity(0.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_Door_Light'):SetOpacity(1.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_Door_Open'):SetOpacity(1.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_boy_nods'):SetOpacity(1.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_girl_back'):SetOpacity(1.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_girl_front'):SetOpacity(0.)", 2., play_field:GetTimer())
	TimerDoString("play_field:GetObject('Layer_boy_straight'):SetOpacity(0.)", 2., play_field:GetTimer())
	
	LinkDoor()
	LinkHO()

	game_state.locker:AddRef()
	play_field:GetObject('area_screen'):Show()
	play_field:GetObject('area_screen'):SetOpacity(1.)
	TimerDoString("game_state.locker:Release();" ..
	              "play_field:GetObject('area_screen'):Hide();" ..
	              "PlayRandomAmbientMusic(true);"
                  , 15, play_field:GetTimer()) -- 13 - composition duration

	game_state.highlighting_disabled = true
	TimerDoString("game_state.highlighting_disabled = false", 15, play_field:GetTimer()) -- 13 - composition duration
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

    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/22_Backstage/Zone_Main/Layers/BG_Backstage.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Backstage',anim,512,384,45,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Door_Open.png',0,0,90,167,1,0)
	anim:SetHotSpot(45,83)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Door_Open',anim,127,404,46,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Door_Light.png',0,0,207,299,1,0)
	anim:SetHotSpot(103,149)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Door_Light',anim,187,402,50,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Zanaves.PNG',0,0,687,627,1,0)
	anim:SetHotSpot(343,313)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Zanaves',anim,494,310,47,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Kulisa.PNG',0,0,213,689,1,0)
	anim:SetHotSpot(106,344)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Kulisa',anim,544,339,49,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_boy_straight.png',0,0,50,109,1,0)
	anim:SetHotSpot(25,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_boy_straight',anim,127,406,100,0,1.2,1.2)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_girl_front.png',0,0,224,448,1,0)
	anim:SetHotSpot(112,224)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_girl_front',anim,111,545,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_girl_back.png',0,0,77,185,1,0)
	anim:SetHotSpot(38,92)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_girl_back',anim,405,467,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_boy_nods.png',0,0,104,169,1,0)
	anim:SetHotSpot(52,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_boy_nods',anim,293,453,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_zoom_scheme',anim,928,424,100,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_zoom_machine',anim,323,641,105,0,12,12)
	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_back',anim,505,714,100,0,100,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_ho',anim,686,436,46,0,12,20)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Scheme/BG_Scheme.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0x00FFFFFF')
	screen:CreateObject('BG_Scheme',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/BG_Mashine.jpg',0,0,649,480,1,0)
	anim:SetHotSpot(324,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Mashine',anim,512,384,159,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Lever_2.PNG',0,0,67,181,1,0)
	anim:SetHotSpot(43,190)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lever_1',anim,463,396,163,5,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Lever_2.PNG',0,0,67,181,1,0)
	anim:SetHotSpot(43,190)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lever_2',anim,515,388,162,5,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Lever_2.PNG',0,0,67,181,1,0)
	anim:SetHotSpot(43,190)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lever_3',anim,563,381,160,5,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('0x00FFFFFF')
	screen:CreateObject('area_lever',anim,511,359,170,0,12,12)
	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,127,400,104,0,10,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_monolog_sheme',anim,518,371,151,0,55,35)
	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_install_lever',anim,518,417,165,349,50,30)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Layer_1.png',0,0,98,39,1,0)
	anim:SetHotSpot(49,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_1',anim,464,376,164,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Layer_2.png',0,0,120,41,1,0)
	anim:SetHotSpot(60,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_2',anim,514,365,161,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Curtain_Top.png',0,0,321,242,1,0)
	anim:SetHotSpot(160,121)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Curtain_Top',anim,294,118,100,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Doll_Right.png',0,0,158,157,1,0)
	anim:SetHotSpot(79,78)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Doll_Right',anim,647,594,48,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Lever_Small.PNG',0,0,22,39,1,0)
	anim:SetHotSpot(11,19)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Lever_Small',anim,326,621,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('0x00FFFFFF')
	screen:CreateObject('area_screen',anim,512,384,170,0,128,96)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Layers/Layer_Kulisa_Right.png',0,0,116,683,1,0)
	anim:SetHotSpot(58,341)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Kulisa_Right',anim,795,340,48,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/22_Backstage/Zone_Main/Zoom_Machine/Layer_2.png',0,0,120,41,1,0)
	anim:SetHotSpot(60,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_2_3',anim,556,358,161,0,1,1)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
