package solutions.tretter.githugandroid

import android.content.Context
import java.io.File

internal fun bundledGitManPage(context: Context?, topic: String): String? {
    val assetNames = listOf(
        "manpages/git-$topic.txt",
        "manpages/git${topic.removePrefix("-")}.txt",
        "manpages/$topic.txt",
    ).distinct()

    if (context != null) {
        assetNames.forEach { assetName ->
            try {
                context.assets.open(assetName).bufferedReader().use { reader ->
                    return reader.readText().takeIf { it.isNotBlank() }
                }
            } catch (_: Exception) {
                // Try the next bundled filename.
            }
        }
    }

    val repoAssetRoot = File("src/main/assets")
    assetNames.forEach { assetName ->
        val file = File(repoAssetRoot, assetName)
        if (file.exists()) {
            return file.readText().takeIf { it.isNotBlank() }
        }
    }

    return null
}

internal fun placeholderGitManPage(context: Context?, topic: String): String {
    bundledGitManPage(context, topic)?.let { return it }
    return when (topic) {
        "tag" -> """
            NAME
                git-tag - Create, list, delete or verify a tag object

            SYNOPSIS
                git tag <tagname>
                git tag -d <tagname>
                git tag

            DESCRIPTION
                Tags name specific points in history. In GitHug Android,
                tag levels usually expect you to create or reference a tag
                by name.

            EXAMPLES
                git tag new_tag
                git tag v1.2
        """.trimIndent()
        "branch" -> """
            NAME
                git-branch - List, create, or delete branches

            SYNOPSIS
                git branch <branchname>
                git branch -d <branchname>

            EXAMPLES
                git branch test_code
                git branch -d delete_me
        """.trimIndent()
        "checkout" -> """
            NAME
                git-checkout - Switch branches or restore files

            SYNOPSIS
                git checkout <branch>
                git checkout -b <branch>
                git checkout -- <file>

            EXAMPLES
                git checkout -b my_branch
                git checkout -- config.rb
        """.trimIndent()
        "add" -> """
            NAME
                git-add - Add file contents to the index

            SYNOPSIS
                git add <path>
                git add .
                git add -p <path>
        """.trimIndent()
        "commit" -> """
            NAME
                git-commit - Record changes to the repository

            SYNOPSIS
                git commit -m <message>
                git commit --amend
        """.trimIndent()
        "ignore" -> """
            NAME
                gitignore - Specifies intentionally untracked files to ignore

            SYNOPSIS
                $'GIT_DIR/info/exclude', .gitignore

            DESCRIPTION
                A gitignore file specifies intentionally untracked files
                that Git should ignore. Each line contains a pattern.

                Blank lines are ignored. Lines beginning with # are comments.
                An optional ! prefix negates a pattern and re-includes a path.
                A pattern ending with / matches directories.

            EXAMPLES
                *.swp
                *.a
                !lib.a
        """.trimIndent()
        else -> """
            NAME
                git-$topic

            DESCRIPTION
                No bundled manpage is available for git $topic.
                Try the command suggestions or use git help for another command.
        """.trimIndent()
    }
}
