package main

import java.io.File


fun delStrings(line : String) : String {
    val regex = "\".*?\"".toRegex()
    return regex.replace(line, "")
}

fun takeOnlyJs(fileName: String): String {
    val extension = fileName.split(".")[1]
    if ("html" == extension){
        return File(fileName).readText().split("<script>")[1].split("</script>")[0]
    }
    return File(fileName).readText()
}

fun jsWords(code : Map<String,Int>) : Map<String,Int>{
    val js = File("src/main/kotlin/main/sampleJS.txt").readText().split(",")

    return code.filter { it.key !in js }
}

fun splitLanguages(file : String){
    val extension = file.split(".")[1]
    if ("html" == extension) {
        val js = File(file).readText().split("<script>")[1].split("</script>")[0]
        val css = File(file).readText().split("<style>")[1].split("</style>")[0]
        val newHtml = File(file).readText().replace("<script>$js</script>", "<script src=\"javascript.js\"></script>")
            .replace("<style>$css</style>", "<link href=\"css.css\" rel=\"stylesheet\" />")


        if (js != "") {
            File("javascript.js").writeText(js)
        }

        if (newHtml != File(file).readText()) {
            File("html.html").writeText(newHtml)
        }
        if (css != "") {
            File("css.css").writeText(css)
        }
    }



}



fun parserJS(fileName : String) : MutableMap<String, Int> {
    val delimiter1 = " "
    val regex = "[^a-zA-Z^éà]".toRegex()
    val array = mutableListOf<String>()

    val line = delStrings(takeOnlyJs(fileName)).replace(regex, " ").lowercase()
    line.split(delimiter1).forEach {

        if (it != "") {
            array.add(it)
        }
    }
    val map = jsWords(array.groupingBy { it }.eachCount())

    var sortedMap = map.toList().take(10).sortedBy { (_, value) -> value }.toMap()
    sortedMap.forEach { (t, u) -> println("$t : $u") } // affiche le nombre d'occurence de chaque mot
    sortedMap = sortedMap.toMutableMap()
    return sortedMap

}

fun ktWords(code : Map<String,Int>) : Map<String,Int>{
    val js = File("src/main/kotlin/main/sampleKt.txt").readText().split(",")

    return code.filter { it.key !in js }
}
fun parserKt(fileName : String) : MutableMap<String, Int> {
    val delimiter1 = " "
    val regex = "[^a-zA-Z^éà]".toRegex()
    val array = mutableListOf<String>()

    val line = delStrings(File(fileName).readText()).replace(regex, " ")
    line.split(delimiter1).forEach {

        if (it != "") {
            array.add(it)
        }
    }
    val map = ktWords(array.groupingBy { it }.eachCount())

    return map.toMutableMap()
}


fun directoryParse(directory : String) : MutableMap<String, Int> {
    val files = File(directory).walk().filter { it.isFile }.toList()
    val map = mutableMapOf<String, Int>()
    files.forEach {


        if (it.extension == "kt" && it.toString().contains("bin") == false) {
            println(it.absolutePath)
            val map2 = parserKt(it.toString())
            map2.forEach { (t, u) -> map[t] = map.getOrDefault(t, 0) + u }
        }

    }
    return map
}
