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"); -- 541, 307
    result.bg:SetHotSpot(270.5, 163);
    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 + 115);
    self.clickToCtn:SetRect(0, 0, 1024, 768);

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/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: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;
    
    play_field:Go();
    
    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

    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);
    play_field:Go();
end

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

    AddGameState(ASK_FOR_HELP, "true");
    
    play_field.mouseDownBlocked = false;
    AddGameState(SHOWING_HELP, "true");
    PopUpScreensMgr:Pop();
    
    OnEnterComicsShown();
    
    --ShowHiddenObjectTip();
    
end

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

    AddGameState(ASK_FOR_HELP, "true")

    play_field.mouseDownBlocked = false;

    PopUpScreensMgr:Pop();
    
    OnEnterComicsShown()

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 ShowHiddenObjectTip()

    local tutorScr = TutorialScreen:New();
    tutorScr:SetTipAlias('HIDDEN_OBJECT_TIP');
    tutorScr:SetContinueAlias('CLICK_SPARKLES');
    tutorScr:SetPos(400, 200);
    tutorScr:AddHelpArrow(600, 500, 45);
    --tutorScr:SetConfirmCallback(ShowZoomZoneTip);
    tutorScr:SetForcedObjectName('area_door');
    
    tutorScr:Show();

end

--------------------------------------------------------------------------------
function ShowZoomZoneTip()
    
    local tutorScr = TutorialScreen:New();
    tutorScr:SetTipAlias('ZOOM_ZONE_TIP');
    tutorScr:SetContinueAlias('CLICK_JEWEL_BOX');
    tutorScr:SetPos(650, 215);
    tutorScr:AddHelpArrow(385, 480, 135);
    tutorScr:SetForcedObjectName('area_box');
    
    tutorScr:Show();
    
end

--------------------------------------------------------------------------------
function ShowInsideZoomZoneTip()

    local tutorScr = TutorialScreen:New();
    tutorScr:SetTipAlias('INSIDE_ZOOM_ZONE_TIP');
    tutorScr:SetPos(500, 300);
    
    tutorScr:Show();

end

--------------------------------------------------------------------------------
function ShowGoToLocTip()

    local tutorScr = TutorialScreen:New();
    tutorScr:SetTipAlias('GOTO_OTHER_LOC_TIP');
    tutorScr:SetContinueAlias('CLICK_DOOR');
    tutorScr:SetPos(580, 250);
    tutorScr:AddHelpArrow(965, 490, 45);
    tutorScr:SetForcedObjectName('area_camin_hall');
    
    tutorScr:Show();

end

--------------------------------------------------------------------------------
function ShowInsideHiddenObjectTip()

    if not HasGameState(HELP_SHOWN, 'INSIDE_HO_TIP') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('INSIDE_HO_TIP');
        tutorScr:SetContinueAlias('CLICK_ITEM');
        tutorScr:SetPos(350, 360);
        tutorScr:AddHelpArrow(530, 710, 90);
        tutorScr:AddHelpArrow(805, 320, 0);
        tutorScr:SetForcedObjectName('Gear_5');
        tutorScr:SetConfirmCallback(ShowDoorSparkleTip);
        
        local ef = CreateAnimationEffector();
        ef:SetKey(_TRACK_S, 0.0, 1.0);
        ef:SetKey(_TRACK_S, 0.5, 1.5);
        ef:SetKey(_TRACK_S, 1.0, 1.0);
        ef:SetMode(_ANIM_LOOP);
        ef:AttachTo(play_field:GetObject('Gear_5'));
        ef:Play();
        
        tutorScr:Show();    
    end
    
end

--------------------------------------------------------------------------------
function ShowDoorSparkleTip()

    if not HasGameState(HELP_SHOWN, 'DOOR_SPARKLE_TIP') and InteractiveHelpBeingShown()
    then
    
        play_field.mouseDownBlocked = true;
            
        TimerDoString(
            "play_field.mouseDownBlocked = false;"..
            "local tutorScr = TutorialScreen:New();"..
            "tutorScr:SetTipAlias('DOOR_SPARKLE_TIP');"..
            "tutorScr:SetContinueAlias('CLICK_CABINET_DOOR');"..
            "tutorScr:SetPos(350, 450);"..
            "tutorScr:AddHelpArrow(570, 200, -45);"..
            "tutorScr:SetForcedObjectName('02_closed_door');"..
            "tutorScr:SetConfirmCallback(ShowHintTip);"..
            
            "tutorScr:Show();", 3., play_field:GetTimer());
    end

end

--------------------------------------------------------------------------------
function ShowHintTip()
    if not HasGameState(HELP_SHOWN, 'HINT_TIP') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetPos(500, 300);
        tutorScr:SetTipAlias('HINT_TIP');
        tutorScr:AddHelpArrow(100, 668, 135);
        
        tutorScr:Show();
    end
end

--------------------------------------------------------------------------------
function ShowLeaveHiddenObjectTip()

    local tutorScr = TutorialScreen:New();
    tutorScr:SetTipAlias('LEAVE_HO_TIP');
    tutorScr:SetPos(512, 360);
    tutorScr:AddHelpArrow(475, 680, 90);
    tutorScr:SetConfirmCallback(HideCurrentHintTip);
    tutorScr:AddCursor('CursorDoorDown', 475, 710);
    
    tutorScr:Show();

end

--------------------------------------------------------------------------------
function ShowActiveZonesTip()

    if not HasGameState(HELP_SHOWN, 'ACTIVE_ZONE_TIP') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('ACTIVE_ZONE_TIP');
        tutorScr:SetContinueAlias('CLICK_SPIDER');
        tutorScr:SetPos(400, 300);
        tutorScr:AddHelpArrow(780, 540, 45);
        tutorScr:SetForcedObjectName('SpiderMan');
        
        tutorScr:Show();
    end
end

--------------------------------------------------------------------------------
function ShowObjectOnLocTip()

    if not HasGameState(HELP_SHOWN, 'OBJECT_ON_LOC_TIP') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('OBJECT_ON_LOC_TIP');
        tutorScr:SetContinueAlias('CLICK_KEY');
        tutorScr:SetPos(285, 490);
        tutorScr:AddHelpArrow(645, 365, -45);
        tutorScr:SetForcedObjectName('Key');
        
        tutorScr:Show();
    end

end

--------------------------------------------------------------------------------
function ShowTakeItemTip()

    if not HasGameState(HELP_SHOWN, 'TAKE_ITEM_TIP') and InteractiveHelpBeingShown()
    then
    
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('TAKE_ITEM_TIP');
        tutorScr:SetContinueAlias('CLICK_TOOLS_BUTTON');
        tutorScr:SetPos(500, 300);
        tutorScr:AddHelpArrow(100, 668, 135);
        --tutorScr:SetConfirmCallback(HideCurrentHintTip);
        
        tutorScr:Show();
        tutorScr.clickToCtn:SetRect(10, 685, 95, 65);
        play_field:GetLevelScreen():GetPanel():Go();
    
    end

end

--------------------------------------------------------------------------------
function ShowUseItemTip(x, y, targetName)

    if not HasGameState(HELP_SHOWN, 'USE_ITEM_TIP') and InteractiveHelpBeingShown()
    then
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('USE_ITEM_TIP');
        tutorScr:SetPos(500, 300);
        tutorScr:AddHelpArrow(100, 668, 135);
        tutorScr:SetConfirmCallback(HideCurrentHintTip);
        
        tutorScr:Show();
    end

end

--------------------------------------------------------------------------------
function TutorialToolTaken(toolName)

    if not HasGameState(HELP_SHOWN, 'USE_ITEM_TIP') and InteractiveHelpBeingShown()
    then
        local x = 300;
        local y = 300;
        local arrX = 512;
        local arrY = 384;
        local arrAngle = 0;
        local targetObj = '';
        local ctnAlias = 'CLICK_TO_CONTINUE';

        if toolName == '03_key' and game_state.box02ZoneOpen
        then
            x = 350;
            y = 250;
            arrX = 555;
            arrY = 495;
            arrAngle = 45;
            targetObj = 'area_lock'
            ctnAlias = 'CLICK_JEWEL_BOX'
        elseif toolName == '03_hand' and game_state.doll03ZoneOpen
        then
            x = 735;
            y = 510;
            arrX = 575;
            arrY = 245;
            arrAngle = -135;
            targetObj = 'area_zoom_doll'
            ctnAlias = 'CLICK_PUPPET'
        else
            return;
        end
        
        local tutorScr = TutorialScreen:New();
        tutorScr:SetTipAlias('USE_ITEM_TIP');
        tutorScr:SetContinueAlias(ctnAlias);
        tutorScr:SetForcedObjectName(targetObj);
        tutorScr:SetPos(x, y);
        tutorScr:AddHelpArrow(arrX, arrY, arrAngle);
        
        tutorScr:Show();
        
        play_field:GetLevelScreen():GetInventory():Go();
        SetCursor('CursorTool', true);
    
    end

end