/*
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
 *
 * Copyright (c) 2015 - 2026 CCBlueX
 *
 * LiquidBounce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * LiquidBounce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
 */

package net.ccbluex.liquidbounce.integration.theme.component

import net.ccbluex.liquidbounce.config.types.group.ModeValueGroup
import net.ccbluex.liquidbounce.config.types.group.ToggleableValueGroup
import net.ccbluex.liquidbounce.config.types.group.ValueGroup
import net.ccbluex.liquidbounce.features.module.modules.render.ModuleHud
import net.ccbluex.liquidbounce.utils.render.Alignment
import java.util.UUID

/**
 * Represents a HUD component
 */
abstract class HudComponent(
    name: String,
    enabled: Boolean,
    alignment: Alignment = Alignment.center(),
    val tweaks: Array<HudComponentTweak> = emptyArray(),
    val componentDescription: String = "",
) : ToggleableValueGroup(parent = ModuleHud, name = name, enabled = enabled) {

    val id: UUID = UUID.randomUUID()
    private val defaultAlignment = Alignment(
        alignment.horizontalAlignment,
        alignment.horizontalOffset,
        alignment.verticalAlignment,
        alignment.verticalOffset,
    )
    var zIndex by int("ZIndex", 0, 0..Int.MAX_VALUE).notAnOption()
    val alignment = tree(alignment)

    fun resetAlignment() {
        alignment.setFrom(defaultAlignment)
    }

    protected fun registerComponentListen(valueGroup: ValueGroup) {
        for (v in valueGroup.inner) {
            when (v) {
                is ModeValueGroup<*> -> {
                    v.onChanged {
                        HudComponentManager.updateComponents()
                    }
                    registerComponentListen(v)
                    v.modes.forEach(::registerComponentListen)
                }
                is ValueGroup -> registerComponentListen(v)
                else -> v.onChanged {
                    HudComponentManager.updateComponents()
                }
            }
        }
    }

}
