package com.knecon.fforesight.service.viewerdoc.model;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;

import com.knecon.fforesight.service.viewerdoc.LayerIdentifier;

public class MarkedContentStack {

    private final Deque<MarkedContent> stack = new LinkedList<>();


    public void enterMarkedContent(String name) {

        stack.push(new MarkedContent(name));
    }


    public void leaveMarkedContent() {

        stack.pop();
    }


    public String currentMarkedContent() {

        if (stack.isEmpty()) {
            return "";
        }
        return stack.peek().name();
    }


    public boolean currentMarkedContentContains(String name) {

        Iterator<MarkedContent> markedContentIterator = stack.descendingIterator();
        while (markedContentIterator.hasNext()) {
            var markedContent = markedContentIterator.next();
            if (markedContent.name().equals(name)) {
                return true;
            }
        }
        return false;
    }


    public boolean currentMarkedContentContainsNone(Set<String> names) {

        if (stack.isEmpty()) {
            return true;
        }
        Iterator<MarkedContent> markedContentIterator = stack.descendingIterator();
        while (markedContentIterator.hasNext()) {
            var markedContent = markedContentIterator.next();
            if (names.contains(markedContent.name())) {
                return false;
            }
        }
        return true;
    }


    public boolean currentMarkedContentIsKneconContent() {

        if (stack.isEmpty()) {
            return false;
        }
        Iterator<MarkedContent> markedContentIterator = stack.descendingIterator();
        while (markedContentIterator.hasNext()) {
            var markedContent = markedContentIterator.next();
            if (markedContent.name().startsWith(LayerIdentifier.KNECON_IDENTIFIER_PREFIX)) {
                return true;
            }
        }
        return false;

    }


    public void clear() {

        stack.clear();
    }


    private record MarkedContent(String name) {

    }

}