/*
 * 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.client.ClientChallengeData;
import com.remains_cave.dungeon.tomb.ChallengeType;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
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;
import net.neoforged.neoforge.client.event.RenderGuiEvent;
import net.neoforged.neoforge.event.level.LevelEvent;

@EventBusSubscriber(modid = "remains_cave", value = Dist.CLIENT)
public class ChallengeOverlayRenderer {

    private static final ResourceLocation WARRIOR = ResourceLocation.parse("remains_cave:textures/gui/warrior_trial_bars.png");
    private static final ResourceLocation ARCHER = ResourceLocation.parse("remains_cave:textures/gui/archer_trial_bars.png");
    private static final ResourceLocation WARLOCK = ResourceLocation.parse("remains_cave:textures/gui/warlock_trial_bars.png");

    private static final int BASE_WIDTH = 128;
    private static final int BASE_HEIGHT = 160; // 整个纹理高度
    private static final float SCALE = 1.25f;

    @SubscribeEvent
    public static void onRenderGui(RenderGuiEvent.Post event) {
        ClientChallengeData.ChallengeData data = ClientChallengeData.current;
        if (data == null) return;

        Minecraft mc = Minecraft.getInstance();
        if (mc.player == null) return;

        long now = System.currentTimeMillis();
        float deltaTime = (now - data.lastRenderTime) / 1000.0f;
        if (deltaTime > 0.05f) deltaTime = 0.05f; // 限制最大间隔
        data.update(deltaTime);
        data.lastRenderTime = now;

        ResourceLocation texture = switch (data.challengeType) {
            case WARRIOR -> WARRIOR;
            case ARCHER -> ARCHER;
            case WARLOCK -> WARLOCK;
            default -> null;
        };
        if (texture == null) return;

        GuiGraphics gui = event.getGuiGraphics();
        int screenWidth = mc.getWindow().getGuiScaledWidth();
        int screenHeight = mc.getWindow().getGuiScaledHeight();

        // 缩放后 UI 的实际尺寸
        int scaledWidth = (int) (BASE_WIDTH * SCALE);
        int scaledHeight = (int) (BASE_HEIGHT * SCALE);
        int x = (screenWidth - scaledWidth) / 2;
        int y = (screenHeight - scaledHeight) / 2 - 20;

        RenderSystem.enableBlend();

        // 使用矩阵变换实现均匀缩放
        var pose = gui.pose();
        pose.pushPose();
        pose.translate(x, y, 0);
        pose.scale(SCALE, SCALE, 1.0f);

        // 以下所有坐标均为未缩放前的 UI 内部坐标（相对于左上角）
        // 1. 背景层 (纹理区域 0-128, 129-160)
        gui.blit(texture, 0, 0, 0, 128, BASE_WIDTH, 32, BASE_WIDTH, BASE_HEIGHT);

        // 2. 怪物进度条 (纹理区域 8-120, 33-64)
        int monsterWidth = (int) (115 * data.monsterProgress);
        if (monsterWidth > 0) {
            gui.blit(texture, 7, 0, 7, 32, monsterWidth, 32, BASE_WIDTH, BASE_HEIGHT);
        }

        // 3. 时间条 (纹理区域 3-124, 65-96)
        int timeWidth = (int) (123 * data.timeProgress);
        if (timeWidth > 0) {
            gui.blit(texture, 3, 0, 3, 64, timeWidth, 32, BASE_WIDTH, BASE_HEIGHT);
        }

        // 4. 波次条 (纹理区域 37-90, 65-96)
        int waveWidth = (int) (55 * data.waveProgress);
        if (waveWidth > 0) {
            gui.blit(texture, 37, 0, 37, 96, waveWidth, 32, BASE_WIDTH, BASE_HEIGHT);
        }

        // 5. 最上层 (纹理区域 0-128, 0-32)
        gui.blit(texture, 0, 0, 0, 0, BASE_WIDTH, 32, BASE_WIDTH, BASE_HEIGHT);

        pose.popPose();

        RenderSystem.disableBlend();
    }
    @SubscribeEvent
    public static void onLevelUnload(LevelEvent.Unload event) {
        if (event.getLevel().isClientSide()) {
            ClientChallengeData.clear();
        }
    }

    @SubscribeEvent
    public static void onRenderBossBar(CustomizeGuiOverlayEvent.BossEventProgress event) {
        if (ClientChallengeData.current != null) {
            event.setCanceled(true);
        }
    }
}