dofile('data/Scripts/screens/InteractiveHelpScreen.lua')

InteractiveHelp = {
--------------------------------------------------------------------------------
	tutorialScreen      = nil,
	askDialogPostAction = nil,
	forcedObject        = nil,
	registeredTips      = {},

	States = {
			ASKED_FOR_HELP  = 'AskedForInteractiveHelp',
			HELP_ACTIVE     = "InteractiveHelpActive",
			TIP_SHOWN       = 'InteractiveTipShown'
			},
	
	Constants = {
	            TIP_MSG_PREFICS   = 'INTERACTIVE_HELP_MSG_',
                TIP_INSTR_PREFICS = 'INTERACTIVE_HELP_INSTR_',
                DEFAULT_TIP_SHOW_DELAY = 0.5 -- 1.5
	            },
	----------------------------------------------------------------------------
    
	AskDialog = {
		Show = function (postAction)
		    if HasGameState(InteractiveHelp.States.ASKED_FOR_HELP) then
		        if postAction then
                    local event  = {}
                    event.save   = true
                    event.agreed = toboolean(GetGameState(InteractiveHelp.States.HELP_ACTIVE):Get(0))
                    
                    postAction(event)
                end
		    else
                InteractiveHelp.askDialogPostAction = postAction
                GetCurrentLevel().mouseBlocker:Push()

                local screen = CreateYesNoScreen("INTERACTIVE_HELP_NEEDED",
                                                 "InteractiveHelp.AskDialog.OnAgree",
                                                 "InteractiveHelp.AskDialog.OnDecline", false)
                play_field:Go()
            end
		end,
        
		OnAgree = function ()
		    InteractiveHelp.AskDialog.AskDialogCallback(true)
		end,
		
		OnDecline = function ()
		    InteractiveHelp.AskDialog.AskDialogCallback(false)
		end,
		
		AskDialogCallback = function(agreed)
		    SetGameState(InteractiveHelp.States.ASKED_FOR_HELP, 'true')
		    SetGameState(InteractiveHelp.States.HELP_ACTIVE,    tostring(agreed))
		    
		    GetCurrentLevel().mouseBlocker:Pop()

			if InteractiveHelp.askDialogPostAction then
			    local event  = {}
			    event.save   = false
			    event.agreed = agreed
			    
				InteractiveHelp.askDialogPostAction(event)
				InteractiveHelp.askDialogPostAction = nil
			end
		end
	},
	----------------------------------------------------------------------------

	WasAskedForHelp = function ()
		return HasGameState(InteractiveHelp.States.ASKED_FOR_HELP)
	end,
	----------------------------------------------------------------------------
    
    IsActive = function()
		--return true
        return HasGameState(InteractiveHelp.States.HELP_ACTIVE, 'true')
    end,
    ----------------------------------------------------------------------------

	Skip = function ()
	    if InteractiveHelp.tutorialScreen then
	        InteractiveHelp.tutorialScreen:Skip()
	   	end
	   	
	    SetGameState(InteractiveHelp.States.HELP_ACTIVE, 'false')
	end,
	----------------------------------------------------------------------------
	
	InGameMenu = function ()
	    GetCurrentLevel():GetPanelLuaObject():OnMenuButtonClick()
	end,
	----------------------------------------------------------------------------

	RegisterTipConstructor = function (id, constructor)
		assert(not InteractiveHelp.registeredTips[id] and "Attempt to register tip with existing Id !");
		InteractiveHelp.registeredTips[id] = constructor;
	end,
	----------------------------------------------------------------------------

	ShowTip = function (tipId, delay, ignoreChecking)
		assert(InteractiveHelp.registeredTips[tipId])
		
		if InteractiveHelp.IsActive() or ignoreChecking then
            if not InteractiveHelp.IsTipShown(tipId) then
                local level = GetCurrentLevel()
                
                level.mouseBlocker:Push()
                level.timer:Add(function()
                                    local inv = GetCurrentLevel():GetInventoryLuaObject()
                                    
                                    if inv:GetTool() then
                                        inv:DropTool()
                                    end
                                    
                                    level.mouseBlocker:Pop()
                                    InteractiveHelp.registeredTips[tipId](tipId, ignoreChecking)
                                end,
                                delay or InteractiveHelp.Constants.DEFAULT_TIP_SHOW_DELAY)
            end
        end
	end,
	----------------------------------------------------------------------------
		
    IsTipShown = function (tipId)
		local messageTextAlias = InteractiveHelp.Constants.TIP_MSG_PREFICS..tipId
		return HasGameState(InteractiveHelp.States.TIP_SHOWN, messageTextAlias)
    end
}


--------------------------------------------------------------------------------
-- Registered Tips -------------------------------------------------------------
--------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('COMICS', function (tipId)
    local screen = CreateInteractiveHelpScreen(300, 450, tipId)
    screen:AddArrowPointerAtObject('area_comics_dupin')
    screen:SetForcedObjectName('area_comics_dupin')
    
	local callback = function()
		
    end
    
    local skipCallback = function()
        SetForcedObject('area_comics_dupin')
    end
    
    screen:SetConfirmCallback( callback )	
    screen:SetSkipCallback( skipCallback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('NOTEBOOK', function (tipId)
    local screen = CreateInteractiveHelpScreen(573, 473, tipId)
    local level  = GetCurrentLevel()
    local panel  = level:GetPanelLuaObject()
    
    local w = panel.buttons.notebook_button:GetSpriteUp():GetWidth()
    local h = panel.buttons.notebook_button:GetSpriteUp():GetHeight()
    local x = 92
    local y = 721
    
   -- screen.instructions:SetRect(x - w/2, y - h/2, w, h)
    screen:AddArrowPointerAt(x, y)
    
    local callback = function()
        local mPos = GetMousePos()
        
        if panel.buttons.notebook_button:TestPoint(mPos.x, mPos.y) then
            panel:OnNotesButtonClick(true)
        else
            panel.bIgnoreMouseBlockerForNotebook = true
        end
        
        local level = GetCurrentLevel()
        level:fogOut(0.5, bind(level.create_comics_villain, level))
        --GetCurrentLevel():create_comics_villain()
    end
    
    local skipCallback = function()
        local level = GetCurrentLevel()
        level:fogOut(0.5, bind(level.create_comics_villain, level))
        --GetCurrentLevel():create_comics_villain()
    end
    
    screen:SetConfirmCallback( callback )
    screen:SetSkipCallback( skipCallback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('ZOOM', function (tipId)
    local screen = CreateInteractiveHelpScreen(300, 450, tipId)
    screen:AddArrowPointerAtObject('area_zoom_hatch')
    screen:SetForcedObjectName('area_zoom_hatch')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )	
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('FIND_TOOL', function (tipId)
    local screen = CreateInteractiveHelpScreen(700, 300, tipId)
    screen:AddArrowPointerAtObject('area_find_boathook')
    screen:SetForcedObjectName('area_find_boathook')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )	
    screen:Show()
end)
-------------------------------------------------------------------------------


InteractiveHelp.RegisterTipConstructor('SPARKLE', function (tipId)
    local screen = CreateInteractiveHelpScreen(700, 300, tipId)
    screen:AddArrowPointerAtObject('area_link_boat')
    screen:SetForcedObjectName('area_link_boat')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )	
    screen:Show()
    
    HighlightObject('area_link_boat')
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('TOOL_APPLY', function (tipId)
    local screen = CreateInteractiveHelpScreen(600, 475, tipId)
    local level  = GetCurrentLevel()
    local inv    = level:GetInventoryLuaObject()
    local SlideInvTime = 0.4
    
    local tool_name = '01_boathook'
    local cell      = inv:GetCellInfoByToolName(tool_name).cell
    local x         = cell:GetScrX()
    local y         = 725
    local half_w    = 84/2
    local half_h    = 84/2
    
    local invTimer
    
    local callback = function()
        if screen:IsMouseInProperRect() then
            level:GetInventory():Go()
            inv:TakeTool(tool_name)
            inv:SlideDown(SlideInvTime)
            
            invTimer:Kill()
        end
    end
    
    
    screen.instructions:SetRect(x - half_w, y - half_h, half_w*2, half_h*2)
    screen:SetConfirmCallback( callback )
    screen:SetRectCursor('CursorActive')
    screen:AddArrowPointerAt(x, y)
    screen:AddArrowPointerAtObject('area_apply_boathook')
    screen:Show()
    
    do
        level:GetInventory():Go()
        inv:SlideUp(SlideInvTime)
        
        invTimer = level.timer:Add(function()
                            level:GetInventory():Stop()
                        end, SlideInvTime)
    end
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('TRANSITION', function (tipId)
    local screen = CreateInteractiveHelpScreen(700, 500, tipId)
    screen:AddArrowPointerAtObject('area_link_02')
    screen:SetForcedObjectName('area_link_02')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )	
    screen:Show()
    screen.instructions:SetRect(0, 0, SCR_W, SCR_H)
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('DOG', function (tipId, bWithoutSkip)
    local screen = CreateInteractiveHelpScreen(300, 150, tipId)
    screen:AddArrowPointerAtObject('area_get_zd_dog')
    screen:SetForcedObjectName('area_get_zd_dog')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )
    screen:SetSkipVisible(not bWithoutSkip)
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('USE_DOG', function (tipId, bWithoutSkip)
    local screen = CreateInteractiveHelpScreen(600, 475, tipId)
    local level  = GetCurrentLevel()
    local inv    = level:GetInventoryLuaObject()
    local SlideInvTime = 0.4
    
    local tool_name = '05_piece_of_cloth'
    local cell      = inv:GetCellInfoByToolName(tool_name).cell
    local x         = cell:GetScrX()
    local y         = 725
    local half_w    = 84/2
    local half_h    = 84/2
    
    local invTimer
    
    local callback = function()
        if screen:IsMouseInProperRect() then
            level:GetInventory():Go()
            inv:TakeTool(tool_name)
            inv:SlideDown(SlideInvTime)
            
            invTimer:Kill()
        end
    end
    
    screen.instructions:SetRect(x - half_w, y - half_h, half_w*2, half_h*2)
    screen:SetConfirmCallback( callback )
    screen:SetRectCursor('CursorActive')
    screen:AddArrowPointerAt(x, y)
    screen:AddArrowPointerAt(920, 725)
    screen:SetSkipVisible(not bWithoutSkip)
    screen:Show()
    
    do
        level:GetInventory():Go()
        inv:SlideUp(SlideInvTime)
        
        invTimer = level.timer:Add(function()
                            level:GetInventory():Stop()
                        end, SlideInvTime)
    end
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('HO', function (tipId)
    local screen = CreateInteractiveHelpScreen(300, 150, tipId)
    screen:AddArrowPointerAtObject('area_link_hp')
    screen:SetForcedObjectName('area_link_hp')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('FROG_ACTION', function (tipId)
    local screen = CreateInteractiveHelpScreen(600, 300, tipId)
    screen:AddArrowPointerAtObject('area_click_left_leaf')
    screen:SetForcedObjectName('area_click_left_leaf')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('FROG_FIND', function (tipId)
    local screen  = CreateInteractiveHelpScreen(650, 450, tipId)
    local task    = GetTask('peg_1')
    local iconPos = GetCurrentLevel():GetAdditionalLogic():GetIndicator():GetTaskPos(task)
    
    screen:AddArrowPointerAt(iconPos.x, iconPos.y)
    screen:AddArrowPointerAtObject('area_find_peg_1')
    screen:SetForcedObjectName('area_find_peg_1')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('FROG_DROP', function (tipId)
    local screen  = CreateInteractiveHelpScreen(700, 500, tipId)
    
    local level  = GetCurrentLevel()
    local object = level:GetPanelLuaObject():GetObject('back_rake_handle')
    
    local iconSize = level:GetAdditionalLogic():GetIndicator().SLOT_SIZE
    local iconPos  = Vector(object:GetScrX(),
                            object:GetScrY())
    
    screen:AddArrowPointerAt(iconPos.x, iconPos.y)
    screen:AddArrowPointerAtObject('area_drop_zr_rake_handle')
    screen.instructions:SetRect(iconPos.x - iconSize/2,
                                iconPos.y - iconSize/2,
                                iconSize,
                                iconSize)
    
	local callback = function()
		level:GetAdditionalLogic():OnPanelObjectMouseDown(object)
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('HO_ITEM', function (tipId)
    local screen  = CreateInteractiveHelpScreen(650, 450, tipId)
    
    local task = GetTask('Star')
    local pos  = GetCurrentLevel():GetAdditionalLogic():GetIndicator():GetTaskPos(task)
    
    screen:AddArrowPointerAt(pos.x + 20, pos.y)
    screen:AddArrowPointerAtObject('Star')
    screen:SetForcedObjectName('Star')
    
	local callback = function()
		InteractiveHelp.ShowTip('HO_INTERACTIVE', 4)
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------

InteractiveHelp.RegisterTipConstructor('HO_INTERACTIVE', function (tipId)
    local screen  = CreateInteractiveHelpScreen(650, 450, tipId)
    screen:AddArrowPointerAtObject('area_click_door_left')
    screen:SetForcedObjectName('area_click_door_left')
    
	local callback = function()
		
    end
    
    screen:SetConfirmCallback( callback )
    screen:Show()
end)
-------------------------------------------------------------------------------
InteractiveHelp.RegisterTipConstructor('SKIP', function (tipId)
    local screen  = CreateInteractiveHelpScreen(650, 350, tipId)
    screen:AddArrowPointerAt(920, 640)

    local callback = function()
        
    end

    screen:SetConfirmCallback( callback )
    screen:Show()
end)
    -------------------------------------------------------------------------------