package com.apps.sketchmaster.data

import AppDataBase
import EntityRepository
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 kotlinx.coroutines.launch

class MainViewModel(application: Application) : AndroidViewModel(application) {
    private val repository: EntityRepository by lazy {
        EntityRepository(
            AppDataBase.getDatabase(application).baseDao(),
            AppDataBase.getDatabase(application).customizeDao()
        )
    }
    private val _locals = MutableLiveData<List<Base>>()
    val locals: LiveData<List<Base>> get() = _locals
    private val _customizes = MutableLiveData<List<Customize>>()
    val customizes: LiveData<List<Customize>> get() = _customizes
    val isBase: LiveData<Boolean> get() = _isBase
    private val _isBase = MutableLiveData<Boolean>()
    private val _isCustomize = MutableLiveData<Boolean>()
    val isCustomize: LiveData<Boolean> get() = _isCustomize
    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> get() = _errorMsg

    init {
        refreshBases()
        refreshCustomizes()
    }

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

    private fun refreshCustomizes() {
        viewModelScope.launch {
            try {
                _customizes.value = repository.getAllCustomizes()
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "error data from customize:${e.message}"
            }
        }
    }

    fun addBase(path: String) {
        viewModelScope.launch {
            val local=repository.getBaseByPath(path)
            if(local==null){
                try {
                    repository.insertBase(Base(url = path))
                    refreshBases()
                    _errorMsg.value = null
                } catch (e: Exception) {
                    _errorMsg.value = "add local error:${e.message}"
                }
            }
        }
    }

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

    fun addCustomize(path: String) {
        viewModelScope.launch {
            val customize=repository.getCustomizeByPath(path)
            if(customize==null){
                try {
                    repository.insertCustomize(Customize(url = path))
                    refreshCustomizes()
                    _errorMsg.value = null
                } catch (e: Exception) {
                    _errorMsg.value = "add customize error:${e.message}"
                }
            }
        }
    }

    fun deleteCustomizePath(path: String) {
        viewModelScope.launch {
            try {
                repository.deleteCustomizeByPath(path)
                refreshCustomizes()
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "delete local error:${e.message}"
            }
        }
    }
    fun checkIsBase(path: String){
        viewModelScope.launch {
            try {
                val local = repository.getBaseByPath(path)
                _isBase.value = local != null
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "search error: ${e.message}"
                _isBase.value = false  // 出错时默认设为false
            }
        }
    }
    fun checkIsCustomize(path: String){
        viewModelScope.launch {
            try {
                val customize = repository.getCustomizeByPath(path)
                _isCustomize.value = customize != null
                _errorMsg.value = null
            } catch (e: Exception) {
                _errorMsg.value = "search error: ${e.message}"
                _isCustomize.value = false
            }
        }
    }
}