package displayshelf;
import javafx.animation.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.*;
import javafx.scene.input.*;
import javafx.util.*;

public class DisplayShelf extends CustomNode {
    //public var content:ShelfItem[];
    public var items:Node[];
    public-init var spacing = 110;
    public-init var leftOffset = -50;
    public-init var rightOffset = 50;
    var center:Group = Group { };
    public-init var perspective = false;
    public-init var scaleSmall = 0.5;


    var leftitems:Node[];
    var left:Group = Group { };

    var rightContent:Node[];
    var right:Group = Group { };

    public var centerIndex = 0;

    override public function create():Node {
        var half = items.size()/2-1;

        left.content = items[0..half-2];
        center.content = items[half-1];
        right.content = items[half..items.size()-1];
        right.content = Sequences.<<reverse>>(right.content) as Node[];

        centerIndex = half-1;

        doLayout();
        return Group {
            content: [
                left,
                right,
                center
            ]
        }
    }

    public function shift(offset:Integer):Void {
        if(centerIndex <= 0 and offset > 0 ) {
            return;
        }
        if(centerIndex >= items.size()-1 and offset < 0) {
            return;
        }


        centerIndex -= offset;
        left.content = items[0..centerIndex-1];
        center.content = items[centerIndex];
        right.content = Sequences.<<reverse>>(items[centerIndex+1..items.size()-1]) as Node[];
        doLayout();
    }

    function doLayout() {

        var startKeyframes:KeyFrame[];
        var endKeyframes:KeyFrame[];
        var duration = 0.5s;


        for(n in items) {
            var it = n as Item;
            insert KeyFrame { time: 0s values: [
                    n.translateX => n.translateX,
                    n.scaleX => n.scaleX,
                    n.scaleY => n.scaleY,
                    it.angle => it.angle,
                    ] } into startKeyframes;
        }

        for(n in left.content) {
            var it = n as Item;
            var newX = -left.content.size()*spacing +  spacing * indexof n + leftOffset;
            insert KeyFrame { time: duration values: [
                    n.translateX => newX,
                    n.scaleX => scaleSmall,
                    n.scaleY => scaleSmall,
                    it.angle => 45
                ] } into endKeyframes;
        }

        for(n in center.content) {
            var it = n as Item;
            insert KeyFrame { time: duration values: [
                    n.translateX => 0,
                    n.scaleX => 1.0,
                    n.scaleY => 1.0,
                    it.angle => 90
                ] } into endKeyframes;
        }

        for(n in right.content) {
            var it = n as Item;
            var newX = right.content.size()*spacing -spacing * indexof n + rightOffset;
            insert KeyFrame { time: duration values: [
                    n.translateX => newX,
                    n.scaleX => scaleSmall,
                    n.scaleY => scaleSmall,
                    it.angle => 135
                ] } into endKeyframes;
        }

        var anim = Timeline {
            keyFrames: [startKeyframes, endKeyframes]
        };
        anim.play();

    }

    public function shiftToCenter(item:Item):Void {
        for(n in left.content) {
            if(n == item) {
                var index = indexof n;
                var shiftAmount = left.content.size()-index;
                shift(shiftAmount);
                return;
            }
        }
        for(n in center.content) {
            if(n == item) {
                return;
            }
        }
        for(n in right.content) {
            if(n == item) {
                var index = indexof n;
                var shiftAmount = -(right.content.size()-index);
                shift(shiftAmount);
                return;
            }
        }
    }

}