package net.taehui.twilight.system

import net.taehui.twilight.Logger
import java.nio.file.Path
import kotlin.io.path.pathString

object AbilityClassSystem : Logger {
    enum class AbilityClassVariety {
        ABILITY_CLASS_5K, ABILITY_CLASS_7K, ABILITY_CLASS_9K
    }

    private var abilityClasses = arrayOf(emptyArray<Array<ByteArray>>(), emptyArray<Array<ByteArray>>(), emptyArray<Array<ByteArray>>())

    fun getAbilityClass(abilityClassVariety: AbilityClassVariety, abilityClass: Double): ByteArray {
        val abilityClasses = abilityClasses[abilityClassVariety.ordinal]

        return when (abilityClass) {
            -1.0, -2.0, -3.0 -> abilityClasses[0][0]
            -4.0, -5.0, -6.0, -7.0, -8.0, -9.0, -10.0 -> abilityClasses[1][0]
            else -> {
                return if (0 <= abilityClass && abilityClass < 2) {
                    abilityClasses[2][(5 * abilityClass / 2).toInt()]
                } else if (2 <= abilityClass && abilityClass < 13) {
                    abilityClasses[3][(5 * (abilityClass - 2) / 11).toInt()]
                } else if (13 <= abilityClass && abilityClass < 33) {
                    abilityClasses[4][(5 * (abilityClass - 13) / 20).toInt()]
                } else if (33 <= abilityClass && abilityClass < 62) {
                    abilityClasses[5][(5 * (abilityClass - 33) / 29).toInt()]
                } else if (62 <= abilityClass && abilityClass < 100) {
                    abilityClasses[6][(5 * (abilityClass - 62) / 38).toInt()]
                } else {
                    abilityClasses[7][0]
                }
            }
        }
    }

    fun loadAbilityClasses() {
        fun loadAbilityClass(abilityClassVariety: AbilityClassVariety) {
            fun loadDrawing(path: String): ByteArray {
                return AbilityClassSystem::class.java.classLoader.getResourceAsStream(
                    Path.of(
                        when (abilityClassVariety) {
                            AbilityClassVariety.ABILITY_CLASS_5K -> "abilityClass5K"
                            AbilityClassVariety.ABILITY_CLASS_7K -> "abilityClass7K"
                            AbilityClassVariety.ABILITY_CLASS_9K -> "abilityClass9K"
                        },
                        path
                    ).pathString
                )
                    .use { it!!.readAllBytes() }
            }

            abilityClasses[abilityClassVariety.ordinal] = arrayOf(
                arrayOf(
                    loadDrawing("S+.png")
                ), arrayOf(
                    loadDrawing("S.png")
                ), arrayOf(
                    loadDrawing("A+1.png"),
                    loadDrawing("A+2.png"),
                    loadDrawing("A+3.png"),
                    loadDrawing("A+4.png"),
                    loadDrawing("A+5.png")
                ), arrayOf(
                    loadDrawing("A1.png"),
                    loadDrawing("A2.png"),
                    loadDrawing("A3.png"),
                    loadDrawing("A4.png"),
                    loadDrawing("A5.png")
                ), arrayOf(
                    loadDrawing("B1.png"),
                    loadDrawing("B2.png"),
                    loadDrawing("B3.png"),
                    loadDrawing("B4.png"),
                    loadDrawing("B5.png")
                ), arrayOf(
                    loadDrawing("C1.png"),
                    loadDrawing("C2.png"),
                    loadDrawing("C3.png"),
                    loadDrawing("C4.png"),
                    loadDrawing("C5.png")
                ), arrayOf(
                    loadDrawing("D1.png"),
                    loadDrawing("D2.png"),
                    loadDrawing("D3.png"),
                    loadDrawing("D4.png"),
                    loadDrawing("D5.png")
                ), arrayOf(
                    loadDrawing("F.png")
                )
            )
        }

        loadAbilityClass(AbilityClassVariety.ABILITY_CLASS_5K)
        loadAbilityClass(AbilityClassVariety.ABILITY_CLASS_7K)
        loadAbilityClass(AbilityClassVariety.ABILITY_CLASS_9K)

        logInfo("Loaded Ability Class")
    }
}