/*
 * Copyright (c) 2023 New Vector Ltd
 *
 * 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 io.element.android.libraries.sessionstorage.impl.observer

import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.sessionstorage.api.SessionStore
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.libraries.sessionstorage.api.toUserListFlow
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.CopyOnWriteArraySet
import javax.inject.Inject

@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class)
class DefaultSessionObserver @Inject constructor(
    private val sessionStore: SessionStore,
    private val coroutineScope: CoroutineScope,
    private val dispatchers: CoroutineDispatchers,
) : SessionObserver {
    // Keep only the userId
    private var currentUsers: Set<String>? = null

    init {
        observeDatabase()
    }

    private val listeners = CopyOnWriteArraySet<SessionListener>()
    override fun addListener(listener: SessionListener) {
        listeners.add(listener)
    }

    override fun removeListener(listener: SessionListener) {
        listeners.remove(listener)
    }

    private fun observeDatabase() {
        coroutineScope.launch {
            withContext(dispatchers.io) {
                sessionStore.sessionsFlow()
                    .toUserListFlow()
                    .map { it.toSet() }
                    .onEach { newUserSet ->
                        val currentUserSet = currentUsers
                        if (currentUserSet != null) {
                            // Compute diff
                            // Removed user
                            val removedUsers = currentUserSet - newUserSet
                            removedUsers.forEach { removedUser ->
                                listeners.onEach { listener ->
                                    listener.onSessionDeleted(removedUser)
                                }
                            }
                            // Added user
                            val addedUsers = newUserSet - currentUserSet
                            addedUsers.forEach { addedUser ->
                                listeners.onEach { listener ->
                                    listener.onSessionCreated(addedUser)
                                }
                            }
                        }

                        currentUsers = newUserSet
                    }
                    .collect()
            }
        }
    }
}
