// From allwpilib/docs. Licensed under the WPILib BSD License

plugins {
    id 'java'
    id "org.ysb33r.doxygen" version "1.0.4"
}


evaluationDependsOn ':photon-targeting'
evaluationDependsOn ':photon-core'
evaluationDependsOn ':photon-server'
evaluationDependsOn ':photon-lib'


def baseArtifactIdCpp = 'documentation'
def artifactGroupIdCpp = 'org.photonvision.cppdocs'
def zipBaseNameCpp = '_GROUP_org.photonvision_cpp_ID_documentation_CLS'

def baseArtifactIdJava = 'documentation'
def artifactGroupIdJava = 'org.photonvision.javadocs'
def zipBaseNameJava = '_GROUP_org.photonvision_java_ID_documentation_CLS'

def outputsFolder = file("$project.buildDir/outputs")

def cppProjectZips = []
def cppIncludeRoots = []

cppProjectZips.add(project(':photon-lib').cppHeadersZip)
cppProjectZips.add(project(':photon-targeting').cppHeadersZip)

doxygen {
    // Doxygen binaries are only provided for x86_64 platforms
    // Other platforms will need to provide doxygen via their system
    // See below maven and https://doxygen.nl/download.html for provided binaries

    String arch = System.getProperty("os.arch");
    if (arch.equals("x86_64") || arch.equals("amd64")) {
        executables {
            doxygen {
                executableByVersion('1.12.0')
            }
        }
    }
}

doxygen {
    option 'generate_html', true
    option 'html_extra_stylesheet', 'theme.css'

    cppProjectZips.each {
        dependsOn it
        source it.source
        it.ext.includeDirs.each {
            cppIncludeRoots.add(it.absolutePath)
        }
    }
    option 'case_sense_names', false
    option 'extension_mapping', 'inc=C++ no_extension=C++'
    option 'extract_all', true
    option 'extract_static', true
    option 'file_patterns', '*'
    option 'full_path_names', true
    option 'generate_html', true
    option 'generate_latex', false
    option 'generate_treeview', true
    option 'html_extra_stylesheet', 'theme.css'
    option 'html_timestamp', true
    option 'javadoc_autobrief', true
    option 'project_name', 'PhotonVision C++'
    option 'project_logo', '../docs/source/assets/RoundLogo.png'
    option 'project_number', pubVersion
    option 'quiet', true
    option 'recursive', true
    option 'strip_code_comments', false
    option 'strip_from_inc_path', cppIncludeRoots
    option 'strip_from_path', cppIncludeRoots
    option 'use_mathjax', true
    option 'warnings', false
    option 'warn_if_incomplete_doc', true
    option 'warn_if_undocumented', false
    option 'warn_no_paramdoc', true

    //enable doxygen preprocessor expansion of WPI_DEPRECATED to fix MotorController docs
    option 'enable_preprocessing', true
    option 'macro_expansion', true
    option 'expand_only_predef', true
    option 'predefined', "WPI_DEPRECATED(x)=[[deprecated(x)]]\"\\\n" +
            "\"__cplusplus\"\\\n" +
            "\"HAL_ENUM(name)=enum name : int32_t"

    if (project.hasProperty('docWarningsAsErrors')) {
        warn_as_error 'FAIL_ON_WARNINGS'
    }
}

tasks.register("zipCppDocs", Zip) {
    archiveBaseName = zipBaseNameCpp
    destinationDirectory = outputsFolder
    dependsOn doxygen
    from ("$buildDir/docs/doxygen/html")
    into '/'
}

// Java
configurations {
    javaSource {
        transitive false
    }
}

ext {
    sharedCvConfigs = [:]
    staticCvConfigs = [:]
    useJava = true
    useCpp = false
    skipDev = true
    useDocumentation = true
}

task generateJavaDocs(type: Javadoc) {
    def exportedProjects = [
        ':photon-core',
        ':photon-server',
        ':photon-targeting',
        ':photon-lib'
    ]

    source exportedProjects.collect { project(it).sourceSets.main.allJava }
    classpath = files(exportedProjects.collect { project(it).sourceSets.main.compileClasspath })
    dependsOn project(':photon-core').writeCurrentVersion

    options.links "https://docs.oracle.com/en/java/javase/17/docs/api/", "https://github.wpilib.org/allwpilib/docs/release/java/"
    options.addStringOption("tag", "pre:a:Pre-Condition")
    options.addBooleanOption("Xdoclint:html,missing,reference,syntax", true)
    options.addBooleanOption('html5', true)
    failOnError = true

    title = "PhotonVision $pubVersion"
    ext.entryPoint = "$destinationDir/index.html"

    if (JavaVersion.current().isJava8Compatible() && project.hasProperty('docWarningsAsErrors')) {
        // Treat javadoc warnings as errors.
        //
        // The second argument '-quiet' is a hack. The one parameter
        // addStringOption() doesn't work, so we add '-quiet', which is added
        // anyway by gradle. See https://github.com/gradle/gradle/issues/2354.
        //
        // See JDK-8200363 (https://bugs.openjdk.java.net/browse/JDK-8200363)
        // for information about the nonstandard -Xwerror option. JDK 15+ has
        // -Werror.
        options.addStringOption('Xwerror', '-quiet')
    }

    if (JavaVersion.current().isJava11Compatible()) {
        if (!JavaVersion.current().isJava12Compatible()) {
            options.addBooleanOption('-no-module-directories', true)
        }
        doLast {
            // This is a work-around for https://bugs.openjdk.java.net/browse/JDK-8211194. Can be removed once that issue is fixed on JDK's side
            // Since JDK 11, package-list is missing from javadoc output files and superseded by element-list file, but a lot of external tools still need it
            // Here we generate this file manually
            new File(destinationDir, 'package-list').text = new File(destinationDir, 'element-list').text
        }
    }
}

tasks.register("zipJavaDocs", Zip) {
    archiveBaseName = zipBaseNameJava
    destinationDirectory = outputsFolder
    dependsOn generateJavaDocs
    from ("$buildDir/docs/javadoc")
    into '/'
}

tasks.register("zipDocs") {
    dependsOn zipCppDocs
    dependsOn zipJavaDocs
}

apply plugin: 'maven-publish'

publishing {
    publications {
        java(MavenPublication) {
            artifact zipJavaDocs

            artifactId = "${baseArtifactIdJava}"
            groupId artifactGroupIdJava
            version pubVersion
        }
        cpp(MavenPublication) {
            artifact zipCppDocs

            artifactId = "${baseArtifactIdCpp}"
            groupId artifactGroupIdCpp
            version pubVersion
        }
    }

    repositories {
        maven {
            // Just throw everything into build/maven
            url(localMavenURL)
        }
    }
}
