/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.secret;

import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;

import java.util.function.Consumer;

public class BouncingText implements Collidable {
    private int collisionCooldown = 0;
    private float x, y;
    private float vx, vy;
    private final float scale;

    private int color;
    private final String text;

    private final Font font;

    // 可选：碰撞回调（比如触发 openUri）
    private Consumer<BouncingText> onBounce;

    public BouncingText(Font font, String text, float x, float y, float scale) {
        this.font = font;
        this.text = text;
        this.x = x;
        this.y = y;
        this.vx = vx;
        this.vy = vy;
        this.scale = scale;
        this.color = 0xFFFFFF;
    }

    public void setOnBounce(Consumer<BouncingText> onBounce) {
        this.onBounce = onBounce;
    }

    public void tick(int screenWidth, int screenHeight) {
        if (collisionCooldown > 0) collisionCooldown--;
        int textWidth = (int) (font.width(text) * scale);
        int textHeight = (int) (font.lineHeight * scale);

        x += vx;
        y += vy;

        boolean bounced = false;

        if (x <= 0 || x + textWidth >= screenWidth) {
            vx = -vx;
            bounced = true;
        }

        if (y <= 0 || y + textHeight >= screenHeight) {
            vy = -vy;
            bounced = true;
        }

        if (bounced) {
            // 随机颜色
            color = (int) (Math.random() * 0xFFFFFF);

            if (onBounce != null) {
                onBounce.accept(this);
            }
        }
    }

    @Override
    public int getCollisionCooldown() {
        return collisionCooldown;
    }

    @Override
    public void setCollisionCooldown(int cooldown) {
        this.collisionCooldown = cooldown;
    }

    @Override
    public void onCollision(Collidable other) {
        if (collisionCooldown > 0) return;

        collisionCooldown = 5;

        this.vx = -this.vx * 0.9f;
        this.vy = -this.vy * 0.9f;

        this.color = (int) (Math.random() * 0xFFFFFF);
    }

    public void setVelocity(float vx, float vy) {
        this.vx = vx;
        this.vy = vy;
    }

    public void render(GuiGraphics g) {
        g.pose().pushPose();

        g.pose().translate(x, y, 0);
        g.pose().scale(scale, scale, 1.0f);

        g.drawString(font, text, 0, 0, color);

        g.pose().popPose();
    }

    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }

    @Override
    public float getWidth() {
        return font.width(text) * scale;
    }

    @Override
    public float getHeight() {
        return font.lineHeight * scale;
    }

    @Override
    public float getVx() {
        return vx;
    }

    @Override
    public float getVy() {
        return vy;
    }
}