-------------------------------------------------------------------------------
class 'CInteractiveHelpScreen' (BaseScreen)
-------------------------------------------------------------------------------

function CreateInteractiveHelpScreen(x, y, tipId, confirmCallback)
    local messageTextAlias      = InteractiveHelp.Constants.TIP_MSG_PREFICS..tipId
    local instructionsTextAlias = InteractiveHelp.Constants.TIP_INSTR_PREFICS..tipId
    
	local scr = CreateScriptedScreen('data/Scripts/screens/InteractiveHelpScreen.lua', 'CInteractiveHelpScreen')
	local screen = scr:GetAssociatedLuaObject()
	screen:SetPosXY(x,y)
	screen:SetMessageText(messageTextAlias)
	screen:SetInstructionsText(instructionsTextAlias)
	
	if confirmCallback then
		screen:SetConfirmCallback(confirmCallback)
	end
	
	return screen
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:__init(name, screen)
	BaseScreen.__init(self, name, screen);
    
    self.level = GetCurrentLevel()
    self.rect_cursor = nil
    self.hide_on_rect_focused = false
    
    self:GetObject('fade'):SetVisible(false);

    local parent = self:GetObject('InteractiveHelpWindow')
    
    do
        self.skip_button = self:CreateButton('bttn_skip', 'Skip');
        self.skip_button:SetAction('OnSkipButtonClick', self)
        self.skip_button:SetActionSound("Menu_button_click")
        self.skip_button:SetZ(parent:GetZ() + 2)
    end
	
	do
        self.menu_button = self:CreateButton('bttn_menu', 'Menu')
        self.menu_button:SetAction('OnMenuButtonClick', self)
        self.menu_button:SetActionSound("Menu_button_click")
        self.menu_button:SetKeyEquivalent(KEY_ESCAPE)
    end
	
	do 
		local text = CreateTextEntity(MENU_FONT, "")
		text:AttachTo(parent)
		text:SetY(-70)
		text:SetZ(1)
		text:SetScale(.7)
		text:SetSpacing(0.7)
		text:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE)
		text:SetBorderSize(460, 150)
	    self.text = text
	end
	
	do
		local text = CreateTextEntity(MENU_FONT, 'CLICK_TO_CONTINUE')
		text:AttachTo(parent)
		text:SetRect(0, 0, 1024 * 2, 768 * 2)
		text:SetY(-75)
		text:SetZ(1)
		text:SetScale(0.53)
		text:SetSpacing(0.7)
		text:SetTextAlignment(HGETEXT_CENTER + HGETEXT_BOTTOM)
		text:SetBorderSize(400, 200)
		text:SetAction("Hide", self)
	    self.instructions = text
	end

    self.messageTextAlias = nil
	self.blocksMouseDownInPlayField = true
	self.arrows = {}
    self.confirmCallback = nil
	self.forcedObjectName = nil
	self.oldForcedObjectName = nil
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:OnMenuButtonClick()
    GetCurrentLevel():GetPanelLuaObject():OnMenuButtonClick()
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:OnSkipButtonClick()
    InteractiveHelp.Skip()
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:OnButtonPress(action)
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:OnUpdate(dt)
    if self.rect_cursor then
        local mPos = GetMousePos()
        
        if self.instructions:TestPoint(mPos.x, mPos.y) then
            if currentCursor ~= self.rect_cursor then
                GetGameApp():SetCustomCursor(self.rect_cursor)
                self.prev_cursor = currentCursor
                currentCursor    = self.rect_cursor
            end
    
        elseif self.prev_cursor then
            GetGameApp():SetCustomCursor(self.prev_cursor)
            currentCursor    = self.prev_cursor
            self.prev_cursor = nil
        end
    end
    
    if self.hide_on_rect_focused then
        local mPos = GetMousePos()
        
        if self.instructions:TestPoint(mPos.x, mPos.y) then
            self.hide_on_rect_focused = false
            self:Hide()
        end
    end
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetPosXY(x, y)
	assert(self and self.instructions);
	self:GetObject('InteractiveHelpWindow'):SetXY(x, y);
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetMessageText(messageTextAlias)
	assert(self);
	self.messageTextAlias = messageTextAlias;
    self.text:SetText(GetString(messageTextAlias));
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetInstructionsText(instructionsTextAlias)
	assert(self)
    self.instructions:SetText(GetString(instructionsTextAlias))
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetConfirmCallback(callback)
--	assert(self and callback and type(callback) == 'function')
    self.confirmCallback = callback
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetSkipCallback(callback)
--	assert(self and callback and type(callback) == 'function')
    self.skipCallback = callback
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:AddArrowPointerAt(x, y, angle)
    local angleReckoner = function ()
		local windowPos = {x = self:GetObject('InteractiveHelpWindow'):GetX(), y = self:GetObject('InteractiveHelpWindow'):GetY()};
		local v = Vector(x - windowPos.x, y - windowPos.y);
		return v:Angle(nil);
	end
	angle = angle or angleReckoner();

    local arrow = self:GetTopLevelScreen():CreateAnimation("data/Texture/GUI/InteractiveHelp/TutorialArrow.png")
    arrow:SetColor('0xFFFFFF00')
    arrow:SetHotSpot(185, 39)
    arrow:SetAngle(angle)
    arrow:SetXY(x, y)
    arrow:SetZ(1000)
    
    table.insert(self.arrows, arrow)
    
    local eff = CreateAnimationEffector()
    eff:SetDestroysOnEnd(true)
    eff:AttachTo(arrow)
    eff:SetKey(_TRACK_SX, 0.0, 1.0)
    eff:SetKey(_TRACK_SX, 1.0, 1.3)
    eff:SetKey(_TRACK_SX, 2.0, 1.0)
    eff:SetMode(_ANIM_PINGPONG)
    eff:Play()
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:IsMouseInProperRect()
	local mpos = GetMousePos()

	return self.instructions:TestPoint(mpos.x, mpos.y)
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:AddArrowPointerAtObject(objectName)
    local obj = Object(objectName)
	self:AddArrowPointerAt(obj:GetScrX(), obj:GetScrY())
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetForcedObjectName(objectName)
    self.forcedObjectName = objectName;
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetRectCursor(cursor)
    self.rect_cursor = cursor
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetHideOnRectFocused(value)
    self.hide_on_rect_focused = value
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:SetSkipVisible(bVisible)
    if bVisible then
        self.skip_button:Show()
    else
        self.skip_button:Hide()
    end
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:Show()
    if InteractiveHelp.tutorialScreen and PopUpScreensMgr:GetTop() then
        InteractiveHelp.tutorialScreen:Hide()
    end

	do -- skip button adjustment
		local blackBarHeight = 85
		self.skip_button:SetY(self.skip_button:GetY() + (self.level.comics and blackBarHeight or 0))
	end

    local inventory = play_field:GetLevelScreen():GetInventory()
    self:GetTopLevelScreen():SetLayer( inventory:GetLayer() + 1 )

    SetCursor('CursorDefault')
    
	self.level.mouseBlocker:Push()
	
	if self.forcedObjectName then
	    self.oldForcedObjectName = GetForcedObject()
	    SetForcedObject(self.forcedObjectName)
    end
    
    if self.forcedObjectName then
        self.instructions:SetRect(-10, -10, 5, 5)
    end
    
    InteractiveHelp.tutorialScreen = self
    PopUpScreensMgr:Push(self:GetTopLevelScreen())
    play_field:Go()
    
    HideBlackBar(true)
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:Hide()
    if self.prev_cursor then
        GetGameApp():SetCustomCursor(self.prev_cursor)
        currentCursor    = self.prev_cursor
        self.prev_cursor = nil
    end
    
    if self.forcedObjectName then
        SetForcedObject(self.oldForcedObjectName)
        self.oldForcedObjectName = nil
    end
    
    self.level.mouseBlocker:Pop()
    PopUpScreensMgr:Pop()

    assert(self.messageTextAlias)
    AddGameState(InteractiveHelp.States.TIP_SHOWN, self.messageTextAlias)
        
    InteractiveHelp.tutorialScreen = nil

    if self.confirmCallback then
        self.confirmCallback()
    end
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:Skip()
    self:SetConfirmCallback( self.skipCallback )
    self:Hide()
end
-------------------------------------------------------------------------------

function CInteractiveHelpScreen:Destroy()
    PopUpScreensMgr:Pop()
    self.screen:Destroy()
end
-------------------------------------------------------------------------------



--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function CInteractiveHelpScreen:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/GUI/Panel/bttn/bttn_menu_txt.png',0,0,120,30,3,0)
	anim:SetHotSpot(60,15)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_menu',anim,100,660,500,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,4)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_block',anim,940,31,99,0,20,5)
	
	anim = CreateAnimation('data/Texture/GUI/InteractiveHelp/SkipTutorialButton.png',0,0,153,45,3,0)
	anim:SetHotSpot(76,22)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('bttn_skip',anim,940,34,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/GUI/InteractiveHelp/InteractiveHelpWindow.png',0,0,564,279,1,0)
	anim:SetHotSpot(282,139)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('InteractiveHelpWindow',anim,512,384,1001,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
