(function (paw, utils, fn) {
    function WorkflowOverview(settings, localization) {
        var self = this;
        this.settings = paw.lang.append({
            $dataTable: undefined,
            $keywords: undefined,
            userLocale: undefined,
            workflowImport: {
                getImportDefinitionsUrl: undefined,
                importUrl: undefined,
                siteHelpUrl: undefined
            }
        }, settings);

        this.localization = paw.lang.append({
            name: '@Loc.Name',
            note: '@Loc.Note',
            buildNumber: '@Loc.BuildNUmber',
            importType: '@Loc.ImportType',
            forCountry: '@Loc.ForCountry',
            downloadFromServer: '@Loc.DownloadFromServer',
            importIntoCodebook: '@Loc.ImportIntoCodebook',
            importTypeAdding: '@Loc.ImportTypeAdding',
            importTypeAddEdit: '@Loc.ImportTypeAddEdit',
            yesNo: "@Loc.Yes/@Loc.No",
            group: "@Loc.Group",
            importWorkflowTitle: "@Loc.ImportWorkflowDefinition"
        }, localization);

        this.searchDebounced = fn.debouncer(function () {
            $.fn.dataTable.reload(self.settings.$dataTable);
        }, 300);

        this.onSearch = function () {
            self.searchDebounced();
        };
    }

    WorkflowOverview.prototype.init = function () {
        var self = this;
        this.settings.$dataTable.DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                url: this.settings.$dataTable.attr('data-url'),
                data: function (d) {
                    d.Keywords = self.settings.$keywords.val();
                }
            },
            columns: [
                { "data": "Name" },
                { "data": "Note" }

            ],
            responsive: false,
            scrollX: true
        });
        this.settings.$keywords.off('keyup', this.onSearch).on('keyup', this.onSearch);
    };

    WorkflowOverview.prototype.onImportWFDefinition = function () {
        let tableTitles = [this.localization.name, this.localization.note, this.localization.buildNumber];
        let tableDataNames = ["Name", "Note", "BuildNo"];
        let localization = {
            importType: this.localization.importType,
            forCountry: this.localization.forCountry,
            dowloadFromServer: this.localization.downloadFromServer,
            importIntoCodebook: this.localization.importIntoCodebook,
            importTypeAdding: this.localization.importTypeAdding,
            importTypeAddEdit: this.localization.importTypeAddEdit,
            yesNo: this.localization.yesNo
        };

        openImportCodebookModal(this.localization.importWorkflowTitle, tableTitles, tableDataNames,
            this.settings.workflowImport.getImportDefinitionsUrl,
            this.settings.workflowImport.importUrl,
            this.settings.workflowImport.siteHelpUrl,
            localization,
            this.settings.userLocale,
            function () {
                $.fn.dataTable.reload($('#grid-data'));
            },
            function (data) {
                data.group = $(".codebook-group").val();
                data.buildNo = $(".codebook-buildno").val();
            },
            "<div class='col-sm-4 m-t-xs'><label>" + this.localization.group + "</label><select class='form-control codebook-group' style='display:inline-block;width:auto'></select></div>" +
            "<div class='col-sm-5 m-t-xs'><label>" + this.localization.buildNumber + "</label><input type='text' class='form-control codebook-buildno' style='display:inline-block;width:auto;max-width:110px' /></div>",
            function (data) {
                if (data.WorkflowDefinitionGroup && $(".codebook-group").children().length === 0) {
                    for (let i = 0; i < data.WorkflowDefinitionGroup.length; i++) {
                        let definitionGroup = data.WorkflowDefinitionGroup[i];
                        $(".codebook-group").append("<option value='" + (definitionGroup.Group === null ? "" : definitionGroup.Group) + "'>" + (definitionGroup.Name === null ? "" : definitionGroup.Name) + "</option>");
                    }
                }
            }
        );
    };

    paw.view.settings.WorkflowOverview = WorkflowOverview;
})(paw, paw.utils, {
    openImportCodebookModal: openImportCodebookModal,
    debouncer: debouncer
});