/**
 * @module package/ecoyn/core/bin/backend/controls/Setup/Steps/LogoUpload
 * @author www.pcsg.de (Henning Leutz)
 */
define('package/ecoyn/core/bin/backend/controls/Setup/Steps/LogoUpload', [

    'qui/QUI',
    'qui/controls/Control',
    'controls/upload/Form',

    'css!package/ecoyn/core/bin/backend/controls/Setup/Steps/LogoUpload.css'

], function (QUI, QUIControl, Upload) {
    "use strict";

    let Logo = null;

    return new Class({

        Extends: QUIControl,
        Type: 'package/ecoyn/core/bin/backend/controls/Setup/Steps/LogoUpload',

        Binds: [
            '$onImport',
            '$onComplete'
        ],

        initialize: function (options) {
            this.parent(options);

            this.$Upload = null;
            this.$loaded = false;

            this.addEvents({
                onImport: this.$onImport
            });
        },

        $onImport: function () {
            this.getElm().addClass('ecoyn-wizard-logo-upload');
            this.$createUpload();
        },

        $onComplete: function () {
            this.getAttribute('Wizard').hideLoader();
            this.getAttribute('Wizard').next();
        },

        $createUpload: function () {
            this.$loaded = false;
            const Container = this.getElm().getElement('.logo-upload');

            Container.set('html', '');

            this.$Upload = new Upload({
                maxuploads: 1,
                multiple: false,
                events: {
                    onAdd: (Instance, File) => {
                        Logo = File;

                        if (this.$loaded) {
                            this.getAttribute('Wizard').showLoader();
                            this.$Upload.submit();
                        }
                    },
                    onComplete: this.$onComplete
                }
            }).inject(Container);

            this.$Upload.setParam('onfinish', 'package_ecoyn_core_ajax_logoUpload');
            this.$Upload.setParam('package', 'ecoyn/core');

            if (Logo) {
                this.$Upload.addUpload(Logo);
            }

            this.$loaded = true;
        }
    });
});
