# =========== temp

println(Base.Docs.doc(scatter))
println(Main.Docs.doc(Makie.convert_arguments, Tuple{Any, AbstractMatrix}))
println(Base.Docs.doc(Makie.convert_arguments, Tuple{Any, AbstractVector, AbstractVector, AbstractMatrix}))
println(Base.Docs.doc(Makie.convert_arguments, Tuple{Any, AbstractVector, AbstractVector, Function}))

const atomics = (
    heatmap,
    image,
    lines,
    linesegments,
    mesh,
    meshscatter,
    scatter,
    surface,
    text,
    Makie.volume
)


# =============================================
# Trying out heatmap plot from all_samples.jl
path = joinpath(@__DIR__, "..", "docs", "test")
scene = Scene(resolution = (500, 500))
scene = heatmap!(scene, rand(32, 32))
save("heatmap.png", scene)


path = joinpath(@__DIR__, "..", "docs", "test")
# save("heatmap.png", scene)
img = Makie.scene2image(scene)
save(path, img)


# =============================================
# Print code for when database search returns multiple results

# First find those damn entries!
entries = example_database(lines)
entries = findall(x -> x.title == database_key, database)
entries = findall(x -> x.tags == "scatter", database)
entries = findall(x -> occursin("scatter", collect(x.tags)), database)

tgs = database[2].tags

occursin("scatter", tgs)

findall(database) do entry
    # find tags
    tags_found = any(tag -> string(tag) in entry.tags, "scatter")
end

len_entries = length(entries)
println("len_entries = ", len_entries)

for i = 1:len_entries
    # println(x.title)
    # println(x.source)
    println("i = ", i)
    sprint() do io
        print_code(
            io, database, entries[1],
            scope_start = "",
            scope_end = "",
            indent = "",
            resolution = (entry)-> "resolution = (500, 500)",
            outputfile = (entry, ending)-> Pkg.dir("Makie", "docs", "media", string(entry.unique_name, ending))
        )
    end
end



sprint(print_code(stdout, database, entries[1],
scope_start = "",
scope_end = "",
indent = "",
resolution = (entry)-> "resolution = (500, 500)",
outputfile = (entry, ending)-> Pkg.dir("Makie", "docs", "media", string(entry.unique_name, ending))
))


# autogenerate example library including plots test
# TODO: not working right now since a lot of plots are broken
# TODO: need to figure out if there is a way to "close" a Makie plot window
pathroot = joinpath(@__DIR__, "..", "docs", "src", "library-test")
imgpath = joinpath(pathroot, "plots")
mdpath = joinpath(pathroot, "library-test.md")
open(mdpath, "w") do io
    println(io, "# Examples library test -- autogenerated")
    isempty(database) && error("database is empty!")
    counter = 1
    for idx in 1:length(database)
        println(io, "## index = $idx")
        # println(io, "## `$(Makie.to_string(func))`")
        try
            println(io, "Example $counter, \"$(database[idx].title)\"")
            _print_source(io, idx; style = "julia")
            println(io, "`plot goes here`\n")
            # TODO: add code to generate + embed plots
            counter += 1
        catch
            println("ERROR: Didn't work with $tag at index $idx\n")
        end
        println(io, "\n")
    end
end
