'use strict';

var EventHandler = require('./../utilities/event-handler');
var converter = require('best-timelineUI').converter;
var timelineHelper = require('./../helpers/helpers').timeline;

function Timelines(timelineGroups, states) {
    EventHandler.apply(this);

    this._timelineGroups = timelineGroups || {};
    this._states = states;

    this._currentTimeline = null;
    this._currentTimelineName = null;
}

Timelines.prototype = Object.create(EventHandler.prototype);
Timelines.prototype.constructor = Timelines;

Timelines.prototype.get = function get(timelineName) {;
    this._currentTimeline = this._timelineGroups[timelineName];
    this._currentTimelineName = timelineName;
    return this;
}

Timelines.prototype.start = function start() {
    var duration = this._currentTimeline.duration;
    var convertedTimeline = converter.sweetToSalty(this._currentTimeline);

    var stateTimelines = convertedTimeline.states;

    var timelineStateName = '__' + this._currentTimelineName + 'Time';

    this._states.set(timelineStateName, 0);
    this._states.set(timelineStateName, duration, { duration: duration});

    // loops through each individual state timeline
    for (var state in stateTimelines) {
        var timeline = stateTimelines[state];
        var valueFunction = timelineHelper(timeline);

        (function(state, valueFunction) {
            this._states.subscribeTo(timelineStateName, function(key, time) {
                this._states.set(state, valueFunction(time));
            }.bind(this));
        }.bind(this))(state, valueFunction);
    }

}

Timelines.prototype.halt = function halt() {
    var timelineStateName = '__' + this._currentTimelineName + 'Time';
    var currentTimelineTime = this._states.get(timelineStateName);

    // duration is needed until all states are stored in Transitionables
    this._states.set(timelineStateName, currentTimelineTime, {duration:0});
}

module.exports = Timelines;
'use strict';

var EventHandler = require('./../utilities/event-handler');
var converter = require('best-timelineUI').converter;
var timelineHelper = require('./../helpers/helpers').timeline;

function Timelines(timelineGroups, states) {
    EventHandler.apply(this);

    this._timelineGroups = timelineGroups || {};
    this._states = states;

    this._currentTimeline = null;
    this._currentTimelineName = null;

    this._pausedTimelines = {};
}

Timelines.prototype = Object.create(EventHandler.prototype);
Timelines.prototype.constructor = Timelines;

Timelines.prototype.get = function get(timelineName) {;
    this._currentTimeline = this._timelineGroups[timelineName];
    this._currentTimelineName = timelineName;
    return this;
}

Timelines.prototype.start = function start() {
    var duration = this._currentTimeline.duration;
    var convertedTimeline = converter.sweetToSalty(this._currentTimeline);

    var stateTimelines = convertedTimeline.states;

    var timelineStateName = '__' + this._currentTimelineName + 'Time';

    this._states.set(timelineStateName, 0);
    this._states.set(timelineStateName, duration, { duration: duration});

    this._pausedTimelines[this._currentTimelineName] = false;

    // loops through each individual state timeline
    for (var state in stateTimelines) {
        var timeline = stateTimelines[state];
        var valueFunction = timelineHelper(timeline);

        (function(state, valueFunction) {
            this._states.subscribeTo(timelineStateName, function(key, time) {
                this._states.set(state, valueFunction(time));
            }.bind(this));
        }.bind(this))(state, valueFunction);
    }

}

Timelines.prototype.halt = function halt() {
    var timelineStateName = '__' + this._currentTimelineName + 'Time';
    var currentTimelineTime = this._states.get(timelineStateName);

    // duration is needed until all states are stored in Transitionables
    this._states.set(timelineStateName, currentTimelineTime, {duration:0});

    this._pausedTimelines[this._currentTimelineName] = true;
}

Timelines.prototype.resume = function resume() {
    var timelineStateName = '__' + this._currentTimelineName + 'Time';
    var time = this._timelineGroups[this._currentTimelineName].duration;
    var timeLeft = time - this._states.get(timelineStateName);

    this._states.set(timelineStateName, time, {duration: timeLeft});

    this._pausedTimelines[this._currentTimelineName] = false;
}

Timelines.prototype.isPaused = function isPaused() {
    return this._pausedTimelines[this._currentTimelineName] || false;
}

module.exports = Timelines;
implement resume + isPaused