package org.estatio.dom.index;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.joda.time.LocalDate;


public class IndexationCalculator {

    public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

    private Index index;
    private LocalDate baseIndexStartDate;
    private LocalDate baseIndexEndDate;
    private LocalDate nextIndexStartDate;
    private LocalDate nextIndexEndDate;
    private BigDecimal baseIndexValue;
    private BigDecimal nextIndexValue;
    private BigDecimal indexationFactor;
    private BigDecimal rebaseFactor;
    private BigDecimal baseValue;
    private BigDecimal indexedValue;
    private BigDecimal indexationPercentage;

    public IndexationCalculator(Index index, LocalDate baseIndexStartDate, LocalDate baseIndexEndDate, LocalDate nextIndexStartDate, LocalDate nextIndexEndDate, BigDecimal baseValue) {
        super();
        this.index = index;
        this.baseIndexStartDate = baseIndexStartDate;
        this.baseIndexEndDate = baseIndexEndDate;
        this.nextIndexStartDate = nextIndexStartDate;
        this.nextIndexEndDate = nextIndexEndDate;
        this.baseValue = baseValue;
    }

    public BigDecimal getBaseIndexValue() {
        return baseIndexValue;
    }

    public BigDecimal getNextIndexValue() {
        return nextIndexValue;
    }

    public BigDecimal getIndexationFactor() {
        return indexationFactor;
    }

    public BigDecimal getIndexedValue() {
        return indexedValue;
    }

    public BigDecimal getIndexationPercentage() {
        return indexationPercentage;
    }
    
    public void calculate(Indexable input) {
        this.calculate();
        input.setBaseIndexValue(baseIndexValue);
        input.setNextIndexValue(nextIndexValue);
        input.setIndexationPercentage(indexationPercentage);
        input.setIndexedValue(indexedValue);
    }        

    public void calculate() {
        index.initialize(this, baseIndexStartDate, nextIndexStartDate);
        if (this.baseIndexValue != null && this.nextIndexValue !=null){
            indexationFactor = nextIndexValue.divide(baseIndexValue, 5, RoundingMode.HALF_UP).multiply(rebaseFactor).setScale(2, RoundingMode.HALF_UP);
            indexationPercentage = indexationFactor.subtract(BigDecimal.ONE).multiply(ONE_HUNDRED).setScale(0);
            indexedValue = baseValue.multiply(indexationFactor);
        }
    }
    public void setBaseIndexValue(BigDecimal baseIndexValue) {
        this.baseIndexValue = baseIndexValue;
    }
    public void setNextIndexValue(BigDecimal nextIndexValue) {
        this.nextIndexValue = nextIndexValue;
    }
    public void setRebaseFactor(BigDecimal rebaseFactor) {
        this.rebaseFactor = rebaseFactor;
    }
    
}

@@ -54,16 +54,46 @@
 ecimal;%0A
+import java.math.MathContext;%0A
 import j
@@ -1622,21 +1622,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     publ
@@ -1903,24 +1903,16 @@
 );%0A    %7D
-        
 %0A%0A    pu
@@ -1931,24 +1931,56 @@
 lculate() %7B%0A
+        if (index != null)%7B%0A    
         inde
@@ -2035,24 +2035,28 @@
 StartDate);%0A
+    
         if (
@@ -2119,32 +2119,36 @@
 l)%7B%0A            
+   
+ 
 indexationFactor
@@ -2192,9 +2192,9 @@
 ue, 
-5
+4
 , Ro
@@ -2249,9 +2249,9 @@
 ale(
-2
+3
 , Ro
@@ -2275,32 +2275,36 @@
 P);%0A            
+  
+  
 indexationPercen
@@ -2310,16 +2310,17 @@
 ntage = 
+(
 indexati
@@ -2352,16 +2352,17 @@
 mal.ONE)
+)
 .multipl
@@ -2385,17 +2385,17 @@
 etScale(
-0
+1
 );%0A     
@@ -2397,24 +2397,28 @@
             
+    
 indexedValue
@@ -2456,17 +2456,65 @@
 nFactor)
-;
+.setScale(2, RoundingMode.HALF_UP);%0A            %7D
 %0A       
@@ -2518,24 +2518,25 @@
     %7D%0A    %7D%0A
+%0A
     public v
@@ -2630,32 +2630,33 @@
 dexValue;%0A    %7D%0A
+%0A
     public void 
@@ -2750,24 +2750,25 @@
 alue;%0A    %7D%0A
+%0A
     public v
@@ -2862,15 +2862,11 @@
 ;%0A    %7D%0A
-    
 %0A%7D%0A

fix rounding issue