package pt.whatsms.gateway.modules.gateway

import android.util.Log
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import pt.whatsms.gateway.domain.EntitySource
import pt.whatsms.gateway.modules.events.EventBus
import pt.whatsms.gateway.modules.events.EventsReceiver
import pt.whatsms.gateway.modules.gateway.events.DeviceRegisteredEvent
import pt.whatsms.gateway.modules.gateway.events.MessageEnqueuedEvent
import pt.whatsms.gateway.modules.gateway.events.SettingsUpdatedEvent
import pt.whatsms.gateway.modules.gateway.events.WebhooksUpdatedEvent
import pt.whatsms.gateway.modules.gateway.services.SSEForegroundService
import pt.whatsms.gateway.modules.gateway.workers.PullMessagesWorker
import pt.whatsms.gateway.modules.gateway.workers.SendStateWorker
import pt.whatsms.gateway.modules.gateway.workers.SettingsUpdateWorker
import pt.whatsms.gateway.modules.gateway.workers.WebhooksUpdateWorker
import pt.whatsms.gateway.modules.messages.events.MessageStateChangedEvent
import pt.whatsms.gateway.modules.ping.events.PingEvent
import org.koin.core.component.get

class EventsReceiver : EventsReceiver() {

    private val settings = get<GatewaySettings>()

    override suspend fun collect(eventBus: EventBus) {
        coroutineScope {
            launch {
                Log.d("EventsReceiver", "launched MessageEnqueuedEvent")
                eventBus.collect<MessageEnqueuedEvent> { event ->
                    Log.d("EventsReceiver", "Event: $event")

                    if (!settings.enabled) return@collect

                    PullMessagesWorker.start(get())
                }
            }
            launch {
                Log.d("EventsReceiver", "launched MessageStateChangedEvent")
                val allowedSources = setOf(EntitySource.Cloud, EntitySource.Gateway)
                eventBus.collect<MessageStateChangedEvent> { event ->
                    Log.d("EventsReceiver", "Event: $event")

                    if (!settings.enabled) return@collect

                    if (event.source !in allowedSources) return@collect

                    SendStateWorker.start(get(), event.id)
                }
            }

            launch {
                Log.d("EventsReceiver", "launched PingEvent")
                eventBus.collect<PingEvent> {
                    Log.d("EventsReceiver", "Event: $it")

                    if (!settings.enabled) return@collect

                    PullMessagesWorker.start(get())
                }
            }

            launch {
                Log.d("EventsReceiver", "launched WebhooksUpdatedEvent")
                eventBus.collect<WebhooksUpdatedEvent> {
                    Log.d("EventsReceiver", "Event: $it")

                    if (!settings.enabled) return@collect

                    WebhooksUpdateWorker.start(get())
                }
            }

            launch {
                Log.d("EventsReceiver", "launched SettingsUpdatedEvent")
                eventBus.collect<SettingsUpdatedEvent> {
                    Log.d("EventsReceiver", "Event: $it")

                    if (!settings.enabled) return@collect

                    SettingsUpdateWorker.start(get())
                }
            }

            launch {
                Log.d("EventsReceiver", "launched DeviceRegisteredEvent")
                eventBus.collect<DeviceRegisteredEvent> {
                    Log.d("EventsReceiver", "Event: $it")

                    if (!settings.enabled) return@collect
                    if (settings.fcmToken != null) return@collect

                    SSEForegroundService.start(get())
                }
            }
        }

    }
}