package net.iponweb.disthene.reader.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Andrei Ivanov
 */
public class XAxisConfigProvider {
    public static final long SEC = 1;
    public static final long MIN = 60;
    public static final long HOUR = MIN * 60;
    public static final long DAY = HOUR * 24;
    public static final long WEEK = DAY * 7;
    public static final long MONTH = DAY * 31;
    public static final long YEAR = DAY * 365;

    private static List<XAxisConfig> configs = new ArrayList<>();

    static {
        configs.add(new XAxisConfig(0, SEC, 5, MIN, 1, SEC, 5, "H:m:s", 10 * MIN));
        configs.add(new XAxisConfig(0.07, SEC, 10, MIN, 1, SEC, 10, "H:m:s", 20 * MIN));
        configs.add(new XAxisConfig(0.14, SEC, 15, MIN, 1, SEC, 15, "H:m:s", 30 * MIN));
        configs.add(new XAxisConfig(0.27, SEC, 30, MIN, 2, MIN, 1, "H:m", 2 * HOUR));
        configs.add(new XAxisConfig(0.5, MIN, 1, MIN, 2, MIN, 1, "H:m", 2 * HOUR));
        configs.add(new XAxisConfig(1.2, MIN, 1, MIN, 4, MIN, 2, "H:m", 3 * HOUR));
        configs.add(new XAxisConfig(2, MIN, 1, MIN, 10, MIN, 5, "H:m", 6 * HOUR));
        configs.add(new XAxisConfig(5, MIN, 2, MIN, 10, MIN, 10, "H:m", 12 * HOUR));
        configs.add(new XAxisConfig(10, MIN, 5, MIN, 20, MIN, 20, "HH:mm", 1 * DAY));
        configs.add(new XAxisConfig(30, MIN, 10, HOUR, 1, HOUR, 1, "HH:mm", 2 * DAY));
        configs.add(new XAxisConfig(60, MIN, 30, HOUR, 2, HOUR, 2, "HH:mm", 2 * DAY));
        configs.add(new XAxisConfig(100, HOUR, 2, HOUR, 4, HOUR, 4, "EEE ha", 6 * DAY));
        configs.add(new XAxisConfig(255, HOUR, 6, HOUR, 12, HOUR, 12, "MM/dd hha", 10 * DAY));
        configs.add(new XAxisConfig(600, HOUR, 6, DAY, 1, DAY, 1, "MM/dd", 14 * DAY));
        configs.add(new XAxisConfig(600, HOUR, 12, DAY, 1, DAY, 1, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(2000, DAY, 1, DAY, 2, DAY, 2, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(4000, DAY, 2, DAY, 4, DAY, 4, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(8000, DAY, 3.5, DAY, 7, DAY, 7, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(16000, DAY, 7, DAY, 14, DAY, 14, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(32000, DAY, 15, DAY, 30, DAY, 30, "MM/dd", 365 * DAY));
        configs.add(new XAxisConfig(64000, DAY, 30, DAY, 60, DAY, 60, "MM/dd YYYY", Long.MAX_VALUE));
        configs.add(new XAxisConfig(100000, DAY, 60, DAY, 120, DAY, 120, "MM/dd YYYY", Long.MAX_VALUE));
        configs.add(new XAxisConfig(120000, DAY, 120, DAY, 240, DAY, 240, "MM/dd YYYY", Long.MAX_VALUE));
    }

    public static XAxisConfig getXAxisConfig(double secondsPerPixel, long timeRange) {
        XAxisConfig result = null;

        for (XAxisConfig config : configs) {
            if ((config.getSeconds() <= secondsPerPixel) && (config.getMaxInterval() >= timeRange)) {
                result = config;
            }
        }

        return result != null ? result : configs.get(configs.size() - 1);

    }

}
@@ -611,20 +611,23 @@
 C, 5, %22H
-:
+H:m
 m:s
+s
 %22, 10 * 
@@ -703,20 +703,23 @@
 , 10, %22H
-:
+H:m
 m:s
+s
 %22, 20 * 
@@ -799,12 +799,15 @@
 , %22H
-:
+H:m
 m:s
+s
 %22, 3
@@ -880,32 +880,33 @@
  2, MIN, 1, %22H:m
+m
 %22, 2 * HOUR));%0A 
@@ -967,25 +967,27 @@
 , MIN, 1, %22H
-:
+H:m
 m%22, 2 * HOUR
@@ -1053,25 +1053,27 @@
 , MIN, 2, %22H
-:
+H:m
 m%22, 3 * HOUR
@@ -1138,25 +1138,27 @@
 , MIN, 5, %22H
-:
+H:m
 m%22, 6 * HOUR
@@ -1228,17 +1228,19 @@
 , 10, %22H
-:
+H:m
 m%22, 12 *

fix date time format