import de.undercouch.gradle.tasks.download.Download
import org.gradle.kotlin.dsl.prepareSandbox
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
import org.jetbrains.intellij.platform.gradle.extensions.IntelliJPlatformExtension
import org.jetbrains.kotlin.gradle.dsl.JvmTarget


val slintVersion: String by project
//github url
val slintGithubUrl = uri("https://github.com/slint-ui/slint/releases/download/v$slintVersion")

val downloadDir: Provider<Directory> = layout.buildDirectory.dir("downloads/slint-lsp")

val extractedDir: Provider<Directory> = downloadDir.map { it.dir("extracted") }


plugins {
    id("java")
    id("org.jetbrains.kotlin.jvm") version "2.1.20"
    id("org.jetbrains.intellij.platform") version "2.10.2"
    id("de.undercouch.download") version "5.6.0"
}


val slintViewerFilenames = arrayOf(
    "slint-lsp-linux.tar.gz" to "slint-lsp-linux",
    "slint-lsp-macos.tar.gz" to "slint-lsp-macos",
    "slint-lsp-windows-x86_64.zip" to "slint-lsp-windows.exe",
)
group = "me.zhouxi"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    intellijPlatform {
        defaultRepositories()
    }
}

// Read more: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin.html
dependencies {
    intellijPlatform {
        intellijIdea("2025.2.4")
        testFramework(TestFrameworkType.Platform)

        // Add plugin dependencies for compilation here:
        bundledPlugin("org.jetbrains.plugins.textmate")

    }
}

intellijPlatform {
    sandboxContainer = layout.projectDirectory.dir("sandbox")
    pluginConfiguration {
        ideaVersion {
            sinceBuild = "252.25557"
        }

        changeNotes = """
            Initial version
        """.trimIndent()
    }
}


tasks {
    withType<JavaCompile> {
        sourceCompatibility = "21"
        targetCompatibility = "21"
    }

    val downloadTasks = slintViewerFilenames.map { (filename, _) ->
        register<Download>("downloadSlint-${filename.removeSuffix(".tar.gz").removeSuffix(".zip")}") {
            src("$slintGithubUrl/$filename")
            dest(downloadDir.get().file(filename))
            overwrite(false)
            onlyIfModified(true)
        }
    }
    val processLspResources by registering(Copy::class) {

        dependsOn(downloadTasks)

        slintViewerFilenames.forEach { (filename, renamed) ->
            val fileTarget = downloadDir.get().file(filename)
            val fileTree = if (filename.endsWith("zip")) zipTree(fileTarget) else tarTree(fileTarget)
            from(fileTree) {
                // 匹配压缩包内的 slint-lsp 文件夹及其内容
                include("**/slint-lsp*")
                // 展平目录并重命名
                eachFile {
                    path = renamed
                }
                includeEmptyDirs = false
            }
        }
        into(extractedDir)
    }
    prepareSandbox {
        dependsOn(processLspResources)

        from(extractedDir.get()) {
            into("slint/lsp")
        }
    }
}

kotlin {
    compilerOptions {
        jvmTarget.set(JvmTarget.JVM_21)
    }
}
