(function (paw, utils) {
    function DocumentMetadataComponent(localization) {
        this.localization = paw.lang.append({
            subject: '@Loc.Subject',
            name2: '@Loc.Name2',
            address: '@Loc.Address',
            post: '@Loc.Post',
            phone: '@Loc.Telefon',
            acDescription: '@Loc.Description',
            acSubject: '@Loc.Subject',
            description: '@Loc.Description',
            code: '@Loc.Code',
            company: '@Loc.Company',
            title: '@Loc.Title',
            vatNumber: '@Loc.VatNumber',
            department: '@Loc.Department',
            name: '@Loc.Name',
            docType: '@Loc.DocType'
        }, localization || {});

        this.searchDebouncer = debouncer(function ($elt, tpl, val, response) {
            return utils.async.get('/Documents/MetaDataSetterLookup' + qparams({
                setterType: $elt.attr('data-metadata-setter'),
                search: $elt.val()
            })).then(function (result) {
                var items = [];
                tpl.resultField = result.ResultField;
                tpl.displayProperties = utils.value.notSet(result.DisplayValue) ? [result.ResultField] : [result.DisplayValue.substring(1, result.DisplayValue.length - 1)];
                utils.iterable.iterate(result.Items, function (item, i) {
                    var transformedItem = {};
                    utils.iterable.iterate(item.Properties, function (val, key) {
                        transformedItem[key] = val;
                    });
                    transformedItem.$$row = i;
                    transformedItem.$$isLast = i === item.Properties.length - 1;
                    items.push(transformedItem);
                });
                response(items);
                if (!$elt.hasClass("typeahead-normal"))
                    $(".sticky-header").floatThead();
                return items;
            });
        }, 400);
    }

    DocumentMetadataComponent.prototype.init = function () {
        var self = this;
        $('[data-metadata-setter]').each(function () {
            var $elt = $(this);
            self.initSetter($elt)
        });
    };

    DocumentMetadataComponent.prototype.initSetter = function ($elt) {
        var self = this;
        var tpl = this.produceTpl($elt.attr('data-metadata-setter'), $elt.attr('data-meta-data-setter-value'));
        $elt.typeahead({
            display: '',
            appendTo: 'body',
            source: function (val, response) {
                return self.searchDebouncer($elt, tpl, val, response);
            },
            minLength: 0,
            items: 100,
            updater: function (item) {
                var $valueField = $('input[name="' + tpl.setterValueField + '"]');
                $valueField[0].value = item[tpl.resultField];
                return item;
            },
            highlighter: Object,
            displayText: function (item) {
                var row = "";
                var pos = item.$$row;
                row += '<table class="table sticky-header">';
                if (pos === 0) {
                    this.$menu.css({
                        'min-width': this.$element.outerWidth()
                    });
                    this.$menu.addClass('document-meta-data-lookup');
                    row += '<thead><tr class="row">';
                    row += tpl.header(item);
                    row += '</tr></thead>';
                }

                row += '<tbody >';
                row += '<tr class="row">';
                row += tpl.row(item);
                row += '</tr>';
                row += '</tbody></table>';

                return row;
            },
            sorter: function (items) {
                return items;
            },
            matcher: function (item) {
                return true;
            },
            afterSelect: function (item) {
                this.$element[0].value = item[tpl.resultField];
            }
        });
    };

    DocumentMetadataComponent.prototype.produceTpl = function (setterType, setterValueField) {
        switch (setterType) {
            case 'ltDobavitelj':
                return new paw.com.documentMetadata.tpl.TplLtDobavitelj(setterValueField, this.localization);
            case 'ltKupec':
                return new paw.com.documentMetadata.tpl.TplLtKupec(setterValueField, this.localization);
            case 'ltMNClassification':
                return new paw.com.documentMetadata.tpl.TplLtMNClassification(setterValueField, this.localization);
            case 'ltOddelekAct':
                return new paw.com.documentMetadata.tpl.TplLtOddelekAct(setterValueField, this.localization);
            case 'ltPoslDog':
                return new paw.com.documentMetadata.tpl.TplLtPoslDog(setterValueField, this.localization);
            case 'ltSubj':
                return new paw.com.documentMetadata.tpl.TplLtSubj(setterValueField, this.localization);
            default:
                return new paw.com.documentMetadata.DocumentMetadataLookupTpl(setterType, setterValueField, this.localization);
        }
    };
    

    paw.com.documentMetadata.DocumentMetadataComponent = DocumentMetadataComponent;

})(paw, paw.utils);