package cz.muni.sutralog.repository

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import cz.muni.sutralog.data.PhotoPresentableListItem
import cz.muni.sutralog.database.PhotoDao
import cz.muni.sutralog.database.SutralogDatabase

class PhotoRepository (
    context: Context,
    private val photoDao: PhotoDao = SutralogDatabase.create(context).photoDao()
){
    fun savePhoto(photo: PhotoPresentableListItem): Long {
        return photoDao.savePhoto(photo.toPhotoEntity())
    }

    fun savePhotos(photos: List<PhotoPresentableListItem>) {
        photoDao.savePhotos(photos.map { it.toPhotoEntity() })
    }

    fun getPhotoById(id: Long): PhotoPresentableListItem {
        return photoDao.getPhotoById(id).toPresentablePhotoItem()
    }

    fun getAllPhotos(): List<PhotoPresentableListItem> {
        return photoDao.getAllPhotos().map { photoEntity ->
            photoEntity.toPresentablePhotoItem()
        }
    }

    fun getPhotosByRockId(rockId: Long): List<PhotoPresentableListItem> {
        return photoDao.getPhotosByRockId(rockId).map { photoEntity ->
            photoEntity.toPresentablePhotoItem()
        }
    }

    fun deletePhotoById(photoId: Long) {
        photoDao.deletePhotoById(photoId)
    }

    fun deletePhoto(photo: PhotoPresentableListItem) {
        photoDao.deletePhoto(photo.toPhotoEntity())
    }

    fun deletePhotos(photos: List<PhotoPresentableListItem>) {
        photoDao.deletePhotos(photos.map { it.toPhotoEntity() })
    }
}