package com.fuzzywave.tetribattle.gameplay.statemachine;


import com.fuzzywave.tetribattle.TetriBattle;
import com.fuzzywave.tetribattle.gameplay.Block;
import com.fuzzywave.tetribattle.gameplay.BlockType;
import com.fuzzywave.tetribattle.gameplay.GameInstance;
import com.fuzzywave.tetribattle.gameplay.Piece;

public class PieceDropState implements State {

    @Override
    public void enter(GameInstance gameInstance) {
        if (checkGameEnd(gameInstance)) {
            createRandomPiece(gameInstance);
        } else {
            // TODO game end.
            TetriBattle.analytics.logEvent("GAME_END");
        }
    }

    @Override
    public void update(GameInstance gameInstance, float delta) {
        Piece currentPiece = gameInstance.getCurrentPiece();
        currentPiece.setNextDropTime(currentPiece.getNextDropTime() - delta);

        if (currentPiece.getNextDropTime() <= 0) {
            dropPiece(gameInstance);
        }

        checkUserInput(gameInstance);

        if (currentPiece.isMovementDone()) {
            // TODO switch to automatic drop state.
            StateMachine stateMachine = gameInstance.getStateMachine();
            stateMachine.changeState(stateMachine.pieceDropState);
        }
    }

    private void checkUserInput(GameInstance gameInstance) {
        if (gameInstance.isMoveLeft()) {
            moveLeft(gameInstance);
            gameInstance.setMoveLeft(false);
        }

        if (gameInstance.isMoveRight()) {
            moveRight(gameInstance);
            gameInstance.setMoveRight(false);
        }

        if (gameInstance.isFastDrop()) {
            gameInstance.getCurrentPiece().setFastDrop(true);
            gameInstance.setFastDrop(false);
        }

        if (gameInstance.isRotate()) {
            rotate(gameInstance);
            gameInstance.setRotate(false);
        }
    }

    private void rotate(GameInstance gameInstance) {
        gameInstance.getCurrentPiece().tryToRotate(gameInstance);
    }

    private void moveRight(GameInstance gameInstance) {
        gameInstance.getCurrentPiece().tryToMoveRight(gameInstance);

    }

    private void moveLeft(GameInstance gameInstance) {
        gameInstance.getCurrentPiece().tryToMoveLeft(gameInstance);
    }


    @Override
    public void exit(GameInstance gameInstance) {

    }

    public boolean checkGameEnd(GameInstance gameInstance) {
        Block block = gameInstance.getBlock(TetriBattle.BLOCK_SPAWN_X, TetriBattle.BLOCK_SPAWN_Y);
        return (block.getBlockType().equals(BlockType.EMPTY));
    }

    private void createRandomPiece(GameInstance gameInstance) {

        BlockType firstBlockType = BlockType.getRandomBlockType(gameInstance.getRandom());
        BlockType secondBlockType = BlockType.getRandomBlockType(gameInstance.getRandom());

        gameInstance.getCurrentPiece().initialize(firstBlockType, secondBlockType);
    }

    private void dropPiece(GameInstance gameInstance) {

        Piece currentPiece = gameInstance.getCurrentPiece();
        if(currentPiece.tryToMoveDown(gameInstance)){
            if(currentPiece.isFastDrop()) {
                currentPiece.setNextDropTime(TetriBattle.PIECE_FAST_DROP_TIMEOUT - currentPiece.getNextDropTime());
            }else{
                currentPiece.setNextDropTime(TetriBattle.PIECE_DROP_TIMEOUT - currentPiece.getNextDropTime());
            }
        }else{
            currentPiece.setMovementDone(true);
            gameInstance.attach(currentPiece);
        }


    }

}

@@ -922,32 +922,39 @@
 ece(gameInstance
+, false
 );%0A        %7D%0A%0A  
@@ -1668,32 +1668,75 @@
 FastDrop(true);%0A
+            dropPiece(gameInstance, true);%0A
             game
@@ -2984,32 +2984,48 @@
 nce gameInstance
+, boolean byUser
 ) %7B%0A%0A        Pie
@@ -3076,32 +3076,33 @@
 ce();%0A        if
+ 
 (currentPiece.tr
@@ -3127,16 +3127,17 @@
 stance))
+ 
 %7B%0A      
@@ -3144,16 +3144,17 @@
       if
+ 
 (current
@@ -3249,32 +3249,46 @@
 T_DROP_TIMEOUT -
+ (byUser ? 0 :
  currentPiece.ge
@@ -3295,32 +3295,33 @@
 tNextDropTime())
+)
 ;%0A            %7De
@@ -3311,36 +3311,38 @@
 );%0A            %7D
+ 
 else
+ 
 %7B%0A              
@@ -3404,16 +3404,30 @@
 IMEOUT -
+ (byUser ? 0 :
  current
@@ -3450,16 +3450,17 @@
 pTime())
+)
 ;%0A      
@@ -3476,20 +3476,22 @@
        %7D
+ 
 else
+ 
 %7B%0A      
@@ -3589,19 +3589,16 @@
       %7D%0A
-%0A%0A
     %7D%0A
-%0A
 %7D%0A

drop by user