package io.nexilis.service.data.viewmodels

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.nexilis.service.data.entities.Buddy
import io.nexilis.service.data.repositories.BuddyRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

@HiltViewModel
class BuddyViewModel @Inject constructor(private val repository: BuddyRepository) : ViewModel() {

    val all: LiveData<List<Buddy>> = repository.all

    val me: LiveData<Buddy> = repository.me

    fun getBuddy(pin: String): LiveData<Buddy> {
        return repository.getBuddy(pin)
    }

    fun insert(buddy: Buddy) = viewModelScope.launch(Dispatchers.IO) {
        repository.insert(buddy)
    }

    fun signUp(username: String, password: String, completion: (Boolean, String) -> Unit) =
        viewModelScope.launch(Dispatchers.IO) {
            repository.signUp(username, password, completion)
        }

    fun signIn(
        api: String,
        username: String,
        password: String,
        completion: (Boolean, String) -> Unit
    ) =
        viewModelScope.launch(Dispatchers.IO) {
            repository.signIn(api, username, password, completion)
        }

    fun addFriend(pin: String) = viewModelScope.launch(Dispatchers.IO) {
        repository.addFriend(pin)
    }

    fun searchFriend(name: String, completion: (Boolean, String) -> Unit) =
        viewModelScope.launch(Dispatchers.IO) {
            repository.searchFriend(name, completion)
        }

    fun suggestFriend(lastSequence: String, completion: (Boolean, String) -> Unit) =
        viewModelScope.launch(Dispatchers.IO) {
            repository.suggestFriend(lastSequence, completion)
        }

    fun changeProfile(me: String, file: File, completion: (Boolean) -> Unit) =
        viewModelScope.launch(Dispatchers.IO) {
        repository.changeProfile(me, file, completion)
    }
}