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

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

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

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

    let Logo = null;

    return new Class({

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

        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-license-upload');
            this.$createUpload();
        },

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

        $createUpload: function () {
            this.$loaded = false;
            const Container = this.getElm().getElement('.license-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_licenseUpload');
            this.$Upload.setParam('package', 'ecoyn/core');

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

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