package alraune.fuckaround

import alraune.*
import org.apache.tools.ant.Project
import org.apache.tools.ant.taskdefs.Jar
import org.apache.tools.ant.types.FileSet
import pieces100.*
import vgrechka.*
import java.io.File
import java.util.*

object FuckAround_UploadClassesAndExecuteRemotely1 {
    class Secrets(
        val url: String,
        val rootToken: String)

    @JvmStatic fun main(args: Array<String>) {
        val f = this::main2
        clog("Doing ${f.name}\n")
        f()
        clog("\nOK")
    }

    private fun main1() {
        val responseText = HTTPPile.postJSON_bitchUnlessOK(
            "${secrets.url}${ServeNashorn.pathInfo}",
            jsonize(ServeNashorn.Request().fill(
                rootToken = secrets.rootToken,
                code = """
                    clog("So, we are going to conduct a motherfucking computation, OK?")
                    var a = 2, b = 3
                    clog(a + b)
                    clog("Yeah, that was something")
                """)))
        val response = dejsonizeBang(ServeNashorn.Response::class, responseText)
        clog(response.output)
    }

    private fun main2() {
        val distrDir = File("c:/tmp/${this::class.simpleName}")
        val classesRoot = "E:/fegh/alraune/alraune/out/production/classes"

        val classToUpload = ShitToUpload::class.java
        val packageName = classToUpload.`package`.name
        val packageDir = packageName.replace(".", "/")
        val className = classToUpload.name.substring(packageName.length + 1)
        // clog(packageDir)
        // clog(className)

        Alk.createDirOrCleanIfExists(distrDir)

        val jarFile = File(distrDir.path + "/pizda.jar")
        Jar().also {
            it.project = Project()
            it.destFile = jarFile
            it.addFileset(FileSet().also {
                it.dir = File(classesRoot)
                val s = "$packageDir/$className*"
                it.setIncludes(s)
            })
            it.level = 9
            it.execute()
        }
        // clog("Packed shit to ${jarFile.path}")

        val response = dejsonizeBang(ServeUploadClassesAndInvoke.Response::class, HTTPPile.postJSON_bitchUnlessOK(
            secrets.url + ServeUploadClassesAndInvoke.pathInfo,
            jsonize(ServeUploadClassesAndInvoke.Request().fill(
                rootToken = secrets.rootToken,
                jarBase64 = Base64.getEncoder().encodeToString(jarFile.readBytes()),
                classToInstantiateAndInvoke = ShitToUpload::class.java.name))))
        clog(response.output)
    }
}

class ShitToUpload : ServeUploadClassesAndInvoke.Fart() {
    val kind =
        "young"
        // "moderate"

    override fun fart() {
        out.ln("I am the $kind fart")
        SubShit1("hello").say()
        SubShit2("Alraune config name is `${alConfig.configName}`").alsoSay()
    }

    inner class SubShit1(val text: String) {
        fun say() {
            out.ln("${simpleClassName(this)} of ${kind} fart says: $text")
        }
    }

    inner class SubShit2(val text: String) {
        fun alsoSay() {
            out.ln("${simpleClassName(this)} of ${kind} fart also says: $text")
        }
    }
}

