package com.dlsc.flexgantt.showcase.demo.layers;

import java.awt.Color;
import java.util.Calendar;

import com.dlsc.flexgantt.model.TimeSpan;
import com.dlsc.flexgantt.model.gantt.DefaultGanttChartModel;
import com.dlsc.flexgantt.model.gantt.DefaultGanttChartNode;
import com.dlsc.flexgantt.model.gantt.DefaultTimelineObject;
import com.dlsc.flexgantt.model.gantt.ILayer;
import com.dlsc.flexgantt.model.gantt.ITimelineObject;

public class LayerDemoGanttChartModel
        extends
        DefaultGanttChartModel<DefaultGanttChartNode<String, ITimelineObject>, ITimelineObject> {

    @SuppressWarnings("unchecked")
    public LayerDemoGanttChartModel() {
        super(new DefaultGanttChartNode<String, ITimelineObject>());
        Color[] color = new Color[10];
        color[0] = Color.RED;
        color[1] = Color.GREEN.darker();
        color[2] = Color.BLUE;
        color[3] = Color.ORANGE.darker();
        color[4] = Color.YELLOW;

        DefaultGanttChartNode<String, ITimelineObject> rootNode = getRoot();
        final int ROW_COUNT = 250;
        DefaultGanttChartNode[] node = new DefaultGanttChartNode[ROW_COUNT];
        for (int i = 0; i < ROW_COUNT; i++) {
            node[i] = new DefaultGanttChartNode<String, ITimelineObject>(false);
            node[i].setKey("Node " + i);
            node[i].setRowHeight(30);
            
            rootNode.add(node[i]);
        }

        final int LAYER_COUNT = 5;
        LayerDemoLayer[] layer = new LayerDemoLayer[LAYER_COUNT];
        for (int i = 0; i < LAYER_COUNT; i++) {
            layer[i] = new LayerDemoLayer(i + 1, color[i % color.length]);
            layer[i].addFeature(ILayer.Feature.DELETION);
            addLayer(layer[i]);
        }
        
        for (int i = 0; i < ROW_COUNT; i++) {
            for (int j = 0; j < LAYER_COUNT; j++) {
                if (Math.random() > 0.33) {
                    // The Chances that row contains timeline objects for the
                    // current layer is 2 to 1.
                    for (int k = 0; k < Math.random() * 20; k++) {
                        // Row can have up to 20 timeline objects on the current
                        // layer.
                        DefaultTimelineObject tlo = new DefaultTimelineObject();
                        int offset = (int) (Math.random() * 90);
                        Calendar c = Calendar.getInstance();
                        c.add(Calendar.DAY_OF_YEAR, offset);
                        if (Math.random() > 0.9) {
                            Calendar c2 = Calendar.getInstance();
                            c2.add(Calendar.DAY_OF_YEAR, offset);
                            c2.add(Calendar.DAY_OF_YEAR, Math.max(3,
                                    (int) (Math.random() * 10)));
                            tlo.setTimeSpan(new TimeSpan(c, c2));
                        } else {
                            tlo.setTimeSpan(new TimeSpan(c));
                        }
                        node[i].addTimelineObject(layer[j], tlo);
                    }
                }
            }
        }
    }
}
