package com.alexstyl.specialdates.events.namedays.calendar.resource;

import com.alexstyl.specialdates.date.Date;
import com.alexstyl.specialdates.events.namedays.NameCelebrations;
import com.alexstyl.specialdates.events.namedays.NamedayBundle;
import com.alexstyl.specialdates.events.namedays.NamedayLocale;

import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;

import static com.alexstyl.specialdates.date.DateConstants.*;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.fail;

public class NamedayJSONParserTest {

    private NamedayJSON namedayJSON;

    @Before
    public void setUp() throws JSONException {
        JavaJSONResourceLoader resourceLoader = new JavaJSONResourceLoader();
        NamedayJSONResourceProvider resourceProvider = new NamedayJSONResourceProvider(resourceLoader);
        namedayJSON = resourceProvider.getNamedayJSONFor(NamedayLocale.gr);

    }

    @Test
    public void returningBundleHasNames() {
        NamedayBundle namedayBundle = NamedayJSONParser.getNamedaysFrom(namedayJSON);
        assertThat(namedayBundle.getNames()).isNotEmpty();
    }

    @Test
    public void alexandrosNamedayIsReturnedCorrectly() {
        NamedayBundle namedayBundle = NamedayJSONParser.getNamedaysFrom(namedayJSON);
        NameCelebrations dates = namedayBundle.getDatesFor("Αλέξανδρος");
        assertThatContainsDate(dates, Date.on(30, AUGUST));
    }

    @Test
    public void davidNamedayIsReturnedCorrectly() {
        NamedayBundle namedayBundle = NamedayJSONParser.getNamedaysFrom(namedayJSON);
        NameCelebrations dates = namedayBundle.getDatesFor("Δαβίδ");
        assertThatContainsDate(dates, Date.on(26, JUNE));
    }

    @Test
    public void magdoulaNamedayIsReturnedCorrectly() {
        NamedayBundle namedayBundle = NamedayJSONParser.getNamedaysFrom(namedayJSON);
        NameCelebrations dates = namedayBundle.getDatesFor("Αμαλία");
        assertThatContainsDate(dates, Date.on(10, JULY));
    }

    private void assertThatContainsDate(NameCelebrations celebrations, Date date) {
        boolean found = false;
        for (int i = 0; i < celebrations.getDates().size(); i++) {
            Date date1 = celebrations.getDate(i);
            if (date1.equals(date)) {
                found = true;
                break;
            }
        }
        if (!found) {
            fail("Couldn't find date " + date + " inside " + celebrations);
        }
    }
}

@@ -107,16 +107,61 @@
 e.Date;%0A
+import com.alexstyl.specialdates.date.Dates;%0A
 import c
@@ -1820,15 +1820,13 @@
 oid 
+a
 ma
-gdou
 l
+i
 aNam
@@ -2086,16 +2086,23 @@
 private 
+static 
 void ass
@@ -2177,29 +2177,45 @@
     
-boolean found = false
+Dates dates = celebrations.getDates()
 ;%0A  
@@ -2224,63 +2224,58 @@
     
-for (
 int i
+ndex
  = 0;
- i %3C celebrations.getD
+%0A        while (index %3C d
 ates
-()
 .size()
-; i++
 ) %7B%0A
@@ -2290,21 +2290,12 @@
     
-Date date1 = 
+if (
 cele
@@ -2316,32 +2316,13 @@
 te(i
-);%0A            if (date1
+ndex)
 .equ
@@ -2354,20 +2354,14 @@
     
-found = true
+return
 ;%0A  
@@ -2374,18 +2374,9 @@
     
-    break;
+%7D
 %0A   
@@ -2376,33 +2376,40 @@
   %7D%0A            
-%7D
+index++;
 %0A        %7D%0A     
@@ -2407,34 +2407,9 @@
   %7D%0A
-        if (!found) %7B%0A    
+%0A
     
@@ -2476,26 +2476,16 @@
 tions);%0A
-        %7D%0A
     %7D%0A%7D%0A

Simplify assert method