/*
 * Ubuntu Countdown Widget
 * Copyright (C) 2022 Roberto Leinardi
 *
 * This program 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.
 *
 * This program 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 this
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */
import com.github.benmanes.gradle.versions.reporter.PlainTextReporter

plugins {
    id 'com.github.ben-manes.versions'
}

def isNonStable = { String version ->
    def stableKeyword = ['RELEASE', 'FINAL', 'GA'].any { it -> version.toUpperCase().contains(it) }
    def regex = /^[0-9,.v-]+(-r)?$/
    return !stableKeyword && !(version ==~ regex)
}
tasks.named("dependencyUpdates").configure {
    gradleReleaseChannel = 'current'
    rejectVersionIf {
        isNonStable(it.candidate.version) && !isNonStable(it.currentVersion)
    }
    outputFormatter = { result ->
        def writer = new StringWriter()
        def html = new groovy.xml.MarkupBuilder(writer)
        def updatable = result.outdated.dependencies

        html.html {
            body {
                h1 'Outdated dependencies'
                if (!updatable.isEmpty()) {
                    table {
                        thead {
                            tr {
                                td { strong 'Dependency' }
                                td { strong 'Current version' }
                                td { strong 'Latest version' }
                            }
                        }
                        tbody {
                            updatable.each { dependency ->
                                tr {
                                    def dependencyName = "${dependency.group}:${dependency.name}"
                                    if (dependency.projectUrl != null) {
                                        td { a(href: dependency.projectUrl, dependencyName) }
                                    } else {
                                        td(dependencyName)
                                    }
                                    td(style: 'color:red', dependency.version)
                                    td(style: 'color:green', dependency.available.release ?: dependency.available.milestone)
                                }
                            }
                        }
                    }
                }

                h1 'Exceed latest dependencies'
                def exceeded = result.exceeded.dependencies
                if (!exceeded.isEmpty()) {
                    table {
                        thead {
                            tr {
                                td { strong 'Dependency' }
                                td { strong 'Current version' }
                                td { strong 'Latest version' }
                            }
                        }
                        tbody {
                            exceeded.each { dependency ->
                                tr {
                                    def dependencyName = "${dependency.group}:${dependency.name}"
                                    if (dependency.projectUrl != null) {
                                        td { a(href: dependency.projectUrl, dependencyName) }
                                    } else {
                                        td(dependencyName)
                                    }
                                    td(style: 'color:red', dependency.version)
                                    td(style: 'color:green', dependency.latest)
                                }
                            }
                        }
                    }
                }

                h1 'Unresolved dependencies'
                def unresolved = result.unresolved.dependencies
                if (!unresolved.isEmpty()) {
                    table {
                        thead {
                            tr {
                                td { strong 'Dependency' }
                                td { strong 'Current version' }
                            }
                        }
                        tbody {
                            unresolved.each { dependency ->
                                tr {
                                    def dependencyName = "${dependency.group}:${dependency.name}"
                                    if (dependency.projectUrl != null) {
                                        td { a(href: dependency.projectUrl, dependencyName) }
                                    } else {
                                        td(dependencyName)
                                    }
                                    td(style: 'color:red', dependency.version)
                                }
                            }
                        }
                    }
                }

                h1('Gradle')
                result.gradle.with {

                    table {
                        thead {
                            tr {
                                td { strong 'Current version' }
                                if (current != running) {
                                    td { strong 'Latest version' }
                                }
                            }
                        }
                        tbody {
                            if (current == running) {
                                tr {
                                    td(style: 'color:green', running.version)
                                }
                            } else {
                                tr {
                                    td(style: 'color:red', running.version)
                                    td(style: 'color:green', current.version)
                                }
                            }
                        }
                    }
                }

                h1 'UpToDate dependencies'
                def current = result.current.dependencies
                if (!current.isEmpty()) {
                    table {
                        thead {
                            tr {
                                td { strong 'Dependency' }
                                td { strong 'Current version' }
                            }
                        }
                        tbody {
                            current.each { dependency ->
                                tr {
                                    def dependencyName = "${dependency.group}:${dependency.name}"
                                    if (dependency.projectUrl != null) {
                                        td { a(href: dependency.projectUrl, dependencyName) }
                                    } else {
                                        td(dependencyName)
                                    }
                                    td(style: 'color:green', dependency.version)
                                }
                            }
                        }
                    }
                }
            }
        }
        new File("${project.buildDir}/reports/dependencyUpdates").mkdirs()
        def file = new File("${project.buildDir}/reports/dependencyUpdates/dependencyUpdates.html")
        file.createNewFile()
        file.write(writer.toString())

        PlainTextReporter plainTextReporter = new PlainTextReporter(project, revision, gradleReleaseChannel)
        plainTextReporter.write(System.out, result)
    }
}
