using System;
using CounterDrone.Core.Models;

namespace CounterDrone.Core.Algorithms
{
    /// <summary>吸入式爆炸 — 指数累积型</summary>
    public class ActiveFuelDamageModel : IDamageModel
    {
        private const float BaseRate = 0.02f;
        private const float ExpFactor = 0.3f;

        public float CalculateDamage(DroneType droneType, PowerType powerType,
            AerosolType aerosolType, float cloudDensity, float exposureTime, float deltaTime)
        {
            if (aerosolType != AerosolType.ActiveFuel) return 0f;
            var exponential = (float)System.Math.Exp(exposureTime * ExpFactor);
            var damage = BaseRate * exponential * deltaTime;
            var sensitivity = droneType == DroneType.HighSpeed ? 1.5f : 1.0f;
            return damage * sensitivity;
        }

        public float RequiredExposureSeconds(DroneType droneType, PowerType powerType, AerosolType aerosolType)
        {
            if (aerosolType != AerosolType.ActiveFuel) return float.MaxValue;
            var sensitivity = droneType == DroneType.HighSpeed ? 1.5f : 1.0f;
            // 数值求解：BaseRate * e^(t*ExpFactor) * sensitivity 的积分 = 1
            // ∫[0,T] BaseRate * s * e^(k*t) dt = BaseRate * s * (e^(k*T) - 1) / k = 1
            // e^(k*T) = 1 + k / (BaseRate * s)
            float k = ExpFactor;
            float bs = BaseRate * sensitivity;
            float target = 1f + k / bs;
            return (float)Math.Log(target) / k;
        }

        public DamageStage GetDamageStage(float accumulatedDamage)
        {
            if (accumulatedDamage >= 1.0f) return DamageStage.Destroyed;
            if (accumulatedDamage >= 0.6f) return DamageStage.AttitudeLoss;
            if (accumulatedDamage >= 0.3f) return DamageStage.EngineAnomaly;
            return DamageStage.Normal;
        }
    }
}
