/*
 *  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 org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public class BouncingButton implements Collidable {
    private float x, y;
    private float width, height;
    private float scale;
    private int color;
    private final String text;
    private final Font font;
    private int collisionCooldown = 0;
    private Consumer<BouncingButton> onClick;

    private float vx = 0f;
    private float vy = 0f;

    public BouncingButton(Font font, String text, float x, float y, float width, float height, float scale) {
        this.font = font;
        this.text = text;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.scale = scale;
        this.color = 0xFFFFFF;
    }

    public void setOnClick(Consumer<BouncingButton> onClick) {
        this.onClick = onClick;
    }

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

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

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

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

    @Override
    public float getX() {
        return x;
    }

    @Override
    public float getY() {
        return y;
    }

    @Override
    public float getWidth() {
        return width;
    }

    @Override
    public float getHeight() {
        return height;
    }

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

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

    public void tick(int screenWidth, int screenHeight) {
        if (collisionCooldown > 0) collisionCooldown--;
        x += vx;
        y += vy;

        boolean bounced = false;

        if (x <= 0 || x + width >= screenWidth) {
            vx = -vx;
            bounced = true;
        }
        if (y <= 0 || y + height >= screenHeight) {
            vy = -vy;
            bounced = true;
        }
        
        if (bounced) {
            color = 0xFF0000 | (int) (Math.random() * 0xFFFFFF);
        }
    }

    @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 render(@NotNull GuiGraphics g, int mouseX, int mouseY) {
        g.pose().pushPose();

        int bgColor = isHovered(mouseX, mouseY) ? 0xAA333333 : 0xAA000000;
        g.fill((int)x, (int)y, (int)(x + width), (int)(y + height), bgColor);

        g.pose().translate(x + width / 2, y + height / 2, 0);
        g.pose().scale(scale, scale, 1f);

        int textWidth = font.width(text);
        int textHeight = font.lineHeight;
        g.drawString(font, text, -textWidth / 2, -textHeight / 2, color);

        g.pose().popPose();
    }

    public boolean isHovered(int mouseX, int mouseY) {
        return mouseX >= x && mouseX <= x + width
                && mouseY >= y && mouseY <= y + height;
    }

    public void mouseClicked(double mouseX, double mouseY, int button) {
        if (button == 0 && isHovered((int)mouseX, (int)mouseY)) {
            if (onClick != null) onClick.accept(this);
        }
    }

    
    public void setColor(int color) { this.color = color; }
    public void setScale(float scale) { this.scale = scale; }
    public void setPosition(float x, float y) { this.x = x; this.y = y; }
    public void setSize(float width, float height) { this.width = width; this.height = height; }
}