package com.simplecleaner.app.ui.largefile

import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import com.simplecleaner.app.bean.FileBean
import com.simplecleaner.app.utils.MediaUtils
import com.simplecleaner.app.utils.MineTypeUtils
import java.util.Calendar

class LargeFileCleanViewModel : ViewModel() {
    private var allData: List<FileBean> = emptyList()
    private var filteredList: List<FileBean> = emptyList()

    var typePosition = 0
    var sizePosition = 0
    var timePosition = 0
    var largeFileSize: Long = 1024 * 1024 * 10

    fun loadFiles(context: Context, onLoaded: (() -> Unit)? = null) {
        viewModelScope.launch(Dispatchers.IO) {
            allData = MediaUtils.findFiles(context, largeFileSize)
            filterData()
            withContext(Dispatchers.Main) {
                onLoaded?.invoke()
            }
        }
    }

    fun filterData() {
        filteredList = filterType(allData, typePosition)
            .let { filterSize(it, sizePosition) }
            .let { filterTime(it, timePosition) }
    }

    fun getFilteredList(): List<FileBean> = filteredList

    fun toggleSelect(position: Int) {
        if (position in filteredList.indices) {
            filteredList[position].isSelected = !filteredList[position].isSelected
        }
    }

    private fun filterType(list: List<FileBean>, position: Int): List<FileBean> {
        return when (position) {
            1 -> list.filter { MineTypeUtils.isImage(it.mineType) }
            2 -> list.filter { MineTypeUtils.isVideo(it.mineType) }
            3 -> list.filter { MineTypeUtils.isAudio(it.mineType) }
            4 -> list.filter { MineTypeUtils.isDoc(it.mineType) }
            5 -> list.filter { MineTypeUtils.isApk(it.mineType) }
            6 -> list.filterNot { file ->
                MineTypeUtils.mineTypes.any { mimeType -> file.mineType.startsWith(mimeType) }
            }
            else -> list
        }
    }

    private fun filterSize(list: List<FileBean>, position: Int): List<FileBean> {
        return when (position) {
            1 -> list.filter { it.length >= largeFileSize * 2 }
            2 -> list.filter { it.length >= largeFileSize * 5 }
            3 -> list.filter { it.length >= largeFileSize * 10 }
            4 -> list.filter { it.length >= largeFileSize * 50 }
            else -> list.filter { it.length >= largeFileSize }
        }
    }

    private fun filterTime(list: List<FileBean>, position: Int): List<FileBean> {
        return when (position) {
            1 -> list.filter { it.lastModified > Calendar.getInstance().apply { add(Calendar.WEEK_OF_YEAR, -1) }.timeInMillis }
            2 -> list.filter { it.lastModified > Calendar.getInstance().apply { add(Calendar.MONTH, -1) }.timeInMillis }
            3 -> list.filter { it.lastModified > Calendar.getInstance().apply { add(Calendar.MONTH, -3) }.timeInMillis }
            4 -> list.filter { it.lastModified > Calendar.getInstance().apply { add(Calendar.MONTH, -6) }.timeInMillis }
            5 -> list.filter { it.lastModified > Calendar.getInstance().apply { add(Calendar.YEAR, -1) }.timeInMillis }
            else -> list
        }
    }
} 