package com.mxn.helper.screenshot.server.controller

import android.content.Context
import com.koushikdutta.async.http.server.AsyncHttpServer
import com.koushikdutta.async.http.server.AsyncHttpServerRequest
import com.koushikdutta.async.http.server.AsyncHttpServerResponse
import com.koushikdutta.async.http.server.HttpServerRequestCallback
import org.json.JSONObject

class RootController(private val context: Context) {

    private val imageController by lazy { ImageController(context) }

    fun bindServer(server: AsyncHttpServer) {
        server.post(
            "/image/list"
        ) { request, response ->
            response.send("application/json", imageController.getImageList())
        }

        server.post(
            "/image/create"
        ) { request, response ->
            response.send("application/json", imageController.createImage())
        }

        server.post(
            "/image/delete"
        ) { request, response ->
            val path = (request.body.get() as JSONObject).get("path").toString()
            response.send("application/json", imageController.deleteImage(path))
        }

        server.post(
            "/image/zip"
        ) { request, response ->
            response.send("application/json", imageController.zipImages())
        }

        server.post(
            "/image/clearall"
        ) { request, response ->
            response.send("application/json", imageController.clearAllFile())
        }

        server.post(
            "/image/rename"
        ) { request, response ->
            val path = (request.body.get() as JSONObject).get("path").toString()
            val newName = (request.body.get() as JSONObject).get("name").toString()
            response.send("application/json", imageController.rename(path,newName))
        }
    }

}