package net.taehui.twilight.note

import net.taehui.twilight.Component
import net.taehui.twilight.awilight.DefaultCompute
import net.taehui.twilight.note.JudgedNoteData.ID

open class InputNote(logicalY: Double, wait: Double, input: Int) : BaseNote(logicalY, wait) {
    override val logicalLayer: Int
        get() = 1
    override val layer: Int
        get() = if (longWait > 0.0) 3 else 4

    override fun hasStand(): Boolean {
        return true
    }

    init {
        this.input = input
    }

    override fun isFailedAsTooLate(wait: Double, judgmentStage: Double): Boolean {
        return wait - this.wait >= Component.getJudgmentMillis(
            Component.Judged.LOWEST,
            judgmentStage,
            1,
            Component.JudgmentAssist.DEFAULT
        )
    }

    override fun autoJudge(wait: Double): JudgedNoteData? {
        return null
    }

    override fun judge(input: Int, wait: Double, judgmentStage: Double, isAutoLongNote: Boolean): JudgedNoteData? {
        if (input > 0) {
            val judgmentMeter = wait - this.wait
            val judged = Component.getJudged(judgmentMeter, judgmentStage, Component.JudgmentAssist.DEFAULT)
            if (judged != Component.Judged.NOT) {
                return JudgedNoteData(ID.NOTE_JUDGMENT, judgmentMeter, judged)
            }
        }
        return null
    }

    override fun paint(defaultComputer: DefaultCompute) {
        val multiplier = defaultComputer.modeComponentValue.getPaidMultiplier()
        if (multiplier > 0.0) {
            val position1 = getY(multiplier) - NOTE_HEIGHT + NOTE_HEIGHT_JUDGMENT
            if (position1 + NOTE_HEIGHT > 0.0) {
                defaultComputer.netDrawings.add(
                    EventOuterClass.Event.NetDrawing.newBuilder().apply {
                        this.drawingVariety = EventOuterClass.Event.NetDrawing.Variety.Note
                        this.param = Component.NET_DRAWINGS[defaultComputer.inputMode.value]!![input]
                        this.position0 = (input - 1) * NOTE_LENGTH
                        this.position1 = position1
                        this.length = NOTE_LENGTH
                        this.height = NOTE_HEIGHT
                    }.build()
                )
            }
        }
    }

    companion object {
        const val NOTE_LENGTH = 51.0
        const val NOTE_HEIGHT = 11.0
        const val NOTE_HEIGHT_JUDGMENT = NOTE_HEIGHT / 2
    }
}