package com.isidroid.c23.domain.use_case

import android.content.Context
import com.isidroid.c23.JobNotFoundException
import com.isidroid.c23.constant.Argument
import com.isidroid.c23.data.mapper.createListItem
import com.isidroid.c23.ext.isDebug
import com.isidroid.core.FlowResult
import com.isidroid.job.repository.JobRepository
import com.isidroid.location.repository.LocationRepository
import com.isidroid.spot.repository.SpotRepository
import com.isidroid.ui.maps.ext.addKilometerToLatLng
import com.isidroid.ui.maps.model.MapMarker
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DetailsUseCase @Inject constructor(
    @ApplicationContext private val context: Context,
    private val jobRepository: JobRepository,
    private val spotRepository: SpotRepository,
    private val locationRepository: LocationRepository,
) {
    fun loadDetails(id: String) = flow {
        emit(FlowResult.Loading)

        val job = jobRepository.readLocalList(ids = listOf(id)).firstOrNull() ?: throw JobNotFoundException()
        val richSpot = spotRepository.findLocalRichSpots(ids = listOf(job.spotId))?.firstOrNull()

        val result = job.createListItem(context = context, richSpot = richSpot)
        val currentLocation = locationRepository.getCurrentLocation()

        val location = if (isDebug() && richSpot?.spot != null)
            addKilometerToLatLng(richSpot.spot.lat, richSpot.spot.lng, distance = 0.2)
        else
            currentLocation

        val mapMarker = richSpot?.spot?.let { spot ->
            MapMarker(id = spot.id, lat = spot.lat, lng = spot.lng, name = spot.name)
        }

        emit(
            FlowResult.Success(
                mapOf(
                    Argument.INFO to result,
                    Argument.LATITUDE to location.first,
                    Argument.LONGITUDE to location.second,
                    Argument.MARKER to mapMarker
                )
            )
        )
    }
}