/* LanguageTool, a natural language style checker 
 * Copyright (C) 2012 Daniel Naber (http://www.danielnaber.de)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */
package org.languagetool.rules.de;

import org.junit.Test;
import org.languagetool.AnalyzedSentence;
import org.languagetool.JLanguageTool;
import org.languagetool.Languages;
import org.languagetool.TestTools;
import org.languagetool.language.German;
import org.languagetool.language.GermanyGerman;
import org.languagetool.rules.RuleMatch;
import org.languagetool.rules.spelling.hunspell.HunspellRule;

import java.io.IOException;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

public class GermanSpellerRuleTest {

  private static final German GERMAN_DE = (German) Languages.getLanguageForShortCode("de-DE");

  @Test
  public void testErrorLimitReached() throws IOException {
    HunspellRule rule1 = new GermanSpellerRule(TestTools.getMessages("de"), GERMAN_DE);
    JLanguageTool lt = new JLanguageTool(GERMAN_DE);
    RuleMatch[] matches1 = rule1.match(lt.getAnalyzedSentence("Ein schöner Satz."));
    assertThat(matches1.length, is(0));
    RuleMatch[] matches2 = rule1.match(lt.getAnalyzedSentence("But this is English."));
    assertThat(matches2.length, is(4));
    assertNull(matches2[0].getErrorLimitLang());
    assertNull(matches2[1].getErrorLimitLang());
    assertThat(matches2[2].getErrorLimitLang(), is("en"));
    RuleMatch[] matches3 = rule1.match(lt.getAnalyzedSentence("Und er sagte, this is a good test."));
    assertThat(matches3.length, is(4));
    assertNull(matches3[3].getErrorLimitLang());
  }

  @Test
  // case: signature is (mostly) English, user starts typing in German -> first, EN is detected for whole text
  // Also see MorfologikAmericanSpellerRuleTest
  public void testMultilingualSignatureCase() throws IOException {
    JLanguageTool lt = new JLanguageTool(GERMAN_DE);
    HunspellRule rule = new GermanSpellerRule(TestTools.getMessages("de"), GERMAN_DE);
    String sig = "-- " +
            "Das ist eine deutsche Signatur.\n" +
            "Eigentlich egal, was hier genau steht. Aber es reicht, um den Gesamttext als deutsch zu erkennen.\n";
    //assertZZ(lt, rule, "Hi Tom, I'm happy to discuss the\n\n" + sig);  // "Hi Tom, I'm happy" also accepted by German speller
    //assertZZ(lt, rule, "Tom, I'm happy to discuss the\n\n" + sig);
    assertZZ(lt, rule, "Tom, could we meet next Monday\n\n" + sig);
  }

  private void assertZZ(JLanguageTool lt, HunspellRule rule, String input) throws IOException {
    List<AnalyzedSentence> analyzedSentences = lt.analyzeText(input);
    RuleMatch[] matches = rule.match(analyzedSentences.get(0));
    /*System.out.println("--> " + input);
    for (RuleMatch ruleMatch : matches) {
      System.out.println(">>>" + ruleMatch.getRule().getId() + " " + ruleMatch.getErrorLimitLang());
    }*/
    assertThat(analyzedSentences.size(), is(4));
    assertThat(matches.length, is(5));
    boolean hasErrorLimitLang = false;
    for (RuleMatch rm : matches) {
      if (rm.getErrorLimitLang() != null && rm.getErrorLimitLang().equals("en")) {
        hasErrorLimitLang = true;
        break;
      }
    }
    assertTrue("Should have at least one match with errorLimitLang == \"en\"", hasErrorLimitLang);
  }

}

@@ -1991,101 +1991,221 @@
     
-assertNull(matches2%5B0%5D.getErrorLimitLang());%0A    assertNull(matches2%5B1%5D.getErrorLimitLang());
+boolean match2Found = false;%0A    for (RuleMatch match : matches2) %7B%0A      if (match.getErrorLimitLang() != null && match.getErrorLimitLang().equals(%22en%22)) %7B%0A        match2Found = true;%0A        break;%0A      %7D%0A    %7D
 %0A   
@@ -2216,53 +2216,23 @@
 ertT
-hat
+rue
 (match
-es2%5B2%5D.getErrorLimitLang(), is(%22en%22)
+2Found
 );%0A 
@@ -2380,50 +2380,249 @@
     
-assertNull(matches3%5B3%5D.getErrorLimitLang()
+boolean match3Found = false;%0A    for (RuleMatch match : matches3) %7B%0A      if (match.getErrorLimitLang() != null && match.getErrorLimitLang().equals(%22en%22)) %7B%0A        match3Found = true;%0A        break;%0A      %7D%0A    %7D%0A    assertFalse(match3Found
 );%0A 

fix tests