package com.bassbooster.soundeffects.equalizer.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bassbooster.soundeffects.equalizer.database.AppDatabase
import com.bassbooster.soundeffects.equalizer.database.EffectsData
import com.bassbooster.soundeffects.equalizer.database.EffectsRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MyViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: EffectsRepository
    val allData: LiveData<List<EffectsData>>
    private val _text = MutableLiveData<String>()
    val text: LiveData<String> = _text

    // Phương thức để cập nhật dữ liệu
    fun updateText(newText: String?) {
        _text.value = newText
    }

    init {
        val myDao = AppDatabase.getDatabase(application).mEffectsDAO()
        repository = EffectsRepository(myDao)
        allData = repository.getAllData()
    }

    fun insertData(myEntity: EffectsData) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.insert(myEntity)
        }
    }

    fun deleteData(myEntity: EffectsData) {
        viewModelScope.launch(Dispatchers.IO) {
            repository.delete(myEntity)
        }
    }
}
