package com.convenient.android.lib.ui.sample.media

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.convenient.android.common.extension.spGetString
import com.convenient.android.common.media.MediaBean
import com.convenient.android.common.media.config.MediaQueryConfig
import com.convenient.android.common.media.config.MediaSortOrder
import com.convenient.android.common.media.config.MediaSortType
import com.convenient.android.common.utils.string.SharedPreferencesSave
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.io.File
import java.util.*
import kotlin.collections.ArrayList

/**
 * @classname:
 * @author: LiuXiaoLong
 * @date: 2022/8/4
 * description:
 */
class MediaFilesViewModel(application: Application) : AndroidViewModel(application) {

    private var _config : MutableStateFlow<MediaQueryConfig> = MutableStateFlow(MediaQueryConfig())
    var config : StateFlow<MediaQueryConfig> = _config

    private var _dir : MutableStateFlow<String> = MutableStateFlow(application.spGetString(key = "dir"))
    var dir : StateFlow<String> = _dir

    private var _queryType : MutableStateFlow<MediaQueryType> = MutableStateFlow(MediaQueryType.FILES)
    var queryType : StateFlow<MediaQueryType> = _queryType

    private var _result : MutableStateFlow<List<MediaBean>> = MutableStateFlow(mutableListOf())
    var result :StateFlow<List<MediaBean>>  = _result


    fun changeQueryType(queryType: MediaQueryType){
        _queryType.value = queryType
        viewModelScope.launch {
            query()
        }
    }

    fun changeConfig(config: MediaQueryConfig){
        _config.value = config
    }


    fun changeQueryDir(dir : String){
        _dir.value = dir
    }

    fun addConfigIgnoreChildFiles(file: File?){
        file?.let {
            changeConfig(copyNewConfig {
                this.ignoreChildFiles = this.ignoreChildFiles.toMutableList().apply { add(file) }
            })
        }
    }

    fun removeConfigIgnoreChildFiles(file : File){
        changeConfig(copyNewConfig {
            ignoreChildFiles = ignoreChildFiles.toMutableList().apply {
                remove(file)
            }
        })
    }

    fun changeConfigIncludeFolder(includeFolder : Boolean){
        changeConfig(copyNewConfig {
            this.includeFolder = includeFolder
        })
    }

    fun changeSortType(sortType: MediaSortType){
        changeConfig(copyNewConfig {
            this.sortType = sortType
        })
    }
    fun changeOrder(order: MediaSortOrder){
        changeConfig(copyNewConfig {
            this.order = order
        })
    }


    private fun copyNewConfig(config: MediaQueryConfig.()-> Unit) : MediaQueryConfig{
        return _config.value.copy().also(config)
    }

    fun getSupportMimeTypesFiledTextValue(config: MediaQueryConfig) : String{
        val value = if (config.supportMimeTypes.isNotEmpty()) {
            buildString {
                config.supportMimeTypes.forEachIndexed { index, s ->
                    if (index != config.supportMimeTypes.lastIndex) {
                        append(s).append(",")
                    } else {
                        append(s)
                    }
                }
            }
        } else {
            ""
        }
        return value
    }

    suspend fun query(){
        _result.value = MediaFilesRepository.instance.queryMediaFiles(_queryType.value, _dir.value, _config.value)
    }



}