package com.apps.adrcotfas.goodtime.Statistics;

import android.widget.TextView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.List;

public class StickyDateAxisValueFormatter implements IAxisValueFormatter {

    private List<LocalDate> dates;
    private LineChart chart;
    private TextView sticky;
    private final DateTimeFormatter monthFormatter = DateTimeFormat.forPattern("MMM");

    StickyDateAxisValueFormatter(List<LocalDate> dates, LineChart chart, TextView sticky) {
        this.dates = dates;
        this.chart = chart;
        this.sticky = sticky;
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {
        // in case of invalid values
        if(value < 0) {
            return "";
        }

        // set the sticky TextView
        int firstValue = (int)chart.getXAxis().mEntries[0];
        if (firstValue < 0 || firstValue >= dates.size()) {
            return "";
        }
        LocalDate firstDate = dates.get(firstValue);
        int stickyMonth = firstDate.getMonthOfYear();
        int stickyYear = firstDate.getYear();
        final String stickyText = firstDate.toString(monthFormatter) + "\n" + stickyYear;
        sticky.setText(stickyText);

        LocalDate dateTime = dates.get((int)value);
        int dayOfMonth = dateTime.getDayOfMonth();
        int month = dateTime.getMonthOfYear();

        String ret;
        if(stickyMonth != month && dayOfMonth == 1) {
            ret = dateTime.toString(monthFormatter);
        }
        else {
            ret = Integer.toString(dayOfMonth);
        }
        return ret;
    }
}
@@ -478,24 +478,30 @@
 tter %7B%0A%0A    
+final 
 private List
@@ -508,25 +508,26 @@
 %3CLocalDate%3E 
-d
+mD
 ates;%0A    pr
@@ -538,25 +538,26 @@
 e LineChart 
-c
+mC
 hart;%0A    pr
@@ -571,22 +571,27 @@
 extView 
-s
+mS
 ticky
+Text
 ;%0A    pr
@@ -721,17 +721,18 @@
 alDate%3E 
-d
+mD
 ates, Li
@@ -739,17 +739,18 @@
 neChart 
-c
+mC
 hart, Te
@@ -756,22 +756,27 @@
 extView 
-s
+mS
 ticky
+Text
 ) %7B%0A    
@@ -788,17 +788,19 @@
 his.
-d
+mD
 ates = 
-d
+mD
 ates
@@ -818,17 +818,19 @@
 his.
-c
+mC
 hart = 
-c
+mC
 hart
@@ -848,23 +848,33 @@
 his.
-s
+mS
 ticky
+Text
  = 
-s
+mS
 ticky
+Text
 ;%0A  
@@ -962,145 +962,22 @@
 ) %7B%0A
- 
+%0A
        
-// in case of
  
+f
 in
-v
 al
-id values%0A        if(value %3C 0) %7B%0A            return %22%22;%0A        %7D%0A%0A        // set the sticky TextView%0A       
  int
@@ -995,17 +995,19 @@
  = (int)
-c
+ mC
 hart.get
@@ -1039,12 +1039,61 @@
     
-if (
+// in case of invalid values%0A        if(value %3C 0 %7C%7C 
 firs
@@ -1120,17 +1120,18 @@
 alue %3E= 
-d
+mD
 ates.siz
@@ -1170,24 +1170,25 @@
       %7D%0A
+%0A
         
 LocalDat
@@ -1171,32 +1171,73 @@
      %7D%0A%0A        
+// set the sticky TextView%0A        final 
 LocalDate firstD
@@ -1238,25 +1238,26 @@
 firstDate = 
-d
+mD
 ates.get(fir
@@ -1266,32 +1266,38 @@
 Value);%0A        
+final 
 int stickyMonth 
@@ -1330,24 +1330,30 @@
 ();%0A        
+final 
 int stickyYe
@@ -1476,22 +1476,27 @@
         
-s
+mS
 ticky
+Text
 .setText
@@ -1518,16 +1518,22 @@
         
+final 
 LocalDat
@@ -1545,17 +1545,18 @@
 eTime = 
-d
+mD
 ates.get
@@ -1573,24 +1573,30 @@
 e);%0A        
+final 
 int dayOfMon
@@ -1634,16 +1634,22 @@
         
+final 
 int mont

refactor xAxisFormatter