package com.ux.video.file.filerecovery.sort

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.ux.video.file.filerecovery.db.ResultData
import com.ux.video.file.filerecovery.db.ResultDataFiles
import com.ux.video.file.filerecovery.result.ScanResultRepository
import com.ux.video.file.filerecovery.utils.Common

class SortingViewModel : 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

    val sortingData:  MutableLiveData<List<ResultDataFiles>> = ScanResultRepository.sortingData

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

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

    fun toggleSelection(isAdd: Boolean, resultDataFiles: ResultDataFiles) {
        val current = _selectedLiveData.value?.toMutableSet() ?: mutableSetOf()
        val currentDisplay = _selectedDisplayLiveData.value?.toMutableSet() ?: mutableSetOf()
        resultDataFiles.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<ResultDataFiles>){
        _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: ResultDataFiles){
        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(resultDataFiles: ResultDataFiles): Boolean {
        val current = _selectedLiveData.value
        return current?.contains(resultDataFiles) == true
    }


}