/*
 * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
 *
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

package org.lttng.scope.tmf2.views.ui.timegraph.swtjfx;

import org.lttng.scope.tmf2.views.core.timegraph.model.render.states.TimeGraphStateInterval;

import javafx.scene.shape.Rectangle;

public class StateRectangle extends Rectangle {

    private final TimeGraphStateInterval fInterval;

    public StateRectangle(SwtJfxTimeGraphViewer viewer, TimeGraphStateInterval interval, int entryIndex) {
        fInterval = interval;

        double xStart = viewer.timestampToPaneXPos(interval.getStartEvent().getTimestamp());

        /*
         * It is possible, especially when re-opening already-indexed traces,
         * that the indexer and the state system do not report the same end
         * time. Make sure to clamp the interval's end to the earliest valid
         * value.
         */
        long modelEndTime = viewer.getControl().getFullTimeGraphEndTime();
        long intervalEndTime = interval.getEndEvent().getTimestamp();
        double xEnd = viewer.timestampToPaneXPos(Math.min(modelEndTime, intervalEndTime));

        double width = Math.max(1.0, xEnd - xStart) + 1.0;

        double height;
        switch (interval.getLineThickness()) {
        case NORMAL:
        default:
            height = SwtJfxTimeGraphViewer.ENTRY_HEIGHT - 4;
            break;
        case SMALL:
            height = SwtJfxTimeGraphViewer.ENTRY_HEIGHT - 8;
            break;
        }

        // TODO Calculate value for small thickness too
        double y = entryIndex * SwtJfxTimeGraphViewer.ENTRY_HEIGHT + 2;

        setX(xStart);
        setY(y);
        setWidth(width);
        setHeight(height);
        setFill(JfxColorFactory.getColorFromDef(interval.getColorDefinition()));
    }

    public TimeGraphStateInterval getStateInterval() {
        return fInterval;
    }

}

@@ -1738,55 +1738,75 @@
     
-// TODO Calculate value for small thickness too
+double yOffset = (SwtJfxTimeGraphViewer.ENTRY_HEIGHT - height) / 2;
 %0A   
@@ -1871,17 +1871,23 @@
 EIGHT + 
-2
+yOffset
 ;%0A%0A     

Compute correct Y-position for smaller state rectangles