package mindustry.maps;

import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.ai.*;
import mindustry.content.*;
import mindustry.entities.*;
import mindustry.entities.abilities.*;
import mindustry.game.*;
import mindustry.gen.*;
import mindustry.logic.*;
import mindustry.world.*;
import mindustry.world.blocks.defense.*;
import mindustry.world.blocks.defense.turrets.*;
import mindustry.world.blocks.defense.turrets.Turret.*;
import mindustry.world.blocks.storage.*;

import static mindustry.Vars.*;

public class SectorDamage{
    //direct damage is for testing only
    private static final boolean direct = false, rubble = true;
    private static final int maxWavesSimulated = 50;

    /** @return calculated capture progress of the enemy */
    public static float getDamage(SectorInfo info, float waveSpace, int wave, int wavesPassed){
        float health = info.sumHealth;

        //this approach is O(n), it simulates every wave passing.
        //other approaches can assume all the waves come as one, but that's not as fair.
        if(wavesPassed > 0){
            int waveBegin = wave;
            int waveEnd = wave + wavesPassed;

            //do not simulate every single wave if there's too many
            if(wavesPassed > maxWavesSimulated){
                waveBegin = waveEnd - maxWavesSimulated;
            }

            for(int i = waveBegin; i <= waveEnd; i++){

                float efficiency = health / info.sumHealth;
                float dps = info.sumDps * efficiency;
                float rps = info.sumRps * efficiency;

                float enemyDps = info.waveDpsBase + info.waveDpsSlope * (i);
                float enemyHealth = info.waveHealthBase + info.waveHealthSlope * (i);

                //happens due to certain regressions
                if(enemyHealth < 0 || enemyDps < 0) continue;

                //calculate time to destroy both sides
                float timeDestroyEnemy = dps <= 0.0001f ? Float.POSITIVE_INFINITY : enemyHealth / dps; //if dps == 0, this is infinity
                float timeDestroyBase = health / (enemyDps - rps); //if regen > enemyDps this is negative

                //sector is lost, enemy took too long.
                if(timeDestroyEnemy > timeDestroyBase){
                    health = 0f;
                    break;
                }

                //otherwise, the enemy shoots for timeDestroyEnemy seconds, so calculate damage taken
                float damageTaken = timeDestroyEnemy * (enemyDps - rps);

                //damage the base.
                health -= damageTaken;

                //regen health after wave.
                health = Math.min(health + rps / 60f * waveSpace, info.sumHealth);
            }
        }

        return 1f - Mathf.clamp(health / info.sumHealth);
    }

    /** Applies wave damage based on sector parameters. */
    public static void applyCalculatedDamage(int wavesPassed){
        //calculate base damage fraction
        float damage = getDamage(state.secinfo, state.rules.waveSpacing, state.wave, wavesPassed);

        //scaled damage has a power component to make it seem a little more realistic (as systems fail, enemy capturing gets easier and easier)
        float scaled = Mathf.pow(damage, 1.5f);

        //apply damage to units
        float unitDamage = damage * state.secinfo.sumHealth;
        Tile spawn = spawner.getFirstSpawn();

        //damage only units near the spawn point
        if(spawn != null){
            Seq<Unit> allies = new Seq<>();
            for(Unit ally : Groups.unit){
                if(ally.team == state.rules.defaultTeam && ally.within(spawn, state.rules.dropZoneRadius * 2.5f)){
                    allies.add(ally);
                }
            }

            allies.sort(u -> u.dst2(spawn));

            //damage units one by one, not uniformly
            for(var u : allies){
                if(u.health < unitDamage){
                    u.remove();
                    unitDamage -= u.health;
                }else{
                    u.health -= unitDamage;
                    break;
                }
            }
        }

        //finally apply scaled damage
        apply(scaled);
    }

    /** Calculates damage simulation parameters before a game is saved. */
    public static void writeParameters(SectorInfo info){
        Building core = state.rules.defaultTeam.core();
        Seq<Tile> spawns = new Seq<>();
        spawner.eachGroundSpawn((x, y) -> spawns.add(world.tile(x, y)));

        if(core == null || spawns.isEmpty()) return;

        Tile start = spawns.first();

        Time.mark();
        var field = pathfinder.getField(state.rules.waveTeam, Pathfinder.costGround, Pathfinder.fieldCore);
        Seq<Tile> path = new Seq<>();
        boolean found = false;

        if(field != null && field.weights != null){
            int[][] weights = field.weights;
            int count = 0;
            Tile current = start;
            while(count < world.width() * world.height()){
                int minCost = Integer.MAX_VALUE;
                int cx = current.x, cy = current.y;
                for(Point2 p : Geometry.d4){
                    int nx = cx + p.x, ny = cy + p.y;

                    Tile other = world.tile(nx, ny);
                    if(other != null && weights[nx][ny] < minCost && weights[nx][ny] != -1){
                       minCost = weights[nx][ny];
                       current = other;
                    }
                }

                path.add(current);

                if(current.build == core){
                    found = true;
                    break;
                }

                count ++;
            }
        }

        if(!found){
            path = Astar.pathfind(start, core.tile, SectorDamage::cost, t -> !(t.block().isStatic() && t.solid()));
        }

        //create sparse tile array for fast range query
        int sparseSkip = 6;
        //TODO if this is slow, use a quadtree
        Seq<Tile> sparse = new Seq<>(path.size / sparseSkip + 1);

        for(int i = 0; i < path.size; i++){
            if(i % sparseSkip == 0){
                sparse.add(path.get(i));
            }
        }

        //regen is in health per second
        //dps is per second
        float sumHealth = 0f, sumRps = 0f, sumDps = 0f;
        float totalPathBuild = 0;

        //first, calculate the total health of blocks in the path

        for(Tile t : path){
            int radius = 2;

            //radius is square.
            for(int dx = -radius; dx <= radius; dx++){
                for(int dy = -radius; dy <= radius; dy++){
                    int wx = dx + t.x, wy = dy + t.y;
                    if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height()){
                        Tile tile = world.rawTile(wx, wy);

                        if(tile.build != null && tile.team() == state.rules.defaultTeam){
                            //health is divided by block size, because multiblocks are counted multiple times.
                            sumHealth += tile.build.health / tile.block().size;
                            totalPathBuild += 1f / tile.block().size;
                        }
                    }
                }
            }
        }

        float avgHealth = totalPathBuild <= 1 ? sumHealth : sumHealth / totalPathBuild;

        //block dps + regen + extra health/shields
        for(Building build : Groups.build){
            float e = build.efficiency();
            if(e > 0.08f){
                if(build.team == state.rules.defaultTeam && build instanceof Ranged ranged && sparse.contains(t -> t.within(build, ranged.range()))){
                    if(build.block instanceof Turret t && build instanceof TurretBuild b && b.hasAmmo()){
                        sumDps += t.shots / t.reloadTime * 60f * b.peekAmmo().estimateDPS() * e;
                    }

                    if(build.block instanceof MendProjector m){
                        sumRps += m.healPercent / m.reload * avgHealth * 60f / 100f * e;
                    }

                    if(build.block instanceof ForceProjector f){
                        sumHealth += f.breakage * e;
                        sumRps += 1f * e;
                    }
                }
            }
        }

        float curEnemyHealth = 0f, curEnemyDps = 0f;

        //unit regen + health + dps
        for(Unit unit : Groups.unit){
            //skip player
            if(unit.isPlayer()) continue;

            if(unit.team == state.rules.defaultTeam){
                sumHealth += unit.health + unit.shield;
                sumDps += unit.type().dpsEstimate;
                if(unit.abilities.find(a -> a instanceof HealFieldAbility) instanceof HealFieldAbility h){
                    sumRps += h.amount / h.reload * 60f;
                }
            }else{
                curEnemyDps += unit.type().dpsEstimate;
                curEnemyHealth += unit.health;
            }
        }

        //calculate DPS and health for the next few waves and store in list
        var reg = new LinearRegression();
        Seq<Vec2> waveDps = new Seq<>(), waveHealth = new Seq<>();

        for(int wave = state.wave, i = 0; i < 3; wave += (1 + i++)){
            float sumWaveDps = 0f, sumWaveHealth = 0f;

            //first wave has to take into account current dps
            if(wave == state.wave){
                sumWaveDps += curEnemyDps;
                sumWaveHealth += curEnemyHealth;
            }

            for(SpawnGroup group : state.rules.spawns){
                int spawned = group.getSpawned(wave);
                if(spawned <= 0) continue;
                sumWaveHealth += spawned * (group.getShield(wave) + group.type.health);
                sumWaveDps +=  spawned * group.type.dpsEstimate;
            }
            waveDps.add(new Vec2(wave, sumWaveDps));
            waveHealth.add(new Vec2(wave, sumWaveHealth));
        }

        //calculate linear regression of the wave data and store it
        reg.calculate(waveHealth);
        info.waveHealthBase = reg.intercept;
        info.waveHealthSlope = reg.slope;

        reg.calculate(waveDps);
        info.waveDpsBase = reg.intercept;
        info.waveDpsSlope = reg.slope;

        info.sumHealth = sumHealth;
        info.sumDps = sumDps;
        info.sumRps = sumRps;

        //finally, find an equation to put it all together and produce a 0-1 number
        //due to the way most defenses are structured, this number will likely need a ^4 power or so
    }

    public static void apply(float fraction){
        Tiles tiles = world.tiles;

        Queue<Tile> frontier = new Queue<>();
        float[][] values = new float[tiles.width][tiles.height];
        float damage = fraction*80; //arbitrary damage value

        //phase one: find all spawnpoints
        for(Tile tile : tiles){
            if((tile.block() instanceof CoreBlock && tile.team() == state.rules.waveTeam) || tile.overlay() == Blocks.spawn){
                frontier.add(tile);
                values[tile.x][tile.y] = damage;
            }
        }

        Building core = state.rules.defaultTeam.core();
        if(core != null && !frontier.isEmpty()){
            for(Tile spawner : frontier){
                //find path from spawn to core
                Seq<Tile> path = Astar.pathfind(spawner, core.tile, SectorDamage::cost, t -> !(t.block().isStatic() && t.solid()));
                Seq<Building> removal = new Seq<>();

                int radius = 3;

                //only penetrate a certain % by health, not by distance
                float totalHealth = path.sumf(t -> {
                    float s = 0;
                    for(int dx = -radius; dx <= radius; dx++){
                        for(int dy = -radius; dy <= radius; dy++){
                            int wx = dx + t.x, wy = dy + t.y;
                            if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height() && Mathf.within(dx, dy, radius)){
                                Tile other = world.rawTile(wx, wy);
                                s += other.team() == state.rules.defaultTeam ? other.build.health / other.block().size : 0f;
                            }
                        }
                    }
                    return s;
                });
                float targetHealth = totalHealth * fraction;
                float healthCount = 0;

                out:
                for(int i = 0; i < path.size && healthCount < targetHealth; i++){
                    Tile t = path.get(i);

                    for(int dx = -radius; dx <= radius; dx++){
                        for(int dy = -radius; dy <= radius; dy++){
                            int wx = dx + t.x, wy = dy + t.y;
                            if(wx >= 0 && wy >= 0 && wx < world.width() && wy < world.height() && Mathf.within(dx, dy, radius)){
                                Tile other = world.rawTile(wx, wy);

                                //just remove all the buildings in the way - as long as they're not cores
                                if(other.build != null && other.team() == state.rules.defaultTeam && !(other.block() instanceof CoreBlock)){
                                    if(rubble && !other.floor().solid && !other.floor().isLiquid && Mathf.chance(0.4)){
                                        Effect.rubble(other.build.x, other.build.y, other.block().size);
                                    }

                                    //since the whole block is removed, count the whole health
                                    healthCount += other.build.health;

                                    removal.add(other.build);

                                    if(healthCount >= targetHealth){
                                        break out;
                                    }
                                }
                            }
                        }
                    }
                }

                for(Building r : removal){
                    if(r.tile.build == r){
                        r.tile.remove();
                    }
                }
            }
        }

        //kill every core if damage is maximum
        if(damage >= 1){
            for(Building c : state.rules.defaultTeam.cores().copy()){
                c.tile.remove();
            }
        }

        float falloff = (damage) / (Math.max(tiles.width, tiles.height) * Mathf.sqrt2);
        int peak = 0;

        //phase two: propagate the damage
        while(!frontier.isEmpty()){
            peak = Math.max(peak, frontier.size);
            Tile tile = frontier.removeFirst();
            float currDamage = values[tile.x][tile.y] - falloff;

            for(int i = 0; i < 4; i++){
                int cx = tile.x + Geometry.d4x[i], cy = tile.y + Geometry.d4y[i];

                //propagate to new tiles
                if(tiles.in(cx, cy) && values[cx][cy] < currDamage){
                    Tile other = tiles.getn(cx, cy);
                    float resultDamage = currDamage;

                    //damage the tile if it's not friendly
                    if(other.build != null && other.team() != state.rules.waveTeam){
                        resultDamage -= other.build.health();

                        if(direct){
                            other.build.damage(currDamage);
                        }else{ //indirect damage happens at game load time
                            other.build.health -= currDamage;
                            //don't kill the core!
                            if(other.block() instanceof CoreBlock) other.build.health = Math.max(other.build.health, 1f);

                            //remove the block when destroyed
                            if(other.build.health < 0){
                                //rubble
                                if(rubble && !other.floor().solid && !other.floor().isLiquid && Mathf.chance(0.4)){
                                    Effect.rubble(other.build.x, other.build.y, other.block().size);
                                }

                                other.remove();
                            }
                        }

                    }else if(other.solid() && !other.synthetic()){ //skip damage propagation through solid blocks
                        continue;
                    }

                    if(resultDamage > 0 && values[cx][cy] < resultDamage){
                        frontier.addLast(other);
                        values[cx][cy] = resultDamage;
                    }
                }
            }
        }
    }

    static float cost(Tile tile){
        return 1f +
            (tile.block().isStatic() && tile.solid() ? 200f : 0f) +
            (tile.build != null ? tile.build.health / 40f : 0f) +
            (tile.floor().isLiquid ? 10f : 0f);
    }
}
