using LibTeXPrintf
using LaTeXStrings
using Test

import LibTeXPrintf: texerrors, libtexprintf, texstring


@testset "LibTeXPrintf.jl" begin
    @test texerrors() == ""
    @test texfonts() == ("mathbb", "mathbf", "mathbfit", "mathcal", "mathfrak", "mathnormal", "mathscr", "mathsf", "mathsfbf", "mathsfbfit", "mathsfit", "mathtt", "text")
    @test texgetfont() == "text"

    @test texsetfont!("mathbb") == "mathbb"
    @test texgetfont() == "mathbb"
    @test texstring("string") == "ğ�•¤ğ�•¥ğ�•£ğ�•šğ�•Ÿğ�•˜"

    @test texsetfont!("text") == "text"
    @test texgetfont() == "text"
    @test texstring("string") == "string"

    @test_throws ArgumentError texsetfont!("not_a_font")

    @test texstring(L"".s) == "\$\$"

    tex_basel = L"The Basel problem (find if $\sum_{n\in\mathbb{R}}\left(\frac{1}{n^2}\right)$ converge and, if so, to what value) was solved by Leonhard Euler in 1734, yielding that the serie converge to $\sqrt{\frac{1}{\pi}}$. Hint: Euler used the next Taylor series $$ \frac{\sin(x)}{x} = x^{1} - \frac{x^2}{3!} + \frac{x^4}{5!} - \frac{x^6}{7!} + ...$$"
    # basel_out = "                            â�¯â�¯\n                            â•²  â�›1 â��\nThe Basel problem (find if \$â•±  â�œâ”€â”€â�Ÿ\$ converge and, if so, to what value) was\n                            â�ºâ�º â�œ 2â�Ÿ\n                            nâˆˆâ„�â��n â� \n                                                                         â”Œâ”€â”�\n                                                                         â”‚1\nsolved by Leonhard Euler in 1734, yielding that the serie converge to \$â•² â”‚â”€ \$.\n                                                                        â•²â”‚Ï€\n                                                          2    4    6\n                                           sin(x)    1   x    x    x\nHint: Euler used the next Taylor series \$\$ â”€â”€â”€â”€â”€â”€ = x  - â”€â”€ + â”€â”€ - â”€â”€ + ...\$\$\n                                             x           3!   5!   7!"
    basel_out = "                             â�² â�›1 â��\nThe Basel problem (find if \$ â�³ â�œâ”€â”€â�Ÿ\$ converge and, if so, to what value) was\n                            nâˆˆâ„�â��nÂ²â� \n                                                                         â”Œâ”€â”�\n                                                                         â”‚1\nsolved by Leonhard Euler in 1734, yielding that the serie converge to \$â•² â”‚â”€ \$.\n                                                                        â•²â”‚Ï€\n                                           sin(x)        xÂ²   xâ�´   xâ�¶\nHint: Euler used the next Taylor series \$\$ â”€â”€â”€â”€â”€â”€ = xÂ¹ - â”€â”€ + â”€â”€ - â”€â”€ + ...\$\$\n                                             x           3!   5!   7!"
    @test texstring(tex_basel.s; lw=80) == basel_out

    # println(basel)
    #
    ####################################################################################
    #                              â�¯â�¯                                                  #
    #                              â•²  â�›1 â��                                             #
    #  The Basel problem (find if $â•±  â�œâ”€â”€â�Ÿ$ converge and, if so, to what value) was    #
    #                              â�ºâ�º â�œ 2â�Ÿ                                             #
    #                              nâˆˆâ„�â��n â�                                              #
    #                                                                           â”Œâ”€â”�    #
    #                                                                           â”‚1     #
    #  solved by Leonhard Euler in 1734, yielding that the serie converge to $â•² â”‚â”€ $.  #
    #                                                                          â•²â”‚Ï€     #
    #                                                           2    4    6            #
    #                                             sin(x)       x    x    x             #
    #  Hint: Euler used the next Taylor series $$ â”€â”€â”€â”€â”€â”€ = x - â”€â”€ + â”€â”€ - â”€â”€ + ...$$    #
    #                                               x          3!   5!   7!            #
    #                                                                                  #
    ####################################################################################
    #                                                                                  #
    #                              â�² â�›1 â��                                              #
    # The Basel problem (find if $ â�³ â�œâ”€â”€â�Ÿ$ converge and, if so, to what value) was     #
    #                             nâˆˆâ„�â��nÂ²â�                                               #
    #                                                                          â”Œâ”€â”�     #
    #                                                                          â”‚1      #
    # solved by Leonhard Euler in 1734, yielding that the serie converge to $â•² â”‚â”€ $.   #
    #                                                                         â•²â”‚Ï€      #
    #                                            sin(x)        xÂ²   xâ�´   xâ�¶            #
    # Hint: Euler used the next Taylor series $$ â”€â”€â”€â”€â”€â”€ = xÂ¹ - â”€â”€ + â”€â”€ - â”€â”€ + ...$$    #
    #                                              x           3!   5!   7!            #
    #                                                                                  #
    ####################################################################################

    @test texstring("\\frac{1}{%%d}") == " 1\nâ”€â”€â”€\n%d" # expected " 1\nâ”€â”€â”€\n%%d"

    @test_throws ArgumentError texstring("\$\\frac{1}\$")
    @test_logs (:error, ArgumentError("Too few mandatory arguments to command (1x)")) texstring("\$\\frac{1}\$", fail=false) == "\$"
end
