/**
 * This file is part of the Meeds project (https://meeds.io/).
 *
 * Copyright (C) 2020 - 2025 Meeds Association contact@meeds.io
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.gatein.api.navigation;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.exoplatform.portal.mop.NodeTarget;
import org.exoplatform.portal.mop.SiteKey;
import org.exoplatform.portal.mop.SiteType;
import org.exoplatform.portal.mop.Visibility;
import org.exoplatform.portal.mop.navigation.NodeState;
import org.exoplatform.portal.mop.navigation.Scope.Visitor;
import org.exoplatform.portal.mop.navigation.VisitMode;
import org.exoplatform.portal.mop.page.PageKey;
import org.junit.Test;

public class NodeVisitorScopeTest {
    @Test
    public void nodePathScope() {
        NodeState nodeState = new NodeState("label", "icon", -1, -1, Visibility.DISPLAYED, new PageKey(new SiteKey(
                SiteType.PORTAL, "site"), "page"), new SiteKey(SiteType.PORTAL, "site"), NodeTarget.NEW_TAB.name(), System.currentTimeMillis());
        NodeVisitorMock mock = new NodeVisitorMock();
        Visitor visitor = new NodeVisitorScope(mock).get();

        mock.instrument(true);
        assertEquals(VisitMode.ALL_CHILDREN, visitor.enter(0, "id", "default", nodeState));
        assertNull(mock.name);
        assertNull(mock.details);

        mock.instrument(true);
        assertEquals(VisitMode.ALL_CHILDREN, visitor.enter(1, "id", "1", nodeState));
        assertNotNull(mock.name);
        assertEquals(1, mock.depth);
        assertEquals(NodePath.path("1"), mock.details.getNodePath());

        mock.instrument(false);
        assertEquals(VisitMode.NO_CHILDREN, visitor.enter(2, "id", "1-1", nodeState));
        assertNotNull(mock.name);
        assertEquals(2, mock.depth);
        assertEquals(NodePath.path("1", "1-1"), mock.details.getNodePath());

        visitor.leave(2, "id", "1-1", nodeState);
        visitor.leave(1, "id", "1", nodeState);
        visitor.leave(0, "id", "default", null);

        mock.instrument(false);
    }

    class NodeVisitorMock implements NodeVisitor {
        private int depth;

        private boolean visit;

        private String name;

        private NodeDetails details;

        @Override
        public boolean visit(int depth, String name, NodeDetails details) {
            this.depth = depth;
            this.name = name;
            this.details = details;
            return visit;
        }

        public void instrument(boolean visit) {
            this.visit = visit;
            name = null;
            depth = -1;
        }
    }
}
