package mg.dot.feufaro

import java.awt.EventQueue
import java.io.File
import javax.swing.JFileChooser
import javax.swing.filechooser.FileNameExtensionFilter

actual fun launchFilePicker(
    mimeTypes: Array<String>,
    initialDirectory: String?,
    onFileSelected: (path: String?) -> Unit
) {
    EventQueue.invokeLater {
        val fileChooser = JFileChooser()

        if(initialDirectory != null) {
            val initialFile = File(initialDirectory)

            fileChooser.currentDirectory = File(initialDirectory)
        }

        var isTxt = mimeTypes[0].equals("text/plain")
        var isImages = mimeTypes[0].equals("images/jpeg")

        if (mimeTypes.isNotEmpty()) {
            if (isTxt) {
                val filter = FileNameExtensionFilter("Fichiers texte", "txt")
                fileChooser.fileFilter = filter
            } else if (isImages) {
                val filter = FileNameExtensionFilter("Images", "jpg")
                fileChooser.fileFilter = filter
            }
        }

        fileChooser.dialogTitle = "Sélectionnez votre fichier"

        val result = fileChooser.showOpenDialog(null)

        if (result == JFileChooser.APPROVE_OPTION) {
            if(isTxt) {
                onFileSelected(fileChooser.selectedFile.absolutePath)
            } else if(isImages) {
//                onFileSelected(fileChooser.selectedFile.absoluteFile)
            }
        } else {
            onFileSelected(null)
        }
    }
}