package com.zxhy.hfilemanagermaster.knife

import android.content.Context
import android.os.Build
import android.os.Environment
import com.zxhy.hfilemanagermaster.data.MediaDataC
import com.zxhy.hfilemanagermaster.data.file2MediaDataC
import java.io.File

fun Context.getPublicMedia(): ArrayList<MediaDataC> {
    val downloads = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
    val dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
    val movies = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
    val music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
    val documents = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
    val picture = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
    val screenshots = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_SCREENSHOTS)
    } else {
        File("")
    }

    val list = arrayListOf<MediaDataC>()
    if (downloads.exists()) {
        traverseFolder(downloads).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }
    if (dcim.exists()) {
        traverseFolder(dcim).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }

    if (movies.exists()) {
        traverseFolder(movies).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }

    if (music.exists()) {
        traverseFolder(music).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }

    if (documents.exists()) {
        traverseFolder(documents).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }


    if (picture.exists()) {
        traverseFolder(picture).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }

    if (screenshots.exists()) {
        traverseFolder(screenshots).forEach {
            if (it.isFile) list.add(it.file2MediaDataC(this))
        }
    }
    return list
}

