/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */
package net.ccbluex.liquidbounce.config.types

import com.google.gson.Gson
import com.google.gson.JsonElement
import net.ccbluex.liquidbounce.config.ConfigSystem
import net.ccbluex.liquidbounce.config.autoconfig.AutoConfig
import net.ccbluex.liquidbounce.config.gson.stategies.Exclude
import net.ccbluex.liquidbounce.lang.LanguageManager
import org.lwjgl.PointerBuffer
import org.lwjgl.system.MemoryStack
import org.lwjgl.util.tinyfd.TinyFileDialogs
import java.io.File

/**
 * A value file input that supports different file dialog modes and optional file type filtering.
 * It will be treated as a relative path if it starts with [ConfigSystem.rootFolder].
 *
 * @param name The name of the configuration option.
 * @param default The default selected file. The default value is [ConfigSystem.rootFolder].
 * @param dialogMode Specifies the type of file dialog to show (e.g., open file, save file, choose folder).
 * @param supportedExtensions A set of allowed file extensions (without the dot), e.g., `setOf("txt", "json")`.
 *        Use `null` to allow any file type. This is ignored if [dialogMode] is set to select directories.
 */
class FileValue(
    name: String,
    default: File?,
    @Exclude val dialogMode: FileDialogMode,
    @Exclude val supportedExtensions: Set<String>?,
) : Value<File>(
    name,
    defaultValue = normalizeToClientFolder(default ?: ConfigSystem.rootFolder),
    valueType = ValueType.FILE
) {
    init {
        onChange(::normalizeToClientFolder)
    }

    override fun deserializeFrom(gson: Gson, element: JsonElement) {
        // File value is not allowed to be deserialized from AutoConfig.
        if (!AutoConfig.loadingNow) {
            super.deserializeFrom(gson, element)
        }
    }

    /**
     * The absolute file path.
     *
     * If the file is not absolute, it is resolved relative to the [ConfigSystem.rootFolder].
     */
    val absoluteFile: File get() = if (inner.isAbsolute) inner else ConfigSystem.rootFolder.resolve(inner)

    companion object {
        @JvmStatic
        private fun normalizeToClientFolder(file: File): File {
            return if (file.startsWith(ConfigSystem.rootFolder)) {
                file.relativeTo(ConfigSystem.rootFolder)
            } else {
                file
            }
        }
    }
}

/**
 * Defines the mode of the file dialog used in a [FileValue].
 *
 * This controls how the file chooser behaves in the UI (e.g., ClickGUI or similar):
 *
 * - [OPEN_FILE]: Opens a dialog to select an existing file.
 * - [SAVE_FILE]: Opens a dialog to choose a file path for saving.
 * - [OPEN_DIRECTORY]: Opens a dialog to select an existing directory. File extension filters are ignored in this mode.
 */
enum class FileDialogMode(
    private val translationKey: String,
    private val fallbackTitle: String
) {
    OPEN_FILE("liquidbounce.fileDialog.mode.openFile", "Open File") {
        override fun selectFilesRaw(extensions: Iterable<String>?) = withFilterPatterns(extensions) {
            TinyFileDialogs.tinyfd_openFileDialog(
                title,
                ConfigSystem.rootFolder.path,
                it,
                null,
                false
            )
        }
    },
    SAVE_FILE("liquidbounce.fileDialog.mode.saveFile", "Save File As") {
        override fun selectFilesRaw(extensions: Iterable<String>?) = withFilterPatterns(extensions) {
            TinyFileDialogs.tinyfd_saveFileDialog(
                title,
                ConfigSystem.rootFolder.path,
                it,
                null
            )
        }
    },
    OPEN_DIRECTORY("liquidbounce.fileDialog.mode.openDirectory", "Select Folder") {
        override fun selectFilesRaw(extensions: Iterable<String>?) = TinyFileDialogs.tinyfd_selectFolderDialog(
            title,
            ConfigSystem.rootFolder.path,
        )
    };

    val title: String
        get() = LanguageManager.getLanguage()?.getOrDefault(translationKey, fallbackTitle) ?: fallbackTitle

    protected abstract fun selectFilesRaw(extensions: Iterable<String>?): String?

    fun selectFiles(extensions: Iterable<String>? = null): List<String> {
        // using `|` as a separator because tinyfd separate multiple file selection
        return selectFilesRaw(extensions)?.split('|') ?: emptyList()
    }

    companion object {
        private fun <T> withFilterPatterns(
            extensions: Iterable<String>?,
            block: (PointerBuffer?) -> T,
        ): T? {
            val patterns = extensions?.map { "*.$it" }.orEmpty()
            if (patterns.isEmpty()) {
                return block(null)
            }

            return MemoryStack.stackPush().use { stack ->
                val buffer = stack.mallocPointer(patterns.size)

                for (pattern in patterns) {
                    buffer.put(stack.ASCII(pattern))
                }

                block(buffer.flip())
            }
        }
    }
}
