package com.ux.video.file.filerecovery.utils

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.ux.video.file.filerecovery.photo.ResultPhotos
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow


class ScanRepository : ViewModel() {
//    private val _selectedFlow = MutableStateFlow<MutableSet<String>>(mutableSetOf())
//    val selectedFlow: StateFlow<MutableSet<String>> = _selectedFlow
//
//
//    private val _selectedDisplayFlow = MutableStateFlow<MutableSet<String>>(mutableSetOf())
//    val selectedDisplayFlow: StateFlow<MutableSet<String>> = _selectedDisplayFlow



    private val _selectedLiveData = MutableLiveData<Set<String>>(emptySet())
    val selectedLiveData: LiveData<Set<String>> = _selectedLiveData

    // 当前筛选显示的选中项
    private val _selectedDisplayLiveData = MutableLiveData<Set<String>>(emptySet())
    val selectedDisplayLiveData: LiveData<Set<String>> = _selectedDisplayLiveData

    fun toggleSelection(isAdd: Boolean, path: String) {
        val current = _selectedLiveData.value?.toMutableSet() ?: mutableSetOf()
        val currentDisplay = _selectedDisplayLiveData.value?.toMutableSet() ?: mutableSetOf()

        if (isAdd) {
            current.add(path)
            currentDisplay.add(path)
        } else {
            current.remove(path)
            currentDisplay.remove(path)
        }
        Common.showLog( "toggleSelection------------ _selectedDisplayFlow=${_selectedDisplayLiveData.value?.size}  _selectedFlow=${_selectedLiveData.value?.size} ")
        _selectedLiveData.value = current.toSet()
        _selectedDisplayLiveData.value = currentDisplay.toSet()

//        Log.d("CallTrace", Log.getStackTraceString(Exception("Call trace")))

    }


    /**
     * 数据筛选后重置当前显示的选中集合
     */
    fun filterResetDisplayFlow(list: MutableSet<String>){
        _selectedDisplayLiveData.value = list.toSet()

        Common.showLog( "筛选后重置 _selectedDisplayFlow=${_selectedDisplayLiveData.value?.size}  _selectedFlow=${_selectedLiveData.value?.size} ")
    }



    fun checkIsSelect(path: String): Boolean {
        val current = _selectedDisplayLiveData.value
        return current?.contains(path) == true
    }


    companion object {
        val instance by lazy { ScanRepository() }
    }
}
