/*
 * Copyright (c) 2014, Araz Abishov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.dhis2.mobile.utils.date.iterators;

import org.dhis2.mobile.utils.date.DateHolder;
import org.joda.time.LocalDate;

import java.util.ArrayList;
import java.util.Collections;

public class FinOctYearIterator extends YearIterator {
    private static final String OCTOBER = "Oct";

    public FinOctYearIterator(int openFP) {
        super(openFP);
        openFuturePeriods = openFP;
        cPeriod = new LocalDate(currentDate.getYear(), OCT, 31);
        checkDate = new LocalDate(cPeriod);
        maxDate = new LocalDate(currentDate.getYear(), SEP, 1);
        for (int i = 0; i < openFuturePeriods; i++) {
            maxDate = maxDate.plusYears(1);
        }
    }

    @Override
    protected boolean hasNext(LocalDate date) {
        if (openFuturePeriods > 0) {
            return checkDate.isBefore(maxDate);
        } else {
            LocalDate sep = new LocalDate(date.getYear(), SEP, 30);
            return currentDate.isAfter(sep);
        }
    }

    @Override
    public ArrayList<DateHolder> current() {
        if (!hasNext()) {
            return previous();
        } else {
            return generatePeriod();
        }
    }

    @Override
    protected ArrayList<DateHolder> generatePeriod() {
        ArrayList<DateHolder> dates = new ArrayList<DateHolder>();
        int counter = 0;
        checkDate = new LocalDate(cPeriod);
        LocalDate sep = new LocalDate(checkDate.getYear(), SEP, 30);

        while ((openFuturePeriods > 0 || currentDate.isAfter(sep)) && counter < 10) {
            String dateStr = checkDate.minusYears(1).year().getAsString();
            String label = String.format(FIN_DATE_LABEL_FORMAT, OCT_STR, dateStr, SEP_STR, checkDate.year().getAsString());
            String date = dateStr + OCTOBER;

            if(checkDate.isBefore(maxDate)) {
                DateHolder dateHolder = new DateHolder(date, checkDate.toString(), label);
                dates.add(dateHolder);
            }

            checkDate = checkDate.plusYears(1);
            counter++;
        }

        Collections.reverse(dates);
        return dates;
    }
}

@@ -1847,17 +1847,16 @@
  %22Oct%22;%0A
-%0A
     publ
@@ -2010,17 +2010,16 @@
 ), OCT, 
-3
 1);%0A    
@@ -2118,20 +2118,59 @@
 ), SEP, 
-1
+30
 );%0A
+        if(openFuturePeriods%3E0) %7B%0A    
         
@@ -2219,32 +2219,36 @@
 ) %7B%0A            
+   
+ 
 maxDate = maxDat
@@ -2255,32 +2255,46 @@
 e.plusYears(1);%0A
+            %7D%0A
         %7D%0A    %7D%0A
@@ -2290,38 +2290,24 @@
    %7D%0A    %7D%0A%0A
-    @Override%0A
     protecte
@@ -2458,32 +2458,193 @@
       LocalDate 
+sep;%0A            if(date.getMonthOfYear()%3E=10) %7B%0A                sep = new LocalDate(date.getYear()+1, SEP, 30);%0A            %7D%0A            else%7B%0A                
 sep = new LocalD
@@ -2665,32 +2665,46 @@
 ar(), SEP, 30);%0A
+            %7D%0A
             retu
@@ -3159,16 +3159,78 @@
 calDate 
+sep;%0A%0A        if(checkDate.getMonthOfYear()%3E=9) %7B%0A            
 sep = ne
@@ -3252,32 +3252,34 @@
 ckDate.getYear()
++1
 , SEP, 30);%0A%0A   
@@ -3274,16 +3274,112 @@
 P, 30);%0A
+        %7D%0A        else%7B%0A            sep = new LocalDate(checkDate.getYear(), SEP, 30);%0A        %7D
 %0A       
@@ -3435,16 +3435,30 @@
 fter(sep
+.minusYears(1)
 )) && co
@@ -3475,83 +3475,8 @@
 ) %7B%0A
-            String dateStr = checkDate.minusYears(1).year().getAsString();%0A
     
@@ -3544,23 +3544,46 @@
 CT_STR, 
-dateStr
+checkDate.year().getAsString()
 , SEP_ST
@@ -3587,32 +3587,45 @@
 _STR, checkDate.
+plusYears(1).
 year().getAsStri
@@ -3661,15 +3661,38 @@
 e = 
-dateStr
+checkDate.year().getAsString()
  + O
@@ -4038,9 +4038,8 @@
 %0A    %7D%0A%7D
-%0A

fix finOctYear iterator