package comlib.adk.team.tactics.basic;

import comlib.adk.team.tactics.PoliceForceTactics;
import comlib.adk.util.action.PoliceAction;
import comlib.adk.util.route.RouteSearcher;
import comlib.adk.util.target.BlockadeSelector;
import comlib.manager.MessageManager;
import comlib.message.information.BuildingMessage;
import comlib.message.information.CivilianMessage;
import rescuecore2.messages.Message;
import rescuecore2.misc.geometry.GeometryTools2D;
import rescuecore2.misc.geometry.Line2D;
import rescuecore2.misc.geometry.Point2D;
import rescuecore2.misc.geometry.Vector2D;
import rescuecore2.standard.entities.*;
import rescuecore2.worldmodel.ChangeSet;
import rescuecore2.worldmodel.EntityID;

import java.util.List;

public abstract class BasicPolice extends PoliceForceTactics{

    public BlockadeSelector blockadeSelector;

    int count = 0;

    public RouteSearcher routeSearcher;

		public ChangeSet changed; // temp add

    @Override
    public void preparation() {
        this.model.indexClass(StandardEntityURN.ROAD);
        this.routeSearcher = this.getRouteSearcher();
        this.blockadeSelector = this.getBlockadeSelector();
    }

    public abstract BlockadeSelector getBlockadeSelector();

    public abstract RouteSearcher getRouteSearcher();

    @Override
    public Message think(int time, ChangeSet changed, MessageManager manager) {

			this.changed = changed; //temp add

        this.updateInfo(changed, manager);

        if(this.target == null) {
            this.target = this.blockadeSelector.getTarget(time);
        }
        Blockade blockade = (Blockade)this.model.getEntity(this.target);
        if(blockade != null) {
            if(blockade.getPosition().equals(this.location.getID())) {
                /*
                List<Line2D> lines = GeometryTools2D.pointsToLines(GeometryTools2D.vertexArrayToPoints(blockade.getApexes()), true);

                double best = Double.MAX_VALUE;
                Point2D bestPoint = null;
                Point2D origin = new Point2D(this.me.getX(), this.me.getY());
                for (Line2D next : lines) {
                    Point2D closest = GeometryTools2D.getClosestPointOnSegment(next, origin);
                    double d = GeometryTools2D.getDistance(origin, closest);
                    if (d < best) {
                        best = d;
                        bestPoint = closest;
                    }
                }
                if(bestPoint != null) {
                    Vector2D v = bestPoint.minus(new Point2D(this.me.getX(), this.me.getY()));
                    v = v.normalised().scale(1000000);
                    return PoliceAction.clear(this, time, (int) (this.me.getX() + v.getX()), (int) (this.me.getY() + v.getY()));
							}
							*/
							//return PoliceAction.clear(this, time, blockade.getID());
                if(this.count >= 13) {
                    this.count = 0;
                }
                int x = (int)(this.distance * Math.cos(30 * count));
                int y = (int)(this.distance * Math.sin(30 * count));
                count++;
                return PoliceAction.clear(this, time, (int) (this.me.getX() + x), (int) (this.me.getY() + y));
            }
            else {
                List<EntityID> path = this.routeSearcher.getPath(time, this.me, blockade);
                return path != null ? PoliceAction.move(this, time, path) : PoliceAction.move(this, time, this.routeSearcher.randomWalk());
            }
        }
        else {
            this.blockadeSelector.remove(this.target);
            this.target = null;
        }
        return PoliceAction.move(this, time, this.routeSearcher.randomWalk());
    }

    private void updateInfo(ChangeSet changed, MessageManager manager) {
        for (EntityID next : changed.getChangedEntities()) {
            StandardEntity entity = this.model.getEntity(next);
            if(entity instanceof Civilian) {
                Civilian civilian = (Civilian)entity;
                if(civilian.getBuriedness() > 0) {
                    manager.addSendMessage(new CivilianMessage(civilian));
                }
            }
            else if(entity instanceof Blockade) {
                this.blockadeSelector.add((Blockade)entity);
            }
            else if(entity instanceof Building) {
                Building b = (Building)entity;
                if(b.isOnFire()) {
                    manager.addSendMessage(new BuildingMessage(b));
                }
            }
        }
    }

}

@@ -2856,17 +2856,17 @@
 s.count 
-%3E
+=
 = 13) %7B%0A
@@ -2909,34 +2909,152 @@
                 
-%7D%0A
+    return PoliceAction.move(this, time, this.routeSearcher.randomWalk());%0A                %7D%0A                else %7B%0A    
                 
@@ -3058,32 +3058,33 @@
    int x = (int)
+ 
 (this.distance *
@@ -3099,32 +3099,36 @@
 s(30 * count));%0A
+    
                 
@@ -3140,16 +3140,17 @@
  = (int)
+ 
 (this.di
@@ -3197,16 +3197,20 @@
         
+    
 count++;
@@ -3210,16 +3210,20 @@
 ount++;%0A
+    
         
@@ -3321,24 +3321,42 @@
 tY() + y));%0A
+                %7D%0A
             

fix sample