local zone_ladder

function add_fibrillation_effector_door_light(obj, delay)
	local duration = 4.0

	eff= CreateAnimationEffector()
	eff:AttachTo(obj)
	eff:SetMode(_ANIM_LOOP)

    local dopacity = -0.

	eff:SetKey(_TRACK_O, 0.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 1.0/4.0 + delay, 0.75 + dopacity)
	eff:SetKey(_TRACK_O, duration * 2.0/4.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 3.0/4.0 + delay, 0.59 + dopacity)
	eff:SetKey(_TRACK_O, duration * 4.0/4.0 + delay, 1.0 + dopacity)
	eff:Play()
end

function add_fibrillation_effector_lamp_light(obj, delay)
	local duration = 0.5

	eff= CreateAnimationEffector()
	eff:AttachTo(obj)
	eff:SetMode(_ANIM_LOOP)

    local dopacity = -0.35

	eff:SetKey(_TRACK_O, 0.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 1.0 / 4.0 + delay, 0.75 + dopacity)
	eff:SetKey(_TRACK_O, duration * 2.0 / 4.0 + delay, 1.0 + dopacity)
	eff:SetKey(_TRACK_O, duration * 3.0 / 4.0 + delay, 0.59 + dopacity)
	eff:SetKey(_TRACK_O, duration * 4.0 / 4.0 + delay, 1.0 + dopacity)
	eff:Play()
end

function OnSceneLoad(screen)
    LoadData(screen)
    DefaultOnSceneLoad('data/Scenes/15_Theatre_Workshop_Inside_Main.lua', 'quest')
	
	RegisterQuestObjects('area_ho', 'area_stick', 'Stairway', 'area_Zoom_Stairway')

    LinkScene('area_ho', 'CursorGlass', 'data/Scenes/15_Theatre_Workshop_Inside_HO.lua', true)
    LinkScene('area_Workshop_Street', 'CursorDoorLeft', 'data/Scenes/14_Workshop_Street_Main.lua', false)

	local on_broken_ladder_click = function()
		ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_STICK')
	end
	local on_broken_ladder_with_nails_click = function()
		ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_NAILS')
	end

    zone_ladder = CreateZoomZone('area_Zoom_Stairway',
                                 'CloseZoneLadder',
                                 'BG_Zoom_Stairway', true,
                                 'Layer_Stick', false,
                                 'Layer_Nail_Up1', false,
                                 'Layer_Nail_Up2', false,
                                 'Layer_Nail_Down1', false,
                                 'Layer_Nail_Down2', false,
                                 'area_stick', true,
								 'area_nails1', true,
								 'area_nails2', true)

    play_field:GetObject('area_stick'):SetMaxOpacity(0.001)
	play_field:GetObject('area_nails1'):SetMaxOpacity(0.001)
	play_field:GetObject('area_nails2'):SetMaxOpacity(0.001)

    play_field:IgnoreOnTestPoint('Door_Light')
    add_fibrillation_effector_door_light(play_field:GetObject('Door_Light'), 0.)
    add_fibrillation_effector_lamp_light(play_field:GetObject('saray_svet'), 0.)

	local on_broken_wo_stick_ladder_click = function()
		ShowMonolog('15_LadderBroken', 'C_15_LADDER_BROKEN')
	end
	RegisterActiveObject('area_stick', on_broken_wo_stick_ladder_click, nil, true, 'CursorGlass')

    play_field:GetObject('Stairway'):SetVisible(false)
    local on_stick_apply = function()
        zone_ladder.SetObjectVisible('Layer_Stick', true)
        play_field:GetObject('Layer_Stick'):ChangeOpacity(0., 1., 0.3)
        zone_ladder.SetObjectVisible('area_stick', false)
        play_field:GetObject('area_stick'):SetVisible(false)
        play_field:GetObject('Stairway'):SetVisible(true)
        play_field:GetObject('Stairway_Broken'):SetVisible(false)
        ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_STICK')
		RegisterActiveObject('Layer_Stick', on_broken_ladder_click, nil, true, 'CursorGlass')
		UnregisterActiveObject('area_stick');
		
		PlaySound("PutStickToLadder");
    end
    local on_load_when_stick_applied = function()
        zone_ladder.SetObjectVisible('Layer_Stick', true)
        zone_ladder.SetObjectVisible('area_stick', false)
        play_field:GetObject('Stairway'):SetVisible(true)
        play_field:GetObject('Stairway_Broken'):SetVisible(false)
		RegisterActiveObject('Layer_Stick', on_broken_ladder_click, nil, true, 'CursorGlass')
		UnregisterActiveObject('area_stick');
    end
    RegisterToolApplyInfo('13_stick', 'area_stick', nil, nil, on_stick_apply, on_load_when_stick_applied)

    local register_hammer_tool_apply_info = function()
        local on_hammer_apply = function(tool_name, obj_name)
			if obj_name == 'area_nails1' then
				play_field:GetObject('Layer_Nail_Up1'):ChangeOpacity(1., 0., 0.2)
				play_field:GetObject('Layer_Nail_Up2'):ChangeOpacity(0., 1., 0.2)
				zone_ladder.SetObjectVisible('Layer_Nail_Up1', false)
				zone_ladder.SetObjectVisible('Layer_Nail_Up2', true);
				UnregisterActiveObject('area_nails1')
				
				PlaySound("HammeringNails");
			end
			if obj_name == 'area_nails2' then
				play_field:GetObject('Layer_Nail_Down1'):ChangeOpacity(1., 0., 0.2)
				play_field:GetObject('Layer_Nail_Down2'):ChangeOpacity(0., 1., 0.2)
				zone_ladder.SetObjectVisible('Layer_Nail_Down1', false)
				zone_ladder.SetObjectVisible('Layer_Nail_Down2', true)
				UnregisterActiveObject('area_nails2')
				
				PlaySound("HammeringNails");
			end
			if not obj_name then
				TimerDoString('FixLadder()', 0.6, play_field:GetTimer())
				UnregisterActiveObject('Layer_Stick')
			end
        end
        local on_load_when_hammer_applied = function(tool_name, obj_name)
			if obj_name == 'area_nails1' then
				play_field:GetObject('Layer_Nail_Up1'):ChangeOpacity(1., 0., 0.2)
				play_field:GetObject('Layer_Nail_Up2'):ChangeOpacity(0., 1., 0.2)
				zone_ladder.SetObjectVisible('Layer_Nail_Up1', false)
				zone_ladder.SetObjectVisible('Layer_Nail_Up2', true)
				UnregisterActiveObject('area_nails1')
			end
			if obj_name == 'area_nails2' then
				play_field:GetObject('Layer_Nail_Down1'):ChangeOpacity(1., 0., 0.2)
				play_field:GetObject('Layer_Nail_Down2'):ChangeOpacity(0., 1., 0.2)
				zone_ladder.SetObjectVisible('Layer_Nail_Down1', false)
				zone_ladder.SetObjectVisible('Layer_Nail_Down2', true)
				UnregisterActiveObject('area_nails2')
			end
			if not obj_name then
				FixLadder()
				UnregisterActiveObject('Layer_Stick')
			end
        end
        RegisterToolApplyInfo('15_hammer', {'area_nails1', 'area_nails2'}, nil, nil, on_hammer_apply, on_load_when_hammer_applied)
    end

    local on_nails_apply = function()
        play_field:GetObject('Layer_Nail_Up1'):ChangeOpacity(0., 1., 0.2)
        play_field:GetObject('Layer_Nail_Down1'):ChangeOpacity(0., 1., 0.2)
        zone_ladder.SetObjectVisible('Layer_Nail_Up1', true)
        zone_ladder.SetObjectVisible('Layer_Nail_Down1', true)
        ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_NAILS')
		RegisterMonologObject('Layer_Stick', 'C_15_LADDER_WITH_NAILS')
		RegisterMonologObject('area_nails1', 'C_15_LADDER_WITH_NAILS') -- C_15_NAIL
		RegisterMonologObject('area_nails2', 'C_15_LADDER_WITH_NAILS') -- C_15_NAIL
		game_state.ignore_on_highlight['Layer_Stick'] = true

        PlaySound("PutNailsToLadder");

        register_hammer_tool_apply_info()
    end
    local on_load_when_nails_applied = function()
        zone_ladder.SetObjectVisible('Layer_Nail_Up1', true)
        zone_ladder.SetObjectVisible('Layer_Nail_Down1', true)
		RegisterMonologObject('Layer_Stick', 'C_15_LADDER_WITH_NAILS')
		RegisterMonologObject('area_nails1', 'C_15_LADDER_WITH_NAILS') -- C_15_NAIL
		RegisterMonologObject('area_nails2', 'C_15_LADDER_WITH_NAILS') -- C_15_NAIL
		game_state.ignore_on_highlight['Layer_Stick'] = true

        register_hammer_tool_apply_info()
    end
    RegisterToolApplyInfo('17_nails', 'Layer_Stick', nil, nil, on_nails_apply, on_load_when_nails_applied)

	if not HasGameState('ToolApplied', '13_stick') then
		RegisterActiveObject('Layer_Stick', on_broken_ladder_click, nil, true, 'CursorGlass')
	elseif HasGameState('ToolApplied', '17_nails') and not HasGameState('ToolApplied', '15_hammer') then
		--RegisterActiveObject('Layer_Stick', on_broken_ladder_with_nails_click, nil, true, 'CursorGlass')
	end

	zone_ladder.on_open = function()
		-- if not HasGameState('ToolApplied', '13_stick') then
			-- ShowMonolog('15_LadderBroken', 'C_15_LADDER_BROKEN')
		-- end
		if HasGameState('ToolApplied', '17_nails') then
			ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_NAILS')
		else 
			if HasGameState('ToolApplied', '13_stick') then
				ShowMonolog('15_LadderBroken', 'C_15_LADDER_WITH_STICK')
			end	
		end
	end

	RegisterMonologObject('area_decor', 'C_15_DECOR_CLICK', true)
	
	LoadSound("PutStickToLadder", "data/Sound/15_Theatre_Workshop_Inside/Put_stick_to_ladder.ogg");
	LoadSound("PutNailsToLadder", "data/Sound/15_Theatre_Workshop_Inside/Put_nails_to_ladder.ogg");
	LoadSound("HammeringNails", "data/Sound/15_Theatre_Workshop_Inside/Hammering_nails.ogg");
end

function RegisterLadderToolFindInfo()
    --local on_find = function()
    --end
    play_field:GetObject('area_Zoom_Stairway'):SetVisible(false)
    RegisterToolFindInfo('15_ladder', 'Stairway', on_find, on_load_when_tool_found)
end

function FixLadder()
    zone_ladder.SetVisible(false)
    zone_ladder.Disable()
    RegisterLadderToolFindInfo()
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

    if is_wrong_click then
        play_field:ShowWrongObjectEffect()
    end
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)

	if tool == '15_hammer' and
	   object:GetName() == 'Layer_Stick' and
	   HasGameState('ToolApplied', '17_nails')
	then
		ShowMonolog('C_15_WRONG_HAMMER_TARGET', 'C_15_WRONG_HAMMER_TARGET')
	end
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/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_ho',anim,531,314,130,0,40,30)
	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_Workshop_Street',anim,188,363,100,0,20,30)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(5,1)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_Zoom_Stairway',anim,355,510,111,11,40,15)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/Stairway.PNG',0,0,379,192,1,0)
	anim:SetHotSpot(189,96)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Stairway',anim,303,566,110,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/Stairway_Broken.PNG',0,0,379,192,1,0)
	anim:SetHotSpot(189,96)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Stairway_Broken',anim,303,567,105,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/saray_svet.png',0,0,155,155,1,0)
	anim:SetHotSpot(77,77)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('saray_svet',anim,534,274,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/saray_svet_1.png',0,0,619,430,1,0)
	anim:SetHotSpot(309,215)
	anim:SetBlendMode(0)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Door_Light',anim,599,414,120,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/BG_Workshop_Inside.jpg',0,0,1024,768,1,0)
	anim:SetHotSpot(512,384)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Workshop_Inside',anim,512,384,40,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/BG_Zoom_Stairway.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('BG_Zoom_Stairway',anim,512,384,150,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/Layer_Stick.PNG',0,0,200,482,1,0)
	anim:SetHotSpot(100,241)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Stick',anim,542,382,151,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/Layer_Nail_Up1.PNG',0,0,50,41,1,0)
	anim:SetHotSpot(25,20)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Nail_Up1',anim,595,188,155,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/Layer_Nail_Down1.PNG',0,0,49,65,1,0)
	anim:SetHotSpot(24,32)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Nail_Down1',anim,469,554,155,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/Layer_Nail_Down2.PNG',0,0,26,26,1,0)
	anim:SetHotSpot(13,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Nail_Down2',anim,482,567,160,0,1,1)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Zoom_Stairway/Layer_Nail_Up2.PNG',0,0,29,29,1,0)
	anim:SetHotSpot(14,14)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Nail_Up2',anim,605,191,160,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_stick',anim,546,379,166,6,40,55)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_nails1',anim,599,210,165,0,12,14)
	local anim = CreateAnimation('data/Texture/Scenes/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('area_nails2',anim,465,540,165,45,12,12)
	local anim = CreateAnimation('data/Texture/Scenes/15_Theatre_Workshop_Inside/Zone_Main/Layers/Layer_Stairway_over.png',0,0,133,92,1,0)
	anim:SetHotSpot(66,46)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('Layer_Stairway_over',anim,184,619,112,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_decor',anim,823,624,100,0,50,35)
end

function FreeData(screen)
end
-- CLEAR_ON_SAVE_END
