/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expressionevaluation;

import expressiontree.operators.Operator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextPane;
import javax.swing.text.StyledDocument;
import org.apfloat.Apfloat;
import org.apfloat.ApfloatRuntimeException;

/**
 *
 * @author Esta
 */
public class ViewHelper {

    public int[] findPairOfBracket(String s, int pos) {
        int[] ret = new int[]{0, 0};
        StringBuilder sb = new StringBuilder(s);
        int max = 0;
        int deep = 0;
        String opening = "([{";
        String closing = ")]}";

        StringBuilder stack = new StringBuilder();
        //nyitó
        if (pos > 0 && pos <= sb.length() && opening.contains("" + sb.charAt(pos - 1))) {
            for (int i = 0; i < sb.length(); i++) {
                String c = "" + sb.charAt(i);
                if (i == pos - 1) {
                    deep = stack.length();
                    ret[0] = i;
                }
                if (opening.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (closing.contains(c) && stack.length() == 0) {
                    ret[0] = pos - 1;
                    return ret;
                } else if (closing.contains(c) && closing.indexOf(c) == opening.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == deep && i >= pos) {
                    ret[1] = i;
                    return ret;
                } else if (stack.length() == deep && max > 0) {
                    ret[0] = -pos + 1;
                    return ret;
                }
            }
            if (stack.length() != deep) {
                ret[0] = -pos + 1;
                return ret;
            }
        } else if (pos >= 0 && pos < sb.length() && opening.contains("" + sb.charAt(pos))) {
            for (int i = 0; i < sb.length(); i++) {
                String c = "" + sb.charAt(i);
                if (i == pos) {
                    deep = stack.length();
                    ret[0] = i;
                }
                if (opening.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (closing.contains(c) && stack.length() == 0) {
                    ret[0] = pos;
                    return ret;
                } else if (closing.contains(c) && closing.indexOf(c) == opening.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == deep && i >= pos) {
                    ret[1] = i;
                    return ret;
                } else if (stack.length() == deep && max > 0) {
                    ret[0] = -pos;
                    return ret;
                }
            }
            if (stack.length() != deep) {
                ret[0] = -pos;
                return ret;
            }
        } //záró
        else if (pos >= 0 && pos < sb.length() && closing.contains("" + sb.charAt(pos))) {
            int length = 0;
            for (int i = 0; i < sb.length(); i++) {
                String c = "" + sb.charAt(i);
                if (opening.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (stack.length() > 0 && closing.contains(c) && closing.indexOf(c) == opening.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == 0 && max > 0) {
                    length = i;
                    break;
                }
            }
            max = 0;
            ret[0] = s.length();
            ret[1] = s.length();
            if (pos > length) {
                ret[0] = -pos;
                return ret;
            }
            for (int i = length; i >= 0; i--) {
                String c = "" + sb.charAt(i);
                if (i == pos) {
                    deep = stack.length();
                    ret[0] = i;
                }
                if (closing.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (opening.contains(c) && stack.length() == 0) {
                    ret[0] = pos;
                    return ret;
                } else if (opening.contains(c) && opening.indexOf(c) == closing.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == deep && i <= pos) {
                    ret[1] = i;
                    return ret;
                } else if (stack.length() == deep && max > 0) {
                    ret[0] = -pos;
                    return ret;
                }
            }
            if (stack.length() != deep) {
                ret[0] = -pos;
                return ret;
            }
        } else if (pos > 0 && pos <= sb.length() && closing.contains("" + sb.charAt(pos - 1))) {
            int length = 0;
            for (int i = 0; i < sb.length(); i++) {
                String c = "" + sb.charAt(i);
                if (opening.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (stack.length() > 0 && closing.contains(c) && closing.indexOf(c) == opening.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == 0 && max > 0) {
                    length = i;
                    break;
                }
            }
            max = 0;
            ret[0] = s.length();
            ret[1] = s.length();
            if (pos - 1 > length) {
                ret[0] = -pos + 1;
                return ret;
            }
            for (int i = length; i >= 0; i--) {
                String c = "" + sb.charAt(i);
                if (i == pos - 1) {
                    deep = stack.length();
                    ret[0] = i;
                }
                if (closing.contains(c)) {
                    stack.append(c);
                    if (stack.length() > max) {
                        max = stack.length();
                    }
                } else if (opening.contains(c) && stack.length() == 0) {
                    ret[0] = pos - 1;
                    return ret;
                } else if (opening.contains(c) && opening.indexOf(c) == closing.indexOf(stack.charAt(stack.length() - 1))) {
                    stack.deleteCharAt(stack.length() - 1);
                }
                if (stack.length() == deep && i <= pos) {
                    ret[1] = i;
                    return ret;
                } else if (stack.length() == deep && max > 0) {
                    ret[0] = -pos + 1;
                    return ret;
                }
            }
            if (stack.length() != deep) {
                ret[0] = -pos + 1;
                return ret;
            }
        }
        return ret;
    }

    void highlight(StyledDocument doc, JTextPane inputField) {
        String text = inputField.getText();
        Pattern p = Pattern.compile("(^|[ \\(\\)\\{\\}\\[\\]]?)(([^ ,\\|\\(\\)\\{\\}\\[\\]])+)([\\(\\)\\{\\}\\[\\] ]|$)?");
        Matcher m = p.matcher(text);
        int last=0;
        while (m.find(last)) {
            last=m.end();
            String s=m.group(2);
            if (Operator.UNARY.contains(s)) {
                doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Operator"), true);
            } else if (Operator.BINARY.contains(s)) {
                doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Operator"), true);
            } else if (Operator.OTHER.contains(s)) {
                doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Special"), true);
            } else if (s.equalsIgnoreCase("VAR") || s.equalsIgnoreCase("pi")) {
                doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Special"), true);
            } else {
                try {
                    new Apfloat(s);
                    doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Number"), true);
                } catch (NumberFormatException numberFormatException) {
                    doc.setCharacterAttributes(m.start()+m.group(1).length(), s.length(), inputField.getStyle("Unknown"), true);
                } catch (ApfloatRuntimeException apfloatRuntimeException) {
                }
            } 
        }
        
    }
}
