/**
 * Created by gerd on 24.01.17.
 */
define('package/quiqqer/tour/bin/classes/Step', [

    'qui/QUI',
    'qui/classes/DOM'

], function (QUI, QDOM) {
    "use strict";

    return new Class({
        Extends: QDOM,
        Type: 'package/quiqqer/tour/bin/classes/Step',

        options: {
            shepherd: {}
        },

        initialize: function (Tour, options) {
            this.$Tour = Tour;

            var events = options.events || {};

            this.setAttribute('shepherd', options);
            this.addEvents(events);
        },

        /**
         *
         * @returns {Promise}
         */
        run: function () {
            return new Promise(function (resolve) {
                // make options
                var options = this.getAttribute('shepherd');

                var oldShow = function () {
                };

                if (typeof options.when === 'undefined') {
                    options.when = {};
                }

                if (typeof options.when.show === 'function') {
                    oldShow = options.when.show;
                }

                options.when.show = function () {
                    oldShow();
                    this.fireEvent('show');
                    resolve();
                }.bind(this);

                if (typeof options.attachTo !== 'undefined'
                    && typeOf(options.attachTo.element) === 'function') {
                    options.attachTo.element = options.attachTo.element()
                };

                // finally add
                this.$Tour.getShepherd().addStep(name, options);
            }.bind(this));
        }
    });
});