package com.ux.video.file.filerecovery.recovery.ui.recoveryphoto

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

/**
 *
 * RecoveryActivity RecoveredFragment  共用
 */
class RecoveryPhotoViewModel : ViewModel() {

    //用于及时更新tab 显示恢复文件的数量
    private val _photoFiles = MutableLiveData<List<ResultDataFiles>>(emptyList())
    val photoFiles: LiveData<List<ResultDataFiles>> = _photoFiles

    private val _videoFiles = MutableLiveData<List<ResultDataFiles>>(emptyList())
    val videoFiles: LiveData<List<ResultDataFiles>> = _videoFiles

    private val _audioFiles = MutableLiveData<List<ResultDataFiles>>(emptyList())
    val audioFiles: LiveData<List<ResultDataFiles>> = _audioFiles

    private val _documentsFiles = MutableLiveData<List<ResultDataFiles>>(emptyList())
    val documentsFiles: LiveData<List<ResultDataFiles>> = _documentsFiles


    private val _selectedPhotoLiveData = MutableLiveData<Set<ResultDataFiles>>(emptySet())
    val selectedPhoto: LiveData<Set<ResultDataFiles>> = _selectedPhotoLiveData

    private val _selectedVideoLiveData = MutableLiveData<Set<ResultDataFiles>>(emptySet())
    val selectedVideo: LiveData<Set<ResultDataFiles>> = _selectedVideoLiveData

    private val _selectedAudioLiveData = MutableLiveData<Set<ResultDataFiles>>(emptySet())
    val selectedAudio: LiveData<Set<ResultDataFiles>> = _selectedAudioLiveData

    private val _selectedDocumentsLiveData = MutableLiveData<Set<ResultDataFiles>>(emptySet())
    val selectedDocuments: LiveData<Set<ResultDataFiles>> = _selectedDocumentsLiveData


    fun setRecoveredFiles(fileType: FileType, data: List<ResultDataFiles>) {
        when (fileType) {
            FileType.PHOTO -> _photoFiles.value = data
            FileType.VIDEO -> _videoFiles.value = data
            FileType.AUDIO -> _audioFiles.value = data
            FileType.DOCUMENT -> _documentsFiles.value = data
        }

    }


    fun toggleSelection(fileType: FileType, isAdd: Boolean, resultDataFiles: ResultDataFiles) {
        getSelectedList(fileType).let { selectList ->
            val current = selectList.value?.toMutableSet() ?: mutableSetOf()
            resultDataFiles.let {
                if (isAdd) {
                    current.add(it)
                } else {
                    current.remove(it)
                }
            }
            selectList.value = current.toSet()
        }

    }

    fun clearSelected(fileType: FileType) {
        getSelectedList(fileType).let {
            it.value = it.value?.toMutableSet()?.apply {
                clear()
            } ?: emptySet()

        }
    }

    fun removeSelectedFromSizeList(
        list: List<ResultDataFiles>,
        selectedLiveData: Set<ResultDataFiles>
    ): List<ResultDataFiles> {
        return list.filterNot { it in selectedLiveData }
    }


    fun checkIsSelect(fileType:  FileType, resultDataFiles: ResultDataFiles): Boolean {
        return when (fileType) {
            FileType.PHOTO  -> _selectedPhotoLiveData
            FileType.VIDEO-> _selectedVideoLiveData
            FileType.AUDIO -> _selectedAudioLiveData
            FileType.DOCUMENT -> _selectedDocumentsLiveData
        }.let {
            val current = it.value
            current?.contains(resultDataFiles) == true
        }
    }

    private fun getSelectedList(fileType: FileType): MutableLiveData<Set<ResultDataFiles>> {
        return when (fileType) {
            FileType.PHOTO -> _selectedPhotoLiveData
            FileType.VIDEO -> _selectedVideoLiveData
            FileType.AUDIO -> _selectedAudioLiveData
            FileType.DOCUMENT -> _selectedDocumentsLiveData
        }
    }
}