package com.isidroid.c23.ui.screen.content

import android.net.Uri
import androidx.lifecycle.viewModelScope
import androidx.room.util.copy
import com.isidroid.c23.domain.use_case.ContentUseCase
import com.isidroid.c23.ext.isDebug
import com.isidroid.core.vm.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
import java.util.UUID
import javax.inject.Inject

@HiltViewModel
class ContentViewModel @Inject constructor(
    private val useCase: ContentUseCase
) : BaseViewModel<ContentContract.Event, ContentContract.State, ContentContract.Effect>() {
    init {
        viewModelScope.launch { create() }
    }

    override val isDebug: Boolean = isDebug()
    override fun setInitialState(): ContentContract.State = ContentContract.State()
    override suspend fun handleEvents(event: ContentContract.Event) {
        when (event) {
            is ContentContract.Event.MultipleContents -> onContentSelected(event.uris)
            is ContentContract.Event.PickContent -> setState {
                copy(
                    galleryHash = if (event.photo) UUID.randomUUID().toString() else null,
                    documentHash = if (event.documents) UUID.randomUUID().toString() else null,
                )
            }

            is ContentContract.Event.OpenMap -> setEffect { ContentContract.Effect.Navigation.ToMap(event.lat, event.lng, event.spotCode) }
            ContentContract.Event.GoBack -> setEffect { ContentContract.Effect.Navigation.ToBack }
        }
    }

    private fun onContentSelected(uris: List<Uri>) {
        if (uris.isEmpty())
            return

        setState { copy(galleryHash = null, documentHash = null) }
        setEffect { ContentContract.Effect.Navigation.ToRenderPreview(uris.joinToString()) }
    }

    private suspend fun create() {
        useCase.create().flowOn(Dispatchers.IO).collect { data ->
            if (data == null)
                setEffect { ContentContract.Effect.Navigation.ToMap() }
            else
                setState { copy(richSpot = data) }
        }
    }
}