/*
 * Copyright © 2015 Stefan Niederhauser (nidin@gmx.ch)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package guru.nidi.codeassert.pmd;

import org.hamcrest.*;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static java.lang.System.lineSeparator;

public class RegexMatcher extends TypeSafeMatcher<String> {
    private final List<Pattern> patterns;

    public RegexMatcher(List<Pattern> patterns) {
        this.patterns = patterns;
    }

    public static Matcher<String> matchesFormat(String format) {
        final List<Pattern> patterns = new ArrayList<>();
        for (final String line : format.split(lineSeparator())) {
            patterns.add(Pattern.compile("\\Q" + line.replace("%d", "\\E\\d+\\Q").replace("\\", "\\\\") + "\\E"));
        }
        return new RegexMatcher(patterns);
    }

    @Override
    protected boolean matchesSafely(String item) {
        final String[] lines = item.split(lineSeparator());
        if (patterns.size() != lines.length) {
            return false;
        }
        for (int i = 0; i < lines.length; i++) {
            if (!patterns.get(i).matcher(lines[i]).matches()) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void describeTo(Description description) {
        description.appendText(String.format("matches pattern%n"));
        for (final Pattern p : patterns) {
            description.appendText(pattern(p)).appendText(lineSeparator());
        }
    }

    @Override
    protected void describeMismatchSafely(String item, Description description) {
        description.appendText(String.format("was%n%s%n", item));
        final String[] lines = item.split(lineSeparator());
        int i;
        for (i = 0; i < lines.length && i < patterns.size(); i++) {
            if (!patterns.get(i).matcher(lines[i]).matches()) {
                description.appendText(String.format("At line %d expected:%n%s%nbut got%n%s",
                        i + 1, pattern(patterns.get(i)), lines[i]));
                return;
            }
        }
        if (lines.length > i) {
            description.appendText("expected no more lines, but got " + (lines.length - i) + " more.");
        } else {
            description.appendText("expected " + (patterns.size() - i) + " more lines, but got none.");
        }
    }

    private String pattern(Pattern p) {
        return p.pattern().replace("\\Q", "").replace("\\E", "").replace("\\d+", "%d").replace("\\\\", "\\");
    }
}

@@ -1231,20 +1231,27 @@
 ile(
-%22%5C%5CQ%22 + 
+Pattern.quote(
 line
+)
 .rep
@@ -1278,38 +1278,8 @@
 %5CQ%22)
-.replace(%22%5C%5C%22, %22%5C%5C%5C%5C%22) + %22%5C%5CE%22
 ));%0A
@@ -2949,13 +2949,16 @@
 (%22%5C%5C
-Q
+d+
 %22, %22
+%25d
 %22).r
@@ -2971,35 +2971,65 @@
 (%22%5C%5C
-E
+Q
 %22, %22%22)
-.replace(%22%5C%5Cd+
+%0A                .replaceAll(%22(%5B%5E%5C%5C%5C%5C%5D)%5C%5C%5C%5CE
 %22, %22
-%25d
+$1
 %22).r
@@ -3040,22 +3040,24 @@
 ce(%22%5C%5C%5C%5C
+E
 %22, %22%5C%5C
+E
 %22);%0A    

support windows paths in RegexMatcher