package com.github.aureliano.damihilogs.matcher;

import java.util.regex.Pattern;

import com.github.aureliano.damihilogs.exception.DaMihiLogsException;
import com.github.aureliano.damihilogs.helper.StringHelper;

public class MultiLineMatcher implements IMatcher {

	private String prefixRegex;
	private String partialRegex;
	private String suffixRegex;
	
	private int maxMatchAttempts;
	private int matchAttemptsCounter;
	
	private Pattern pattern;
	private String oldEntry;
	
	public MultiLineMatcher(String prefixRegex) {
		this(prefixRegex, null);
	}
	
	public MultiLineMatcher(String prefixRegex, String partialRegex) {
		this(prefixRegex, partialRegex, DEFAULT_MAX_MATCH_ATTEMPTS);
	}
	
	public MultiLineMatcher(String prefixRegex, String partialRegex, int maxMatchAttempts) {
		this.prefixRegex = prefixRegex;
		this.partialRegex = partialRegex;
		
		if (this.partialRegex == null) {
			this.partialRegex = "";
		}
		
		if (StringHelper.isEmpty(this.prefixRegex)) {
			throw new DaMihiLogsException("Regular expression must be passed to " + this.getClass().getSimpleName());
		}
		
		this.suffixRegex = String.format("([\\W\\w\\d](?!%s))+", this.prefixRegex);
		this.pattern = Pattern.compile(this.prefixRegex + this.partialRegex);
		this.matchAttemptsCounter = 0;
		this.maxMatchAttempts = maxMatchAttempts;
	}

	@Override
	public boolean partialMatch(String text) {
		if (this.matchAttemptsCounter >= this.maxMatchAttempts) {
			throw new DaMihiLogsException("Max parse attempts overflow (" + this.matchAttemptsCounter + " >= " + this.maxMatchAttempts + ").");
		}
		
		this.matchAttemptsCounter++;
		return this.pattern.matcher(text).find();
	}

	@Override
	public boolean matches(String text) {
		boolean matches = text.matches(this.prefixRegex + this.partialRegex + this.suffixRegex);
		if (!matches) {
			this.matchAttemptsCounter = 0;
		}
		
		return matches;
	}

	@Override
	public String endMatch(String text) {
		boolean partialMatch = this.partialMatch(text);
		boolean match = this.matches(text);
		
		if (partialMatch && !match) {
			this.matchAttemptsCounter = 0;
			return oldEntry;
		} else {
			this.oldEntry = text;
			return null;
		}
	}

	@Override
	public boolean isMultiLine() {
		return true;
	}
}
@@ -301,31 +301,29 @@
 vate String 
-partial
+midle
 Regex;%0A%09priv
@@ -733,31 +733,29 @@
 gex, String 
-partial
+midle
 Regex, int m
@@ -814,31 +814,29 @@
 %0A%09%09this.
-partial
+midle
 Regex = 
 partialR
@@ -827,23 +827,21 @@
 Regex = 
-partial
+midle
 Regex;%0A%09
@@ -849,31 +849,29 @@
 %0A%09%09if (this.
-partial
+midle
 Regex == nul
@@ -879,31 +879,29 @@
 ) %7B%0A%09%09%09this.
-partial
+midle
 Regex = %22%22;%0A
@@ -1202,31 +1202,29 @@
 egex + this.
-partial
+midle
 Regex);%0A%09%09th
@@ -1743,23 +1743,21 @@
  + this.
-partial
+midle
 Regex + 

Rename class property.