package domain.actions;

import com.microsoft.msr.malmo.AgentHost;
import domain.AbstractAction;
import domain.BlockType;
import domain.fluents.BlockAt;
import main.Observations;

import java.util.List;

import static main.JavaAgent.*;

public class FindBlock extends AbstractAction {

    private final BlockType targetBlock;
    private final int xObservationSize = Math.abs(X_START_OBSERVATION - X_STOP_OBSERVATION) + 1;
    private final int yObservationSize = Math.abs(Y_START_OBSERVATION - Y_STOP_OBSERVATION) + 1;
    private final int zObservationSize = Math.abs(Z_START_OBSERVATION - Z_STOP_OBSERVATION) + 1;
    private BlockType[][][] blocks = new BlockType[xObservationSize][yObservationSize][zObservationSize];

    public FindBlock(AgentHost agentHost, BlockType blockType) {
        super(agentHost);
        targetBlock = blockType;
    }

    @Override
    public void doAction(Observations observations) {
            BlockAt position = findBlock(observations.XPos, observations.YPos, observations.ZPos, observations.CellObs);
    }

    private BlockAt findBlock(float xPos, float yPos, float zPos, List<String> cellObs) {
        //TODO special case any
        int xRelative = 0;
        int yRelative = 0;
        int zRelative = 0;
        int i = 0;
        for (String block : cellObs) {
            if (targetBlock.compareTo(BlockType.valueOf(block)) == 0) {
                // calculate position in the grid
                xRelative = i % xObservationSize;
                yRelative = (i % (xObservationSize * yObservationSize)) / xObservationSize;
                zRelative = i / (xObservationSize * yObservationSize);
                // calculate position relative to us
                xRelative += X_START_OBSERVATION + 1;
                yRelative += Y_START_OBSERVATION;
                zRelative += Z_START_OBSERVATION + 1;
                break;
                //TODO maybe find nearest Block isntead?
            }
            i++;
        }
        if (xRelative == yRelative && yRelative == zRelative && zRelative == 0){
            // no blocks of given type found
            return null;
        }
        BlockAt blockAt = new BlockAt(xPos + xRelative, yPos + yRelative, zPos + zRelative, targetBlock);
        return blockAt;
    }
}

@@ -146,16 +146,44 @@
 lockAt;%0A
+import domain.fluents.IsAt;%0A
 import m
@@ -953,28 +953,24 @@
         
-    
 BlockAt 
 position
@@ -965,28 +965,33 @@
 kAt 
-position = find
+dirt = findClosest
 Block
+Of
 (obs
@@ -1070,24 +1070,379 @@
 bs);%0A    %7D%0A%0A
+    private void parseBlockMatrix(List%3CString%3E cellObs) %7B%0A        int i = 0;%0A        for (String blockType : cellObs) %7B%0A            blocks%5Bi %25 xObservationSize%5D%5B(i %25 (xObservationSize * yObservationSize)) / xObservationSize%5D%0A                    %5Bi / (xObservationSize * yObservationSize)%5D = BlockType.valueOf(blockType);%0A            i++;%0A        %7D%0A    %7D%0A%0A
     private 
@@ -1453,21 +1453,30 @@
 kAt find
+Closest
 Block
+Of
 (float x
@@ -1540,31 +1540,61 @@
     
-//TODO special case any
+System.out.println(xPos + %22  %22 + yPos + %22  %22 + zPos);
 %0A   
@@ -1606,33 +1606,49 @@
 int xRelative = 
-0
+Integer.MAX_VALUE
 ;%0A        int yR
@@ -1649,33 +1649,49 @@
 int yRelative = 
-0
+Integer.MAX_VALUE
 ;%0A        int zR
@@ -1700,17 +1700,75 @@
 ative = 
-0
+Integer.MAX_VALUE;%0A        int distance = Integer.MAX_VALUE
 ;%0A      
@@ -1949,33 +1949,41 @@
                 
-x
+int nextX
 Relative = i %25 x
@@ -2007,33 +2007,41 @@
                 
-y
+int nextY
 Relative = (i %25 
@@ -2107,33 +2107,41 @@
                 
-z
+int nextZ
 Relative = i / (
@@ -2247,17 +2247,21 @@
         
-x
+nextX
 Relative
@@ -2275,36 +2275,32 @@
 TART_OBSERVATION
- + 1
 ;%0A              
@@ -2301,17 +2301,21 @@
         
-y
+nextY
 Relative
@@ -2355,17 +2355,21 @@
         
-z
+nextZ
 Relative
@@ -2395,93 +2395,504 @@
 TION
- + 1;%0A                break;%0A                //TODO maybe find nearest Block isntead?
+;%0A                // calculate distance to us%0A                int nextDistance = nextXRelative * nextXRelative + nextYRelative * nextYRelative + nextZRelative * nextZRelative;%0A                // check if block is closer than previous best result%0A                if (nextDistance %3C distance) %7B%0A                    xRelative = nextXRelative;%0A                    yRelative = nextYRelative;%0A                    zRelative = nextZRelative;%0A                    distance = nextDistance;%0A                %7D
 %0A   
@@ -3006,17 +3006,33 @@
 tive == 
-0
+Integer.MAX_VALUE
 )%7B%0A     
@@ -3098,32 +3098,68 @@
 null;%0A        %7D%0A
+        // return absolute position%0A
         BlockAt 
@@ -3277,11 +3277,98 @@
 t;%0A    %7D
+%0A%0A    public boolean isBlockAt (IsAt isAt) %7B%0A        //TODO%0A        return false;%0A    %7D
 %0A%7D%0A

Change FindBlock to find the nearest block