/*
 * 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;

import com.remains_cave.dungeon.tomb.ChallengeType;

public class ClientChallengeData {
    public static ChallengeData current = null;

    public static void update(ChallengeData data) {
        current = data;
    }

    public static void clear() {
        current = null;
    }

    public static class ChallengeData {
        public final String roomId;
        public final ChallengeType challengeType;
        public int wave;
        public float monsterProgress;
        public float timeProgress;
        public float waveProgress;
        private float targetMonsterProgress;
        private float targetTimeProgress;
        private float targetWaveProgress;
        private long lastUpdateTime;
        public long lastRenderTime;

        private int phase;              // 0=准备,1=战斗,2=波次间隔
        private long localTimerStart;   // 本地计时开始时间（毫秒）

        private static final int PREPARATION_DURATION_MS = 9 * 1000;
        private static final int WAVE_INTERVAL_DURATION_MS = 9 * 1000;

        // 构造函数名称必须与类名一致：ChallengeData
        public ChallengeData(String roomId, ChallengeType challengeType, int wave,
                             float monsterProgress, float timeProgress, float waveProgress, int phase) {
            this.roomId = roomId;
            this.challengeType = challengeType;
            this.wave = wave;
            this.monsterProgress = monsterProgress;
            this.timeProgress = timeProgress;
            this.waveProgress = waveProgress;
            this.targetMonsterProgress = monsterProgress;
            this.targetTimeProgress = timeProgress;
            this.targetWaveProgress = waveProgress;
            this.lastUpdateTime = System.currentTimeMillis();
            this.lastRenderTime = lastUpdateTime;
            this.phase = phase;
            if (phase == 0) {
                this.localTimerStart = System.currentTimeMillis();
            } else {
                this.localTimerStart = -1;
            }
        }

        public void updateTarget(float newMonster, float newTime, float newWave, int newWaveNumber, int newPhase) {
            this.lastUpdateTime = System.currentTimeMillis();

            // 波次变化或阶段变化时重置本地计时
            if (newWaveNumber != this.wave || newPhase != this.phase) {
                this.wave = newWaveNumber;
                this.phase = newPhase;
                // 重置目标值
                this.targetMonsterProgress = newMonster;
                this.targetTimeProgress = newTime;
                this.targetWaveProgress = newWave;
                this.monsterProgress = newMonster;
                this.timeProgress = newTime;
                this.waveProgress = newWave;

                // 根据新阶段重置本地计时器
                if (newPhase == 0) {
                    this.localTimerStart = System.currentTimeMillis();
                } else if (newPhase == 2) {
                    this.localTimerStart = System.currentTimeMillis();
                } else {
                    this.localTimerStart = -1;
                }
                return;
            }

            // 阶段未变，更新目标值（战斗阶段 或 波次间隔阶段）
            if (phase == 1 || phase == 2) {  // 关键修改：允许 phase 2 也更新目标值
                this.targetMonsterProgress = newMonster;
                this.targetTimeProgress = newTime;
                this.targetWaveProgress = newWave;
            }
            // 准备阶段（phase 0）不更新目标值
        }

        public void update(float deltaTime) {
            long now = System.currentTimeMillis();

            if (phase == 0 && localTimerStart != -1) {
                // 准备阶段：怪物和时间进度都从0→1
                long elapsed = now - localTimerStart;
                float progress = Math.min(1.0f, elapsed / (float) PREPARATION_DURATION_MS);
                monsterProgress = progress;
                timeProgress = progress;   // 改为同步增加
                waveProgress = 0.0f;
                return;
            } else if (phase == 2 && localTimerStart != -1) {
                // 波次间隔阶段：怪物进度从0→1（模拟等待），时间由服务端控制
                long elapsed = now - localTimerStart;
                float progress = Math.min(1.0f, elapsed / (float) WAVE_INTERVAL_DURATION_MS);
                monsterProgress = progress;
                // 时间进度仍使用服务端目标值（由服务端控制倒计时）
                timeProgress = moveTowards(timeProgress, targetTimeProgress, 6.0f * deltaTime);
                waveProgress = moveTowards(waveProgress, targetWaveProgress, 6.0f * deltaTime);
                return;
            }

            // 战斗阶段：所有进度向目标值插值
            float speed = 6.0f;
            float step = speed * deltaTime;
            monsterProgress = moveTowards(monsterProgress, targetMonsterProgress, step);
            timeProgress = moveTowards(timeProgress, targetTimeProgress, step);
            waveProgress = moveTowards(waveProgress, targetWaveProgress, step);
        }

        private float moveTowards(float current, float target, float maxDelta) {
            if (Math.abs(target - current) <= maxDelta) return target;
            return current + Math.signum(target - current) * maxDelta;
        }
    }
}