package aquadx.sega.maimai2.handler

import ext.*
import aquadx.sega.general.BaseHandler
import aquadx.sega.maimai2.model.request.Mai2UserPhoto
import aquadx.sega.util.BasicMapper
import org.springframework.stereotype.Component
import java.io.IOException
import java.nio.file.Files
import java.nio.file.StandardOpenOption.APPEND
import java.nio.file.StandardOpenOption.CREATE
import java.time.LocalDateTime
import java.util.*

@Component("Maimai2UploadUserPhotoHandler")
class UploadUserPhotoHandler(private val mapper: BasicMapper) :
    BaseHandler {

    override fun handle(request: Map<String, Any>): String {
        // Maimai DX sends split base64 data for one jpeg image.
        // So, make a temp file and keep append bytes until last part received.
        // If finished, rename it to other name so user can keep save multiple scorecards in a single day.
        val up = parsing { mapper.convert(request["userPhoto"]!!, Mai2UserPhoto::class.java) }

        try {
            val tmpFile = tmpDir / "${up.userId}-${up.trackNo}.tmp"

            Files.write(tmpFile, Base64.getDecoder().decode(up.divData), CREATE, APPEND)

            if (up.divNumber == (up.divLength - 1))
                Files.move(tmpFile, uploadDir / "${up.userId}-${LocalDateTime.now().isoDateTime()}.jpg")
        } catch (e: IOException) {
            logger.error("Result: User photo save failed", e)
        }

        logger.info("Result: User photo saved")

        return """{"returnCode":1,"apiName":"com.sega.maimai2servlet.api.UploadUserPhotoApi"}"""
    }

    companion object {
        private val logger = logger()

        val tmpDir = "data/tmp".path().apply { toFile().mkdirs() }
        val uploadDir = "data/upload/mai2/plays".path().apply { toFile().mkdirs() }
    }
}
