package ch.zhaw.ads;

import java.util.Map;
import static java.util.Map.entry;    

public class BracketServer implements CommandExecutor {

    @Override
    public String execute(String command) {
        StringBuffer result = new StringBuffer(100);
        result.append("Die Eingabe war \"");
        result.append(command);
        result.append("\"\n");
        return result.toString();
    }

    private Map<Character, Character> openingBrackets = Map.ofEntries(
            entry('(', ')'),
            entry('[', ']'),
            entry('{', '}'),
            entry('<', '>')
            );

    private Map<Character, Character> closingBrackets = Map.ofEntries(
            entry(')', '('),
            entry(']', '['),
            entry('}', '{'),
            entry('>', '<')
            );

    public boolean checkBrackets(String input) {
        ListStack<Character> bracketStack = new ListStack<>();
        for (int i = 0; i < input.length(); i++) {
            char cur_char = input.charAt(i);
            if (openingBrackets.containsKey(cur_char)) {
                bracketStack.push(cur_char);
            }
            if (closingBrackets.containsKey(cur_char)) {
                if (bracketStack.isEmpty()) {
                    return false;
                }
                Character last = (Character) bracketStack.pop();
                if (last != closingBrackets.get(cur_char)) {
                    return false;
                }
            }
        }
        return bracketStack.isEmpty();
    }

    // search bracket from start, then remove matching bracket from the end of the string. then remove opening bracket.
}
