package com.dumpster.cleaner.ui.recyclebin

import com.dumpster.cleaner.MyApplication
import com.dumpster.cleaner.R
import com.dumpster.cleaner.ui.recovery.FileScanResultActivity.Companion.SCAN_AUDIOS
import com.dumpster.cleaner.ui.recovery.FileScanResultActivity.Companion.SCAN_DOCUMENTS
import com.dumpster.cleaner.ui.recovery.FileScanResultActivity.Companion.SCAN_PHOTOS
import com.dumpster.cleaner.ui.recovery.FileScanResultActivity.Companion.SCAN_VIDEOS
import java.io.File

object RecycleBinUtils {

    val mImageStr: Set<String> = HashSet(
        mutableListOf(".jpg", ".jpeg", ".png", ".gif", ".bmp")
    )
    val mVideoStr: Set<String> = HashSet(
        mutableListOf(".mp4", ".avi", ".mov", ".mkv")
    )
    val mAudioStr: Set<String> = HashSet(
        mutableListOf(".mp3", ".flac", ".wav")
    )

    val mDocumentStr: Set<String> = HashSet(mutableListOf(".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf"))

    fun File.getType(): Int {
        if (mImageStr.contains("." + extension)) {
            return SCAN_PHOTOS
        }
        if (mVideoStr.contains("." + extension)) {
            return SCAN_VIDEOS
        }
        if (mAudioStr.contains("." + extension)) {
            return SCAN_AUDIOS
        }
        if (mDocumentStr.contains("." + extension)) {
            return SCAN_DOCUMENTS
        }

        return -1
    }

    fun File.getImgRes(): Int {
        return when (extension) {
            "mp3" -> R.drawable.mp3
            "flac" -> R.drawable.flac
            "doc", "docx" -> R.drawable.doc
            "xls", "xlsx" -> R.drawable.xls
            "ppt", "pptx" -> R.drawable.ppt
            else -> R.drawable.icon_file
        }
    }

    fun getRecycleBinData(): List<File> {
        val recycleBinDir = File(MyApplication.Companion.appContext.filesDir, "recycleBin")
        if (recycleBinDir.exists()) {
            return recycleBinDir.listFiles()?.toList() ?: listOf()
        } else {
            return listOf()
        }
    }

    fun addRecycleBinFile(file: File) {
        val recycleBinDir = File(MyApplication.Companion.appContext.filesDir, "recycleBin")
        if (!recycleBinDir.exists()) {
            recycleBinDir.mkdirs()
        }

        val recycleBinFile = File(recycleBinDir, file.name)

        file.copyTo(recycleBinFile, true)
    }


}