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

    'qui/QUI',
    'qui/controls/Control',
    'package/quiqqer/countries/bin/controls/Select',

    'Ajax',
    'Locale',

], function (QUI, QUIControl, CountrySelect, QUIAjax, QUILocale) {
    "use strict";

    return new Class({

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

        Binds: [
            '$onImport'
        ],

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

            this.$Select = null;

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

        $onImport: function () {
            this.$Select = this.getElm().getElement('select');

            new CountrySelect({
                styles: {
                    width: 300
                },
                events: {
                    onLoad: function (Instance) {
                        Instance.$Select.name = 'quiqqer-country';
                    }
                }
            }).inject(
                this.getElm().getElement('.general-country')
            );

            QUIAjax.get('package_quiqqer_areas_ajax_import_available', (result) => {
                // sort
                result.sort(function (a, b) {
                    if (a.file === "manual.xml") {
                        return -1;
                    }

                    if (b.file === "manual.xml") {
                        return +1;
                    }

                    const al = QUILocale.get(a.locale[0], a.locale[1]);
                    const bl = QUILocale.get(b.locale[0], b.locale[1]);

                    return al.localeCompare(bl);
                });

                for (let i = 0, len = result.length; i < len; i++) {
                    new Element('option', {
                        html : QUILocale.get(result[i].locale[0], result[i].locale[1]),
                        value: result[i].file
                    }).inject(this.$Select);
                }

                this.$Select.disabled = false;
                this.$Select.value = 'PhysicalGoodsFromEuropeToTheWorld.xml';
            }, {
                'package': 'quiqqer/areas'
            });
        },

        /**
         *
         */
        next: function () {
            if (!this.$Select) {
                return false;
            }

            if (this.$Select.disabled) {
                return false;
            }

            return true;
        }
    });
});
