/*
 * Copyright 2011-2022 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.
 */
package com.b2international.snowowl.core.internal.locks;

import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.b2international.snowowl.core.ApplicationContext;
import com.b2international.snowowl.core.IDisposableService;
import com.b2international.snowowl.core.locks.IOperationLockManager;
import com.b2international.snowowl.eventbus.IEventBus;
import com.b2international.snowowl.eventbus.IHandler;
import com.b2international.snowowl.eventbus.IMessage;
import com.b2international.snowowl.eventbus.events.ClientConnectionNotification;
import com.b2international.snowowl.eventbus.events.SystemNotification;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;

public class RemoteLockTargetListener implements IHandler<IMessage>, IDisposableService {

	private static final Logger LOGGER = LoggerFactory.getLogger(RemoteLockTargetListener.class);

	private final Map<String, Multimap<DatastoreLockTarget, DatastoreLockContext>> remotelyLockedContexts = Maps.newHashMap();

	private final AtomicBoolean active = new AtomicBoolean(false);
	
	public void register(final IEventBus eventBus) {
		if (active.compareAndExchange(false, true)) {
			eventBus.registerHandler(SystemNotification.ADDRESS, this);
		}
	}
	
	public void targetAcquired(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.put(target, context));
	}

	public void targetRemoved(final String clientId, final Iterable<DatastoreLockTarget> targets, final DatastoreLockContext context) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.computeIfAbsent(clientId, key -> HashMultimap.create());
		targets.forEach(target -> targetsForClient.remove(target, context));
	}
	
	private void clientLogout(final String clientId) {
		final Multimap<DatastoreLockTarget, DatastoreLockContext> targetsForClient = remotelyLockedContexts.remove(clientId);
		if (targetsForClient == null || targetsForClient.isEmpty()) {
			return;
		}

		LOGGER.warn("Disconnected client had locks granted, unlocking.");

		final IOperationLockManager lockManager = ApplicationContext.getInstance().getServiceChecked(IOperationLockManager.class);
		for (final Map.Entry<DatastoreLockTarget, DatastoreLockContext> targetContextPair : targetsForClient.entries()) {
			try {
				lockManager.unlock(targetContextPair.getValue(), targetContextPair.getKey());
			} catch (final IllegalArgumentException e) {
				LOGGER.error("Failed to unlock targets left after closed session.", e);
			}
		}
	}
	
	@Override
	public void handle(final IMessage message) {
		final Object body = message.body();
		if (!(body instanceof ClientConnectionNotification)) {
			return;
		}
		
		final ClientConnectionNotification notification = (ClientConnectionNotification) body;
		if (!notification.isJoining()) {
			clientLogout(notification.getClientId());
		}
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean isDisposed() {
		return false;
	}
}

@@ -1819,16 +1819,41 @@
 (false);
+%0A%0A%09private IEventBus bus;
 %0A%09%0A%09publ
@@ -1885,22 +1885,17 @@
 ventBus 
-eventB
+b
 us) %7B%0A%09%09
@@ -1898,16 +1898,17 @@
 %7B%0A%09%09if (
+!
 active.c
@@ -1948,14 +1948,28 @@
 %0A%09%09%09
-eventB
+this.bus = bus;%0A%09%09%09b
 us.r
@@ -3940,45 +3940,142 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub
+if (active.compareAndExchange(true, false)) %7B%0A%09%09%09this.bus.unregisterHandler(SystemNotification.ADDRESS, this);%0A%09%09%09this.bus = null;
 %0A%09%09
+%7D
 %0A%09%7D%0A
@@ -4126,20 +4126,27 @@
 %09return 
-false
+active.get()
 ;%0A%09%7D%0A%7D%0A

Fix service registration/disposal of RemoteLockTargetListener