package ge.edu.freeuni.sdp.arkanoid.view.terminal;


import com.googlecode.lanterna.terminal.Terminal;
import ge.edu.freeuni.sdp.arkanoid.presenter.*;
import ge.edu.freeuni.sdp.arkanoid.view.RoomView;

class TerminalRoomView extends RoomView implements CellUpdateListener, StatusUpdateListener {


    private final Terminal _terminal;

    TerminalRoomView(RoomPresenter presenter, Terminal terminal) {
        super(presenter);
        PressedKeyWatcher.init();
        _terminal = terminal;
        presenter.set_cellUpdateListener(this);
        presenter.setStatusUpdateListener(this);
    }

    public void show() {
        RoomPresenter presenter = getPresenter();
        presenter.init();

        _terminal.clearScreen();
        while (true) {
            Command command = Command.None;
            if (presenter.isGameOver()) break;

            if (PressedKeyWatcher.isRightPressed()) command = Command.Right;
            if (PressedKeyWatcher.isLeftPressed()) command = Command.Left;
            if (PressedKeyWatcher.isSpacePressed()) command = Command.Fire;
            if (PressedKeyWatcher.isPausedPressed()) {
                command = Command.Pause;
                presenter.execute(command);
                do{
                    sleep();
                }while (PressedKeyWatcher.isPausedPressed() && !PressedKeyWatcher.isEscapePressed());
            }
            if (PressedKeyWatcher.isEscapePressed()) break;
            presenter.execute(command);
            sleep();
        }
    }

    private void sleep() {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void updateCell(CellPosition position, CellContent content) {

        _terminal.moveCursor(position.x, position.y + 1);
        switch (content) {

            case None:
                _terminal.applyBackgroundColor(Terminal.Color.BLACK);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter(' ');
                break;
            case RedBrick:
                _terminal.applyBackgroundColor(Terminal.Color.BLACK);
                _terminal.applyForegroundColor(Terminal.Color.RED);
                _terminal.putCharacter('█');
                break;
            case BlueBrick:
                _terminal.applyBackgroundColor(Terminal.Color.BLACK);
                _terminal.applyForegroundColor(Terminal.Color.BLUE);
                _terminal.putCharacter('█');
                break;
            case Paddle:
                _terminal.applyBackgroundColor(Terminal.Color.BLACK);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter('▄');
                break;
            case Ball:
                _terminal.applyBackgroundColor(Terminal.Color.BLACK);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter('○');
                break;
            case ECapsule:
                _terminal.applyBackgroundColor(Terminal.Color.BLUE);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter('E');
                break;
            case BCapsule:
                _terminal.applyBackgroundColor(Terminal.Color.GREEN);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter('B');
                break;
            case ACapsule:
                _terminal.applyBackgroundColor(Terminal.Color.RED);
                _terminal.applyBackgroundColor(Terminal.Color.CYAN);
                _terminal.putCharacter('A');
                break;
            case PCapsule:
                _terminal.applyBackgroundColor(Terminal.Color.GREEN);
                _terminal.applyForegroundColor(Terminal.Color.WHITE);
                _terminal.putCharacter('P');
        }
    }

    public void updateScore(int score) {
        //TODO: Add implementation
    }

    public void updateLives(int lives) {
        _terminal.applyBackgroundColor(Terminal.Color.BLUE);
        _terminal.applyForegroundColor(Terminal.Color.WHITE);

        for (int i = 0; i < lives; i++) {
            _terminal.moveCursor(i, 0);
            _terminal.putCharacter('*');
        }
    }
}

@@ -3908,24 +3908,276 @@
 acter('P');%0A
+                break;%0A            case Beam:%0A                _terminal.applyBackgroundColor(Terminal.Color.BLACK);%0A                _terminal.applyForegroundColor(Terminal.Color.RED);%0A                _terminal.putCharacter('%7C');%0A                break;%0A
         %7D%0A  

Add Beam symbol with color