/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/**
 * @param {*} server
 * @param {*} deliveryServices
 * @param {*} assignedDeliveryServices
 * @param {*} $scope
 * @param {*} $uibModalInstance
 * @param {import("../../../service/utils/ServerUtils")} serverUtils
 */
var TableAssignDeliveryServicesController = function(server, deliveryServices, assignedDeliveryServices, $scope, $uibModalInstance, serverUtils) {

	var selectedDeliveryServices = [];

	var addAll = function() {
		markVisibleDeliveryServices(true);
	};

	var removeAll = function() {
		markVisibleDeliveryServices(false);
	};

	var markVisibleDeliveryServices = function(selected) {
		var visibleDSIds = $('#assignDSTable tr.ds-row').map(
			function() {
				return parseInt($(this).attr('id'));
			}).get();
		$scope.selectedDeliveryServices = deliveryServices.map(ds => {
			if (ds.topology && serverUtils.isCache(server)) {
				return ds;
			}
			if (visibleDSIds.includes(ds.id)) {
				ds['selected'] = selected;
			}
			return ds;
		});
		updateSelectedCount();
	};

	function updateSelectedCount() {
		selectedDeliveryServices = $scope.selectedDeliveryServices.filter(ds => ds.selected );
		$('div.selected-count').html('<b>' + selectedDeliveryServices.length + ' delivery services selected</b>');
	}

	$scope.server = server;

	$scope.isCache = serverUtils.isCache;

	$scope.selectedDeliveryServices = deliveryServices.map(ds => {
		const isAssigned = assignedDeliveryServices.find(assignedDS => assignedDS.id === ds.id);
		if (isAssigned) {
			ds.selected = true;
		}
		return ds;
	});

	$scope.toggleRow = function(ds) {
		// a ds w/ a topology has no use being assigned to cache servers
		if (ds.topology && $scope.isCache(server)) {
			return;
		}
		ds.selected = !ds.selected;
		$scope.onChange();
	};

	$scope.selectAll = function($event) {
		var checkbox = $event.target;
		if (checkbox.checked) {
			addAll();
		} else {
			removeAll();
		}
	};

	$scope.onChange = function() {
		updateSelectedCount();
	};

	$scope.submit = function() {
		var selectedDSIds = selectedDeliveryServices.map(d => d.id);
		$uibModalInstance.close(selectedDSIds);
	};

	$scope.cancel = function () {
		$uibModalInstance.dismiss('cancel');
	};

	angular.element(document).ready(function () {
		var assignDSTable = $('#assignDSTable').dataTable({
			"scrollY": "60vh",
			"paging": false,
			"order": [[ 1, 'asc' ]],
			"dom": '<"selected-count">frtip',
			"columnDefs": [
				{ 'orderable': false, 'targets': 0 },
				{ "width": "5%", "targets": 0 }
			],
			"stateSave": false
		});
		assignDSTable.on( 'search.dt', function () {
			$("#selectAllCB").removeAttr("checked"); // uncheck the all box when filtering
		} );
		updateSelectedCount();
	});

};

TableAssignDeliveryServicesController.$inject = ['server', 'deliveryServices', 'assignedDeliveryServices', '$scope', '$uibModalInstance', 'serverUtils'];
module.exports = TableAssignDeliveryServicesController;
