define(
    [
      'ep',
      'de_epages/guidedtour/js/hopscotch',
      'de_epages/guidedtour/js/helpers',
      'ep/ui/dialog'
    ],
    function(ep,hopscotch){

        var o = {},
            dialog = {
                width : 450,
                i18n : {}
            },
            /* we could define all the i18n via a js dictinary, but leave it empty for now */
            i18n = {};

        /**
         * This method initializes the GuidedTour object and returns the start method to start the tour.
         *
         */
        function init(options){
            o = {
                tourId : options.tourId,
                shopId : options.shopId,
                userId : options.userId,
                tour   : options.tour,
                tourConfigId : options.tourConfigId,
                tourSettings : options.tourSettings
            };
            ep.extend(true, dialog,options.dialog);
            ep.extend(i18n,options.i18n);

            return { start : start}
        }
        return { init : init }

        /*
         * After properly initializing the tour, it can be started via the parameterless start method.
         */
        function start(tourState){
            if (tourState.Started){
                startTour();
            } else if (tourState.AutoStart) {
                showTourStartDialog();
            }
        }

        /*
         * Private functions
         */
        function startTour(){
            hopscotch.configure(ep.extend({
                i18n : i18n,
                showPrevButton : true,
                onEnd:      function () { SetTourData( {finished:1} ) },
                onClose:    function () { SetTourData( {abortedStep:hopscotch.getCurrStepNum()} ) },
            }, o.tourSettings));
            hopscotch.startTour(o.tour);
        }


        function showTourStartDialog(){
            ep('<div class="modalWindow"></div>')
            .html('<div class="windowContent">'+dialog.content+'</div>')
            .uiDialog({
                title: dialog.i18n.tourTitle,
                modal: true,
                width: dialog.width,
                buttons: [
                {
                    id:         'StartTourButton',
                    text:       dialog.i18n.takeTheTour,
                    'class' :   'Button SaveButton',
                    click: function () {
                        ep(this).uiDialog('close');
                        SetPreConditionData();
                        startTour();
                        SetTourData( {started:1} );
                    },
                },
                {
                    id:         'CloseTourButton',
                    text: dialog.i18n.dismissTour,
                    click: function () {
                        ep(this).uiDialog('close');
                        SetTourData( {abortedStep:0} );
                    }
                }]
            })
            .parent()
            .addClass('tourStartDialog')
            .append((dialog.footer !== undefined) ? '<div class="windowFooter">'+dialog.footer+'</div>' : '');
        }

        /* no special handling for fail because the merchant can't change anything about this
           also always all parameters are set so that we have always a clear state even if
           the merchant startet the tour several times */
        function SetTourData(opts) {
            var tourDataOpts = {
                autoStart   : (opts.autoStart !== undefined)?opts.autoStart:0,
                started     : (opts.started !== undefined)?opts.started:0,
                finished    : (opts.finished !== undefined)?opts.finished:0,
                abortedStep : (opts.abortedStep !== undefined)?opts.abortedStep:'',
            };
            ep.ajax({
                type:     "post",
                dataType: "json",
                data: {
                  AutoStart:     tourDataOpts.autoStart,
                  Started:       tourDataOpts.started,
                  Finished:      tourDataOpts.finished,
                  AbortedOnStep: tourDataOpts.abortedStep,
                  ObjectID:      o.tourId,
                  ChangeAction: 'SetTourData'
                }
            })
        }

        function SetPreConditionData() {
            ep.ajax({
                type:     "post",
                dataType: "json",
                data: {
                  ObjectID:     o.shopId,
                  UserID:       o.userId,
                  TourID:       o.tourConfigId,
                  ChangeAction: 'UnityEnsurePreConditions'
                }
            })
        }

    }
);