package cz.muni.sutralog.repository

import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import cz.muni.sutralog.data.CategoryPresentableListItem
import cz.muni.sutralog.database.CategoryDao
import cz.muni.sutralog.database.SutralogDatabase

class CategoryRepository (
    context: Context,
    private val categoryDao: CategoryDao = SutralogDatabase.create(context).categoryDao()
){

    fun insertCategory(category: CategoryPresentableListItem): Long {
        return categoryDao.insertCategory(category.toCategoryEntity())
    }

    fun insertCategories(categories: List<CategoryPresentableListItem>) {
        categoryDao.insertCategories(categories.map { it.toCategoryEntity() })
    }

    fun updateCategory(category: CategoryPresentableListItem): Int {
        return categoryDao.updateCategory(category.toCategoryEntity())
    }

    fun updateCategories(categories: List<CategoryPresentableListItem>) {
        categoryDao.updateCategories(categories.map { it.toCategoryEntity() })
    }

    fun getAllCategories(): List<CategoryPresentableListItem> {
        return categoryDao.getAllCategories().map {
                it.toPresentableCategoryItem()
            }
    }

    fun getCategoryById(id: Long): CategoryPresentableListItem {
        return categoryDao.getCategoryById(id).toPresentableCategoryItem()
    }

    fun deleteCategoryById(id: Long) {
        categoryDao.deleteCategoryById(id)
    }

    fun deleteCategory(category: CategoryPresentableListItem) {
        categoryDao.deleteCategory(category.toCategoryEntity())
    }
}