/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */


import grails.doc.dropdown.SoftwareVersion
import grails.doc.git.FetchTagsTask
import grails.doc.dropdown.CreateReleaseDropDownTask
import org.asciidoctor.gradle.jvm.AsciidoctorTask

apply plugin: 'org.asciidoctor.jvm.convert'

def createReleaseDropdownTask = tasks.register('createReleaseDropdown', CreateReleaseDropDownTask)
createReleaseDropdownTask.configure { CreateReleaseDropDownTask it ->
    it.filesToAddDropdowns = project.layout.buildDirectory.files('docs-modified/index.html')
    it.sourceDocsDirectory = project.layout.buildDirectory.dir('docs')
    it.modifiedPagesDirectory = project.layout.buildDirectory.dir('docs-modified')

    it.docBaseUrl = "https://apache.github.io/grails-spring-security"
    it.versionHtml = '<span id="revnumber">version ' + project.version + '</span>'
    it.additionalPath = "${project.name - '-docs'}-plugin/guide/"
    it.minimumVersion = SoftwareVersion.build('7.0.0-SNAPSHOT') // The combination of plugins & docs occurred for 7.0.x
}

def fetchTagsTask = project.tasks.register('fetchTags', FetchTagsTask)
createReleaseDropdownTask.configure {
    dependsOn(fetchTagsTask)
}

apply from: rootProject.layout.projectDirectory.file('gradle/groovydoc-config.gradle')
tasks.withType(Groovydoc).configureEach {
    def firstDocTitle
    def firstWindowTitle
    def sources = []
    rootProject.subprojects
            .findAll { it.name in (project.findProperty('apiDocsProjects') ?: []) }
            .each {
                firstDocTitle = firstDocTitle ?: (project.findProperty('apiDocsProjects') as Map)[it.name]['docTitle']
                firstWindowTitle = firstWindowTitle ?: (project.findProperty('apiDocsProjects') as Map)[it.name]['windowTitle']
                sources += it.files('src/main/groovy')
            }
    it.docTitle = firstDocTitle
    it.windowTitle = firstWindowTitle
    it.source = sources
    it.destinationDir = rootProject.layout.buildDirectory.dir("docs/${project.name - '-docs'}-plugin/groovydoc").get().asFile
}

tasks.named('asciidoctor', AsciidoctorTask) {
    sourceDir(layout.projectDirectory.file('src/docs'))
    sources { include('index.adoc') }
    outputDir = layout.buildDirectory.file('docs')
    attributes(findProperty('asciidoctorAttributes') as Map ?: [:])
    baseDirFollowsSourceDir()
    jvm {
        jvmArgs('--add-opens', 'java.base/sun.nio.ch=ALL-UNNAMED', '--add-opens', 'java.base/java.io=ALL-UNNAMED')
    }
    inputs.dir(sourceDir)
    outputs.dir(outputDir)
}

def guideDocsTask = project.tasks.register('guideDocs', Copy)
guideDocsTask.configure { Copy it ->
    it.group = 'documentation'
    it.dependsOn('asciidoctor')
    def sourceDir = layout.projectDirectory.dir('src/docs')
    def targetDir = layout.buildDirectory.dir('docs')
    it.outputs.dir(targetDir)
    it.from(sourceDir)
    it.into(targetDir)
    it.include('**/*.png', '**/*.jpg', '**/*.gif', '**/*.svg')
    it.finalizedBy(createReleaseDropdownTask)
}

tasks.register('copyGhPages', Copy) {
    group = 'documentation'
    description = 'Copy the ghpages.html file to the plugin docs directory'

    from(findProject(':core-docs').layout.projectDirectory.dir('src/docs')) {
        include 'index.tmpl'
        rename('index.tmpl', 'ghpages.html')
    }

    into(rootProject.layout.buildDirectory.dir('docs'))
}

tasks.register('copyGuide', Copy) {
    def sourceDir = layout.buildDirectory.dir('docs-modified')
    def targetDir = rootProject.layout.buildDirectory.dir("docs/${project.name - '-docs'}-plugin/guide")
    from(sourceDir)
    into(targetDir)
    if ('core-docs' == project.name) {
        finalizedBy('copyGhPages')
    }
    dependsOn('guideDocs')
    dependsOn(createReleaseDropdownTask)
    outputs.dir(targetDir)
}

tasks.register('copyApiDocs', Copy) {
    group = 'documentation'
    dependsOn('groovydoc')
    for (String projectName : (findProperty('apiDocsProjects') as Map).keySet()) {
        from(layout.buildDirectory.dir("docs/$projectName/groovydoc")) {
            into(rootProject.layout.buildDirectory.dir("docs/$projectName/groovydoc"))
        }
    }
}

tasks.register('aggregateDocs') {
    dependsOn('copyGuide', 'copyApiDocs')
    group = 'documentation'
    description = 'Aggregate all documentation from all subprojects into the root docs directory'
}