package com.github.premnirmal.ticker.portfolio

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.github.premnirmal.ticker.model.IStocksProvider
import com.github.premnirmal.ticker.network.data.Properties
import com.github.premnirmal.ticker.network.data.Quote
import com.github.premnirmal.ticker.repo.QuoteStorage
import kotlinx.coroutines.launch

class NotesViewModel constructor(
    private val stocksProvider: IStocksProvider,
    private val stocksStorage: QuoteStorage
) : ViewModel() {

    lateinit var symbol: String
    val quote: Quote?
        get() = stocksProvider.getStock(symbol)

    fun setNotes(notesText: String) {
        viewModelScope.launch {
            quote?.let {
                val properties = it.properties ?: Properties(
                    symbol
                )
                it.properties = properties
                properties.notes = notesText
                stocksStorage.saveQuoteProperties(properties)
            }
        }
    }
}
