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 com.ux.video.file.filerecovery.photo.ResultPhotosFiles
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<ResultPhotosFiles>>(emptySet())
    val selectedLiveData: LiveData<Set<ResultPhotosFiles>> = _selectedLiveData

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

    fun toggleSelection(isAdd: Boolean, resultPhotosFiles: ResultPhotosFiles) {
        val current = _selectedLiveData.value?.toMutableSet() ?: mutableSetOf()
        val currentDisplay = _selectedDisplayLiveData.value?.toMutableSet() ?: mutableSetOf()
        resultPhotosFiles.let {
            if (isAdd) {
                current.add(it)
                currentDisplay.add(it)
            } else {
                current.remove(it)
                currentDisplay.remove(it)
            }
        }

        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<ResultPhotosFiles>){
        _selectedDisplayLiveData.value = list.toSet()

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


    /**
     * 删除操作完成过后，对选中集合进行更新
     */
    fun afterDeleted(){
        val selected = _selectedLiveData.value.orEmpty().toMutableSet()
        val display = _selectedDisplayLiveData.value.orEmpty()

        selected.removeAll(display)
        _selectedLiveData.value = selected
        _selectedDisplayLiveData.value = emptySet()

    }

    /**
     * 详情页删除完毕，移除删除掉的数据
     */
    fun afterSingleDeleted(deletedItem:ResultPhotosFiles){
        val selected = _selectedLiveData.value.orEmpty().toMutableSet()
        val display = _selectedDisplayLiveData.value.orEmpty().toMutableSet()

        selected.remove(deletedItem)
        display.remove(deletedItem)
        _selectedLiveData.value = selected
        _selectedDisplayLiveData.value = display

    }



    fun checkIsSelect(resultPhotosFiles: ResultPhotosFiles): Boolean {
        val current = _selectedLiveData.value
        return current?.contains(resultPhotosFiles) == true
    }


}
