package engine.objects;

import engine.InterestManagement.WorldGrid;
import engine.gameManager.ZergManager;
import engine.math.Vector3fImmutable;
import engine.server.MBServerStatics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class ZergTracker {
    public ArrayList<PlayerCharacter> inRangeCurrent;
    public HashMap<Guild,ArrayList<PlayerCharacter>> playersByNation;
    public HashMap<PlayerCharacter,Long> leaveQue;
    public ArrayList<PlayerCharacter> totalAttended;

    public ZergTracker(){
        this.inRangeCurrent = new ArrayList<>();
        this.playersByNation = new HashMap<>();
        this.leaveQue = new HashMap<>();
        this.totalAttended = new ArrayList<>();
    }

    public void compileCurrent(Vector3fImmutable loc, float range){
        this.inRangeCurrent = new ArrayList<>();
        HashSet<AbstractWorldObject> current = WorldGrid.getObjectsInRangePartial(loc,range, MBServerStatics.MASK_PLAYER);
        for(AbstractWorldObject awo : current){
            this.inRangeCurrent.add((PlayerCharacter)awo);
            if(!this.totalAttended.contains((PlayerCharacter)awo))
                this.totalAttended.add((PlayerCharacter)awo);
        }
    }

    public void sortByNation(){
        this.playersByNation = new HashMap<>();
        for(PlayerCharacter pc : this.inRangeCurrent){
            Guild nation = pc.guild.getNation();
            if(this.playersByNation.containsKey(nation)){
                this.playersByNation.get(nation).add(pc);
            }else{
                ArrayList<PlayerCharacter> newList = new ArrayList<>();
                newList.add(pc);
                this.playersByNation.put(nation,newList);
            }
        }
        for(PlayerCharacter pc : this.leaveQue.keySet()){
            Guild nation = pc.guild.getNation();
            if(this.playersByNation.containsKey(nation)){
                this.playersByNation.get(nation).add(pc);
            }else{
                ArrayList<PlayerCharacter> newList = new ArrayList<>();
                newList.add(pc);
                this.playersByNation.put(nation,newList);
            }
        }
    }

    public void processLeaveQue(){
        ArrayList<PlayerCharacter> toRemove = new ArrayList<>();
        for(PlayerCharacter pc : this.leaveQue.keySet()){
            if(System.currentTimeMillis() > this.leaveQue.get(pc) + MBServerStatics.THREE_MINUTES){
                toRemove.add(pc);
            }
        }
        for(PlayerCharacter pc : toRemove){
            this.leaveQue.remove(pc);
            pc.ZergMultiplier = 1.0f;
            this.totalAttended.remove(pc);
        }
    }

    public void compileLeaveQue(Vector3fImmutable loc, float range){
        HashSet<AbstractWorldObject> current = WorldGrid.getObjectsInRangePartial(loc,range, MBServerStatics.MASK_PLAYER);
        for(PlayerCharacter pc : totalAttended){
            if(!current.contains(pc) && !this.leaveQue.containsKey(pc)){
                this.leaveQue.put(pc,System.currentTimeMillis());
            }
        }
    }

    public void applyMultiplier(int cap){
        for(PlayerCharacter pc : this.inRangeCurrent){
            int count = this.playersByNation.get(pc.guild.getNation()).size();
            pc. ZergMultiplier = ZergManager.getCurrentMultiplier(cap,count);
        }
    }
}
