import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.apps.quicksketchtrace.database.Local
import com.apps.quicksketchtrace.database.Upload
import kotlinx.coroutines.launch

class MainViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: DataRepository by lazy {
        DataRepository(
            AppDataBase.getDatabase(application).localDao(),
            AppDataBase.getDatabase(application).uploadDao()
        )
    }
    private val _locals = MutableLiveData<List<Local>>()
    val locals: LiveData<List<Local>> get() = _locals
    private val _uploads = MutableLiveData<List<Upload>>()
    val uploads: LiveData<List<Upload>> get() = _uploads
    val isLocal: LiveData<Boolean> get() = _isLocal
    private val _isLocal = MutableLiveData<Boolean>()
    private val _isUpload = MutableLiveData<Boolean>()
    val isUpload: LiveData<Boolean> get() = _isUpload
    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> get() = _errorMsg

    init {
        refreshLocals()
        refreshUploads()
    }

    fun refreshLocals() {
        viewModelScope.launch {
            try {
                _locals.value = repository.getAllLocals()
                Log.d("ViewModel", "New data size: ${repository.getAllLocals().size}")
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "error data from local:${e.message}"
            }
        }
    }

    private fun refreshUploads() {
        viewModelScope.launch {
            try {
                _uploads.value = repository.getAllUploads()
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "error data from upload:${e.message}"
            }
        }
    }

    fun addLocal(path: String) {
        viewModelScope.launch {
            val local=repository.getLocalByPath(path)
            if(local==null){
                try {
                    repository.insertLocal(Local(path = path))
                    refreshLocals()
                    _errorMsg.value = null
                } catch (e: Exception) {
                    _errorMsg.value = "add local error:${e.message}"
                }
            }
        }
    }

    fun deleteLocalPath(path: String) {
        viewModelScope.launch {
            try {
                repository.deleteLocalByPath(path)
                refreshLocals()
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "delete local error:${e.message}"
            }
        }
    }

    fun addUpload(path: String) {
        viewModelScope.launch {
            val upload=repository.getUploadByPath(path)
            if(upload==null){
                try {
                    repository.insertUpload(Upload(path = path))
                    refreshUploads()
                    _errorMsg.value = null
                } catch (e: Exception) {
                    _errorMsg.value = "add upload error:${e.message}"
                }
            }
        }
    }

    fun deleteUploadPath(path: String) {
        viewModelScope.launch {
            try {
                repository.deleteUploadByPath(path)
                refreshUploads()
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "delete local error:${e.message}"
            }
        }
    }
    fun checkIsLocal(path: String){
        viewModelScope.launch {
            try {
                val local = repository.getLocalByPath(path)
                _isLocal.value = local != null
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "search error: ${e.message}"
                _isLocal.value = false  // 出错时默认设为false
            }
        }
    }
    fun checkIsUpload(path: String){
        viewModelScope.launch {
            try {
                val upload = repository.getUploadByPath(path)
                _isUpload.value = upload != null
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "search error: ${e.message}"
                _isUpload.value = false
            }
        }
    }
}