/**
 * Created by Samuel Gratzl on 27.08.2015.
 */
import * as C from 'phovea_core/src/index';
import * as d3 from 'd3';
import { resolveImmediately } from 'phovea_core/src';
export var FACTOR = 1;
export var MIN_DURATION = -1;
export var MIN_TRANSITION = -1;
/**
 * story player interface and logic
 */
var Player = (function () {
    function Player(graph, controls, options) {
        if (options === void 0) { options = {}; }
        var _this = this;
        this.graph = graph;
        this.anim = -1;
        this.options = {
            //default animation step duration
            step: 1000
        };
        C.mixin(this.options, options);
        var $controls = d3.select(controls);
        var that = this;
        this.$play = $controls.select('[data-player="play"]').on('click', function () {
            var $i = d3.select(this);
            if ($i.classed('fa-play') && that.start()) {
                $i.classed('fa-play', false).classed('fa-pause', true);
            }
            else {
                that.pause();
                $i.classed('fa-play', true).classed('fa-pause', false);
            }
        });
        $controls.select('[data-player="stop"]').on('click', function () {
            that.stop();
        });
        $controls.select('[data-player="forward"]').on('click', function () {
            that.forward();
        });
        $controls.select('[data-player="backward"]').on('click', function () {
            that.backward();
        });
        d3.select(document).on('keydown.playpause', function () {
            var k = d3.event;
            //pause key
            if (k.keyCode === 19) {
                k.preventDefault();
                //fake a click event
                var event_1 = document.createEvent('MouseEvents');
                event_1.initMouseEvent('click', /* type */ true, /* canBubble */ true, /* cancelable */ window, /* view */ 0, /* detail */ 0, /* screenX */ 0, /* screenY */ 0, /* clientX */ 0, /* clientY */ false, /* ctrlKey */ false, /* altKey */ false, /* shiftKey */ false, /* metaKey */ 0, /* button */ null /* relatedTarget */);
                _this.$play.node().dispatchEvent(event_1);
            }
        });
    }
    Player.prototype.start = function () {
        var _this = this;
        var l = this.graph.getSlideChains();
        var act = this.graph.selectedSlides()[0] || l[l.length - 1];
        if (act) {
            this.render(act).then(function () {
                _this.anim = setTimeout(_this.next.bind(_this), act.duration * FACTOR);
            });
            return true;
        }
        else {
            return false;
        }
    };
    Player.prototype.render = function (story) {
        //render by selecting the slide
        this.graph.selectSlide(story);
        //TODO transition time
        return resolveImmediately(story);
    };
    Player.prototype.stopAnim = function () {
        if (this.anim >= 0) {
            clearTimeout(this.anim);
            this.anim = -1;
        }
    };
    Player.prototype.stop = function () {
        var _this = this;
        this.stopAnim();
        this.render(null).then(function () {
            _this.$play.classed('fa-play', true).classed('fa-pause', false);
        });
    };
    Player.prototype.pause = function () {
        this.stopAnim();
    };
    /**
     * renders the next slide in an animated fashion
     */
    Player.prototype.next = function () {
        var _this = this;
        var r = this.forward();
        if (r) {
            r.then(function (act) {
                _this.anim = setTimeout(_this.next.bind(_this), act.duration * FACTOR);
            });
        }
    };
    /**
     * jumps to the next slide
     * @returns {any}
     */
    Player.prototype.forward = function () {
        this.stopAnim();
        var current = this.graph.selectedSlides()[0];
        var act = current.next;
        if (act == null) {
            this.stop();
            return null;
        }
        else {
            return this.render(act);
        }
    };
    /**
     * jumps to the previous slide
     * @returns {any}
     */
    Player.prototype.backward = function () {
        this.stopAnim();
        var current = this.graph.selectedSlides()[0];
        var act = current.previous;
        if (act == null) {
            this.stop();
            return null;
        }
        else {
            return this.render(act);
        }
    };
    return Player;
}());
export { Player };
//# sourceMappingURL=player.js.map