package main

import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Test
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.PrintStream

class ParserTest {

    @Test
    fun testDelStrings() {
        val input = "This is a \"sample\"string."
        val output = delStrings(input)
        assertEquals("This is a string.", output)
    }

    @Test
    fun testTakeOnlyJs() {
        val fileToTest = "src/test/resources/FileWithScript.html"
        val expectedJsCode = File("src/test/resources/OnlyTheScript.txt").readText().trimIndent().replace("\\s+".toRegex(), " ")
        val output = takeOnlyJs(fileToTest).trimIndent().replace("\\s+".toRegex(), " ")
        assertEquals(expectedJsCode, output)
    }

    @Test
    fun testJsWords() {
        // Assume you have a sample code map
        val codeMap = mapOf("word1" to 2, "word2" to 3, "javascript" to 5)
        val output = jsWords(codeMap)
        // Assert the expected output based on the filtering logic
        assertEquals(mapOf("word1" to 2, "word2" to 3, "javascript" to 5), output)
    }

    @Test
    fun testSplitLanguages() {
        // Assume you have a sample HTML file with <script> and <style> tags
        val fileWithHTMLCSSJS = "src/test/resources/FileWithStyleAndScript.html"
        val expectedJsCode = "function greet() {\n    return \"Hello from JavaScript!\";\n}\n\nconsole.log(greet());".trimIndent().replace("\\s+".toRegex(), " ")
        val expectedCssCode = "body {\n    background-color: #f0f0f0;\n}".trimIndent().replace("\\s+".toRegex(), " ")

        // Call the parser function with the sample HTML file
        splitLanguages(fileWithHTMLCSSJS)

        // Verify the content of the generated files
        val actualJsCode = File("javascript.js").readText().trimIndent().replace("\\s+".toRegex(), " ")
        val actualHtmlCode = File("html.html").readText()
        val actualCssCode = File("css.css").readText().trimIndent().replace("\\s+".toRegex(), " ")

        // Assertions
        assertEquals(expectedJsCode, actualJsCode)
        assertEquals(expectedCssCode, actualCssCode)

        // Ensure that HTML content is modified as expected
        assertTrue(actualHtmlCode.contains("<script src=\"javascript.js\"></script>"))
        assertTrue(actualHtmlCode.contains("<link href=\"css.css\" rel=\"stylesheet\" />"))
    }

    @Test
    fun testParser() {
        // Assume you have a sample HTML file for testing
        val puissance4File = "src/test/resources/puissance4.html"

        // Call the parser function
        parserJS(puissance4File)

        // Verify the output by capturing the printed content
        val printedContent = captureStandardOutput {
            parserJS(puissance4File)
        }

        // Assert the expected output based on the content of your sample HTML file
        val expectedOutput =File("src/test/resources/ExpectedParsingResult.txt").readText().trimIndent()

        assertEquals(expectedOutput, printedContent.trimIndent())
    }

    // Function to capture standard output
    private fun captureStandardOutput(block: () -> Unit): String {
        val standardOut = System.out
        val outputStream = ByteArrayOutputStream()
        System.setOut(PrintStream(outputStream))

        try {
            block()
        } finally {
            System.setOut(standardOut)
        }

        return outputStream.toString().trim()
    }
}
