/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.client.gui;

import com.mojang.blaze3d.systems.RenderSystem;
import com.remains_cave.RemainsCaveMod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.BossEvent;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.client.event.CustomizeGuiOverlayEvent;

@EventBusSubscriber(modid = RemainsCaveMod.MODID, value = Dist.CLIENT)
public class CustomBossOverlay {
    private static final ResourceLocation BOSS_BAR_TEXTURE =
            ResourceLocation.fromNamespaceAndPath(RemainsCaveMod.MODID, "textures/gui/bossbars.png");

//    private static final int BAR_WIDTH = 182;
//    private static final int BAR_HEIGHT = (int) Math.round(16 * (BAR_WIDTH / 128.0)); // 23

    private static final int BAR_WIDTH = 128; // 改为纹理原始宽度
    private static final int BAR_HEIGHT = 16; // 改为纹理原始高度

    private static final int TEXTURE_WIDTH = 128;
    private static final int TEXTURE_HEIGHT = 48; // 原32改为48

    // 背景区域（上半部分）
    private static final int BG_U = 0;
    private static final int BG_V = 0;
    private static final int BG_W = TEXTURE_WIDTH;
    private static final int BG_H = 16;

    // 前景区域（下半部分）
    private static final int FG_U = 0;
    private static final int FG_V = 16;
    private static final int FG_W = TEXTURE_WIDTH;
    private static final int FG_H = 16;
    // 最底层背景区域 (0,32) ~ (128,48)
    private static final int BG_BASE_U = 0;
    private static final int BG_BASE_V = 32;
    private static final int BG_BASE_W = TEXTURE_WIDTH;
    private static final int BG_BASE_H = 16;
    @SubscribeEvent
    public static void onRenderBossBar(CustomizeGuiOverlayEvent.BossEventProgress event) {
        BossEvent bossEvent = event.getBossEvent();

        // 判断是否为骷髅王
        if (!bossEvent.getName().getString().equals("Skeleton King")) {
            return;
        }
        event.setCanceled(true);

        Minecraft mc = Minecraft.getInstance();
        GuiGraphics guiGraphics = event.getGuiGraphics();
        int screenWidth = mc.getWindow().getGuiScaledWidth();
        int x = (screenWidth - BAR_WIDTH) / 2;
        int y = 10; // 距离屏幕顶部10像素，可根据需要调整

        float progress = bossEvent.getProgress();
        int filledWidth = (int) (BAR_WIDTH * progress);
        int sourceWidth = (int) (TEXTURE_WIDTH * progress);

        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
        RenderSystem.setShaderTexture(0, BOSS_BAR_TEXTURE);

        // 1. 前景（颜色填充）
        guiGraphics.blit(
                BOSS_BAR_TEXTURE,
                x, y, BAR_WIDTH, BAR_HEIGHT,
                BG_BASE_U, BG_BASE_V, BG_BASE_W, BG_BASE_H,
                TEXTURE_WIDTH, TEXTURE_HEIGHT
        );

// 2. 前景（按进度裁剪）
        if (filledWidth > 0 && sourceWidth > 0) {
            guiGraphics.blit(
                    BOSS_BAR_TEXTURE,
                    x, y, filledWidth, BAR_HEIGHT,
                    FG_U, FG_V, sourceWidth, FG_H,
                    TEXTURE_WIDTH, TEXTURE_HEIGHT
            );
        }

// 3. 顶层背景（镂空边框，完整宽度）
        guiGraphics.blit(
                BOSS_BAR_TEXTURE,
                x, y, BAR_WIDTH, BAR_HEIGHT,
                BG_U, BG_V, BG_W, BG_H,
                TEXTURE_WIDTH, TEXTURE_HEIGHT
        );

        // 3. 绘制 Boss 名称（本地化 + 红色）
        Component displayName = Component.translatable("entity.remains_cave.skeleton_king");
        guiGraphics.drawString(
                mc.font,
                displayName,
                x + (BAR_WIDTH / 2) - (mc.font.width(displayName) / 2),
                y - 9,
                0xe55050,  // 红色
                false
        );
    }
}