// Generated by the ftcsim wizard. Robot sources are shared, never copied.
plugins {
    id 'application'
__KOTLIN_PLUGIN__}
java { sourceCompatibility = JavaVersion.VERSION_1_8; targetCompatibility = JavaVersion.VERSION_1_8 }
sourceSets.main.java.srcDirs = ['../TeamCode/src/main/java']
__KOTLIN_SOURCES__
def ftcsimLocal = new Properties()
rootProject.file('local.properties').withInputStream { ftcsimLocal.load(it) }
def ftcsimPath = providers.gradleProperty('ftcsimSdkDir').orNull ?: System.getenv('FTCSIM_SDK_DIR') ?: ftcsimLocal.getProperty('ftcsim.sdkDir')
if (!ftcsimPath) throw new GradleException('Run ftcsim --wizard first')
def ftcsimLibs = new File(ftcsimPath, 'libs')
dependencies { implementation fileTree(dir: ftcsimLibs, include: ['*.jar']) }

// Copy dependency declarations, excluding Android project outputs.
evaluationDependsOn(':TeamCode')
def robot = project(':TeamCode')
['api', 'implementation', 'compileOnly', 'runtimeOnly'].each { name ->
    def source = robot.configurations.findByName(name)
    source?.allDependencies?.each { dependency ->
        if (!(dependency instanceof org.gradle.api.artifacts.ProjectDependency)) {
            dependencies.add(name == 'api' ? 'implementation' : name, dependency.copy())
        }
    }
}
robot.repositories.withType(org.gradle.api.artifacts.repositories.MavenArtifactRepository).eachWithIndex { repo, index ->
    repositories.maven {
        name = 'ftcsimRobotRepository' + index
        url = repo.url
        metadataSources { mavenPom(); artifact(); ignoreGradleMetadataRedirection() }
    }
}
apply from: 'aar-to-jar.gradle'
apply from: 'extra-dependencies.gradle'

application { mainClass = 'ftcsim.jvm.DesktopSdk' }
def orderedClasspath = {
    def all = sourceSets.main.runtimeClasspath
    all.filter { it.name.startsWith('ftc-sdk-jvm-') } + all.filter { !it.name.startsWith('ftc-sdk-jvm-') }
}
tasks.named('run', JavaExec) {
    group = 'simulation'
    systemProperty 'ftcsim.jvm', 'true'
    workingDir rootDir
    classpath = orderedClasspath()
    doFirst {
        def modes = providers.gradleProperty('simOpmodes').orElse(__OPMODES__).get()
        def values = ['--sim', providers.gradleProperty('simEndpoint').orElse('ws://localhost:9015/sim').get(),
                      '--ds-port', providers.gradleProperty('simDsPort').orElse('1115').get()]
        modes.split(',').collect { it.trim() }.findAll { it }.each { values += ['--opmode', it] }
        setArgs(values)
    }
}
tasks.register('startBackend', Exec) {
    group = 'simulation'
    commandLine providers.gradleProperty('simPython').orElse(ftcsimLocal.getProperty('ftcsim.python', 'python3')).get(), '-m', 'ftcsim'
}
tasks.register('writeClasspath') {
    group = 'simulation'
    dependsOn 'classes'
    inputs.files sourceSets.main.runtimeClasspath
    outputs.file layout.buildDirectory.file('classpath.txt')
    doLast { file('build/classpath.txt').text = orderedClasspath().asPath }
}
tasks.register('verifySimulatorIsolation') {
    group = 'verification'
    doLast {
        def leaked = configurations.runtimeClasspath.resolvedConfiguration.resolvedArtifacts.findAll {
            it.moduleVersion.id.group == 'org.firstinspires.ftc'
        }
        if (!leaked.empty) throw new GradleException('Android FTC SDK artifacts leaked into simulator: ' + leaked)
        def sdk = sourceSets.main.runtimeClasspath.files.findAll { it.name.startsWith('ftc-sdk-jvm-') }
        if (sdk.size() != 1) throw new GradleException('Expected one desktop SDK JAR: ' + sdk)
        logger.lifecycle('PASS: simulator classpath uses the custom desktop FTC SDK')
    }
}
