/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */
var Entaxy;
(function (Entaxy) {
    Entaxy._module
    .component('entaxyModalList', {
        bindings: {
            items: '<',
            selected: '<',
            changeSelection: '<',
            filter: '<'
        },
        template:
        `
          <div class="modal-list-container">
            <div class="modal-list-toolbar-container">
              <pf-toolbar class="entaxy-toolbar" config="$ctrl.toolbarConfig"></pf-toolbar>
            </div>
            <div class="modal-list-list-container">
              <pf-list-view class="entaxy-list" items="$ctrl.viewedItems" config="$ctrl.listConfig">
                <div class="list-view-pf-description">
                  <div class="list-group-item-heading">
                    {{item.name}}
                  </div>
                </div>
              </pf-list-view>
            </div>
          </div>
        `,
        controller: entaxyModalListController
    })
    .name;

    function entaxyModalListController() {

        var ctrl = this;

        ctrl.$onInit = function() {
            ctrl.viewedItems = ctrl.items;
            ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedItems.length;

            // FIXME i don't like invoking of changeSelection somehow
            if (ctrl.selected) {
                ctrl.listConfig.selectedItems = [ ctrl.selected ];
                changeSelection();
            }
        }

        ctrl.toolbarConfig = {
            filterConfig: {
                fields: [
                    {
                        id: 'name',
                        title: 'Name',
                        placeholder: 'Filter by Name...',
                        filterType: 'text'
                    }
                ],
                appliedFilters: [],
                onFilterChange: filterChange
            },
            actionsConfig: {}
        };

        function filterChange(filters) {
            applyFilters(filters);
            ctrl.toolbarConfig.filterConfig.resultsCount = ctrl.viewedItems.length;
        };

        function applyFilters(filters) {
            ctrl.viewedItems = [];
            if (filters && filters.length > 0) {
                ctrl.items.forEach(function (item) {
                    if (matchesFilters(item, filters)) {
                        ctrl.viewedItems.push(item);
                    }
                });
            } else {
                ctrl.viewedItems = ctrl.items;
            }
        };

        function matchesFilters(item, filters) {
            var matches = true;
            filters.forEach(function(filter) {
                if (!matchesFilter(item, filter)) {
                    matches = false;
                    return matches;
                }
            });
            return matches;
        };

        function matchesFilter(item, filter) {
            if (ctrl.filter) {
                return ctrl.filter(item, filter);
            } else {
                var match = true;
                if (filter.id === 'name') {
                    match = item.name.match(filter.value) !== null;
                }
                return match;
            }
        };

        ctrl.listConfig = {
            selectionMatchProp: 'name',
            selectItems: true,
            showSelectBox: false,
            onSelectionChange: changeSelection
        }

        function changeSelection() {
            ctrl.changeSelection(ctrl.listConfig.selectedItems[0]);
        }
    }

})(Entaxy || (Entaxy = {}));