package net.taehui.twilight.note

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

class TrapNote(logicalY: Double, wait: Double, input: Int, private val isPowered: Boolean = true) :
    InputNote(logicalY, wait, input) {
    override val logicalLayer: Int
        get() = 4

    override fun hasStand(): Boolean {
        return false
    }

    override fun autoJudge(wait: Double): JudgedNoteData? {
        if (this.wait <= wait) {
            return JudgedNoteData(ID.NOT, 0.0, judged)
        }
        return null
    }

    override fun judge(input: Int, wait: Double, judgmentStage: Double, isAutoLongNote: Boolean): JudgedNoteData? {
        if (isPowered && input > 0) {
            val judgmentMeter = wait - this.wait
            val judged = Component.getJudged(judgmentMeter, judgmentStage, Component.JudgmentAssist.DEFAULT)
            if (Component.Judged.HIGHEST <= judged && judged <= Component.Judged.LOWER) {
                return JudgedNoteData(ID.TRAP_NOTE_JUDGMENT, judgmentMeter, Component.Judged.LOWEST)
            }
        }
        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 = 65535
                        this.position0 = (input - 1) * NOTE_LENGTH
                        this.position1 = position1
                        this.length = NOTE_LENGTH
                        this.height = NOTE_HEIGHT
                    }.build()
                )
            }
        }
    }
}