ASK_FOR_HELP = "AskedForInteractiveHelp"
SHOWING_HELP = "ShowingInteractiveHelp";
HELP_SHOWN   = "InteractiveHelpShown";

tutorScreen  = nil;
interactiveHelpQuestion = nil;

--------------------------------------------------------------------------------
TutorialScreen = {};

--------------------------------------------------------------------------------
function TutorialScreen:New()
    local result = CreateObjectOfClass(self);
    
    result.screen = CreateScreen();
    result.screen:SetLayer(POPUP_WINDOWS_LAYER);
    result.blocksMouseDownInPlayField = true;
    -- bg
    
    result.bg = result.screen:CreateAnimation("data/Texture/GUI/Help/InteractiveHelpBg.png"); -- 564, 279
    result.bg:SetHotSpot(282, 139);
    result.bg:SetPosZ(0);
    
    -- content
    
    result.textBox = CreateTextEntity(INGAME_MENU_FONT, "");
	result.textBox:SetZ(1);
	result.textBox:SetScale(0.60);
	result.textBox:AttachTo(result.screen);
	result.textBox:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
	
	-- click to continue title
	
	result.clickToCtn = CreateTextEntity(INGAME_MENU_FONT, 'CLICK_TO_CONTINUE')
	result.clickToCtn:SetRect(0, 0, 1024, 768);
	result.clickToCtn:SetZ(2);
	result.clickToCtn:SetScale(0.45);
	result.clickToCtn:AttachTo(result.screen);
	result.clickToCtn:SetTextAlignment(HGETEXT_CENTER + HGETEXT_MIDDLE);
	result.clickToCtn:SetAction("Hide", result);
	
	-- skip button
	result.skipButton = MakeButton(1024 - 90, 35, 150, 50);
	result.skipButton:SetSpriteUp("SkipTutorialButton");
	result.skipButton:SetSpriteOver("SkipTutorialButtonOver");
	result.skipButton:SetSpriteDown("SkipTutorialButtonDown");
	result.skipButton:SetActionSound("Menu_button_click");
	
	result.skipButton:AttachTo(result.screen);
	result.skipButton:SetAction("SkipInteractiveHelp");
	result.skipButton:SetZ(5);
	
    result.arrows = {};
    return result;
end

--------------------------------------------------------------------------------
function TutorialScreen:SetPos(x, y)

    self.bg:SetPosXY(x, y);
    self.textBox:SetXY(x, y);
    
    self.clickToCtn:SetXY(x, y + 100);
    self.clickToCtn:SetRect(0, 0, 1024, 768);
    
    self.textBox:SetY(y - 10);
    
end

--------------------------------------------------------------------------------
function TutorialScreen:SetTipAlias(tipAlias)
    self.tipAlias = tipAlias;
    self.screen:SetName("Tutorial Screen " .. tipAlias);
    self.textBox:SetText(GetString(tipAlias));
end

--------------------------------------------------------------------------------
function TutorialScreen:SetContinueAlias(continueAlias)
    self.clickToCtn:SetText(GetString(continueAlias));
end

--------------------------------------------------------------------------------
function TutorialScreen:SetConfirmCallback(callback)
    self.confirmCallback = callback;
end

--------------------------------------------------------------------------------
function TutorialScreen:AddHelpArrow(x, y, angle)

    local arrow = self.screen:CreateAnimation("data/Texture/GUI/Help/TutorialArrow.png"); -- 64 32
    arrow:SetColor('0xFFFF2222');
    arrow:SetHotSpot(64, 16);
    arrow:SetAngle(math.rad(angle));
    arrow:SetXY(x, y);
    arrow:SetScale(2.5);
    arrow:SetZ(10);
    
    table.insert(self.arrows, arrow);
    
    local eff = CreateAnimationEffector();
    eff:AttachTo(arrow);
    eff:SetKey(_TRACK_O, 0.0, 1.0)
    eff:SetKey(_TRACK_O, 2.0, 0.6)
    eff:SetKey(_TRACK_O, 4.0, 1.0)
    eff:SetMode(_ANIM_LOOP);
    eff:Play();
    
end

--------------------------------------------------------------------------------
function TutorialScreen:AddCursor(cursorName, x, y)

    self.cur = self.screen:CreateAnimationByRef(cursorName)
    self.cur:SetOpacity(0.9);
    self.cur:SetHotSpot(45, 46);
    self.cur:SetXY(x,  y);
    self.cur:SetZ(7);
    
    local anim = self.cur:GetAnimation();
    anim:SetTextureRect(0,0,89,93);
    anim:Stop();
    anim:SetFrame(0);
    
    local eff = CreateAnimationEffector();
    eff:AttachTo(self.cur);
    
    eff:SetKey(_TRACK_S, 0.0, 1)
    eff:SetKey(_TRACK_S, 1.0, 1.25)
    eff:SetKey(_TRACK_S, 2.0, 1)
    
    eff:SetMode(_ANIM_LOOP);
    eff:Play();
end

--------------------------------------------------------------------------------
function TutorialScreen:SetForcedObjectName(objName)
    self.forcedObjectName = objName;
end

--------------------------------------------------------------------------------
function TutorialScreen:SetForcedToolName(toolName)
    self.forcedToolName = toolName;
end

--------------------------------------------------------------------------------
function TutorialScreen:Show()

    if tutorScreen
    then 
        tutorScreen:Hide();
    end

    PopUpScreensMgr:Push(self.screen);
    SetCursor('CursorDefault');
    
    if (self.blocksMouseDownInPlayField)
    then
        self.blocksMouseDownToRestore = play_field.mouseDownBlocked;
        play_field.mouseDownBlocked = true;
    end
    
    if (self.forcedObjectName)
    then
        self.clickToCtn:SetRect(-10, -10, 5, 5);
    end
    
    game_state.forcedObject = self.forcedObjectName;
    game_state.forcedTool   = self.forcedToolName;
    
    play_field:Go();
    
    if self.forcedToolName then
        play_field:GetLevelScreen():GetInventory():Go();
    end
    
    tutorScreen = self;
end

--------------------------------------------------------------------------------
function TutorialScreen:Hide()
    
    if game_state.forcedObject and self.forcedObjectName and
        game_state.forcedObject == self.forcedObjectName
    then
        game_state.forcedObject = nil;
    end
    
    if game_state.forcedTool and self.forcedToolName and
        game_state.forcedTool == self.forcedToolName
    then
        game_state.forcedTool = nil;
    end
    
    assert(PopUpScreensMgr:GetTop() == self.screen);
    PopUpScreensMgr:Pop();
    
    AddGameState(HELP_SHOWN, self.tipAlias);
    
    if (self.blocksMouseDownInPlayField)
    then
        play_field.mouseDownBlocked = self.blocksMouseDownToRestore;
    end
    
    tutorScreen = nil;
    
    if (self.confirmCallback)
    then
        self.confirmCallback();
    end
    
end

--------------------------------------------------------------------------------
function AskForInteractiveHelp()

    if (HasGameState(ASK_FOR_HELP))
    then
        return;
    end
    
    interactiveHelpQuestion =
        BuildYesNoScreen("INTERACTIVE_HELP_NEEDED",
                         "InteractiveHelpAgree",
                         "InteractiveHelpDecline");
                         
    PopUpScreensMgr:Push(interactiveHelpQuestion);
end

--------------------------------------------------------------------------------
function InteractiveHelpAgree()

    AddGameState(ASK_FOR_HELP, "true");
    
    play_field.mouseDownBlocked = false;
    AddGameState(SHOWING_HELP, "true");
    PopUpScreensMgr:Pop();
    
    PanelSetNotebookDisable(true)
    TimerDoString("ShowZoomZoneTip();", 2, play_field:GetTimer());
    
end

--------------------------------------------------------------------------------
function InteractiveHelpDecline()

    AddGameState(ASK_FOR_HELP, "true")

    play_field.mouseDownBlocked = false;

    PopUpScreensMgr:Pop();

end

--------------------------------------------------------------------------------
function InteractiveHelpBeingShown()
    return HasGameState(SHOWING_HELP, "true");
end

-------------------------------------------------------------------------------
function SkipInteractiveHelp()

    if tutorScreen
    then
        tutorScreen:SetConfirmCallback(nil);
        tutorScreen:Hide();
    end
    SetGameState(SHOWING_HELP, "false");
    
end

-------------------------------------------------------------------------------
function TurnOffHelpTip(TipName)
    
    if (not InteractiveHelpBeingShown())
        or (tutorScreen and tutorScreen.tipAlias == TipName)
        or HasGameState(HELP_SHOWN, TipName)
    then
        return;
    end
    
    AddGameState(HELP_SHOWN, TipName)
end

--------------------------------------------------------------------------------
--              TIP FUNCTIONS
--------------------------------------------------------------------------------
function ShowZoomZoneTip()
    
    if not HasGameState(HELP_SHOWN, 'TIP_ZOOM_ZOON') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_ZOOM_ZOON');
        tutorScr:SetContinueAlias('CLICK_SPARKLES');
        tutorScr:SetPos(650, 250);
        tutorScr:AddHelpArrow(830, 610, 65);
        tutorScr:SetForcedObjectName('area_paper');
        
        tutorScr:Show();
    end    
end

--------------------------------------------------------------------------------
function ShowNotebookTip()

    if not HasGameState(HELP_SHOWN, 'TIP_NOTEBOOK') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_NOTEBOOK');
        tutorScr:SetContinueAlias('CLICK_NOTEBOOK');
        tutorScr:SetPos(650, 400);
        tutorScr:AddHelpArrow(968, 697, 50);
        
        tutorScr:Show();
        tutorScr.clickToCtn:SetRect(925, 644, 86, 106);
        play_field:GetLevelScreen():GetPanel():Go();
    end
    
end

--------------------------------------------------------------------------------
function ShowDoorTip()
    if game_state.locker:GetNReferences() ~= 0 then
        play_field:TimerDoString("ShowDoorTip();", 1);
        return;
    end
    
  --  if not HasGameState(HELP_SHOWN, 'TIP_DOOR') and InteractiveHelpBeingShown()
  --  then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_DOOR');
        tutorScr:SetContinueAlias('CLICK_TO_CONTINUE');
        tutorScr:SetPos(300, 160);
        tutorScr:AddHelpArrow(490, 430, 50);
        tutorScr:SetForcedObjectName('area_door');
        
        tutorScr:Show();
   -- end
        
end

--------------------------------------------------------------------------------
function ShowSparklesTip()
    if not HasGameState(HELP_SHOWN, 'TIP_SPARKLES') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_SPARKLES');
        tutorScr:SetContinueAlias('CLICK_TO_CONTINUE');
        tutorScr:SetPos(512, 384);
        tutorScr.clickToCtn:SetRect(0, 0, 1024, 768);
        
        tutorScr:Show();
    end
end

--------------------------------------------------------------------------------
function ShowUseGlueTip()
    if not HasGameState(HELP_SHOWN, 'TIP_USE_ITEM') and InteractiveHelpBeingShown()
    then
        local glue_x = 1024 - 50;
        local glue_y = 768 - 50;
        
        local inventory  = play_field:GetLevelScreen():GetInventory();
        local anchorPos  = inventory:GetAnchorPosition();
        local itemsCount = inventory:GetToolsCount();

        for i=0, itemsCount-1
        do
            local toolName = inventory:GetToolName(i);
            
            if toolName == '01_glue'
            then
                local itemPosition = inventory:GetToolPosition(i);
                glue_x = itemPosition.x + anchorPos.x
                glue_y = itemPosition.y + anchorPos.y
                break;
            end
        end
        
        local glue_arrow_angle = math.atan2(glue_y - 160, glue_x - 724) * 180/M_PI;
        local lock_arrow_angle = math.atan2(525 - 160, 375 - 724) * 180/M_PI;
        
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_USE_ITEM');
        tutorScr:SetContinueAlias('CLICK_TO_CONTINUE');
        tutorScr:SetPos(724, 160);
        tutorScr:AddHelpArrow(glue_x, glue_y, glue_arrow_angle);
        tutorScr:AddHelpArrow(525, 375, lock_arrow_angle);
        tutorScr:SetForcedToolName('01_glue')
        tutorScr.clickToCtn:SetRect(-1, -1, 1, 1);
        
        if game_state.tool then
            DropTool();
        end
        
        tutorScr:Show();
        
        game_state.block_dialogs_in_zoom_lock = false;
    end
end

--------------------------------------------------------------------------------
function ShowHiddenObjectTip()
    if not HasGameState(HELP_SHOWN, 'TIP_HIDDEN_OBJECT') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TIP_HIDDEN_OBJECT');
        tutorScr:SetContinueAlias('CLICK_TO_CONTINUE');
        tutorScr:SetPos(512, 384);
        tutorScr.clickToCtn:SetRect(0, 0, 1024, 768);
        
        tutorScr:Show();
    end
end
