package cz.muni.sutralog.repository

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import cz.muni.sutralog.data.RockPresentableListItem
import cz.muni.sutralog.database.RockDao
import cz.muni.sutralog.database.SutralogDatabase

class RockRepository (
    context: Context,
    private val rockDao: RockDao = SutralogDatabase.create(context).rockDao()
){
    fun insertRock(rock: RockPresentableListItem): Long {
        return rockDao.insertRock(rock.toRockEntity())
    }

    fun insertRocks(rocks: List<RockPresentableListItem>) {
        rockDao.insertRocks(rocks.map { it.toRockEntity() })
    }

    fun updateRock(rock: RockPresentableListItem): Int {
        return rockDao.updateRock(rock.toRockEntity())
    }

    fun updateRocks(rocks: List<RockPresentableListItem>) {
        rockDao.updateRocks(rocks.map { it.toRockEntity() })
    }

    fun getRockById(id: Long): RockPresentableListItem {
        return rockDao.getRockById(id).toPresentableRockItem()
    }

    fun getAllRocks(): List<RockPresentableListItem> {
        return rockDao.getAllRocks().map { rockEntity ->
                rockEntity.toPresentableRockItem()
            }
    }

    fun getRocksByCategoryId(categoryId: Long): List<RockPresentableListItem> {
        return rockDao.getRocksByCategoryId(categoryId).map { rockEntity ->
                rockEntity.toPresentableRockItem()
            }
    }

    fun deleteRockById(rockId: Long) {
        rockDao.deleteRockById(rockId)
    }

    fun deleteRock(rock: RockPresentableListItem) {
        rockDao.deleteRock(rock.toRockEntity())
    }
}