/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ge.edu.freeuni.sdp.arkanoid.view.terminal;

import ge.edu.freeuni.sdp.arkanoid.view.GameOverView;
import com.googlecode.lanterna.input.Key;
import com.googlecode.lanterna.terminal.Terminal;
import ge.edu.freeuni.sdp.arkanoid.model.geometry.Size;
import ge.edu.freeuni.sdp.arkanoid.presenter.GameOverPresenter;

/**
 *
 * @author dato
 */
public class TerminalGameOverView extends GameOverView {

    private Terminal _terminal;
    private GameOverPresenter _presenter;
    private Size _roomSize;
    
    public TerminalGameOverView(GameOverPresenter presenter, Terminal terminal) {
        super(presenter);
        _terminal = terminal;
        _presenter = presenter;
        _roomSize = presenter.getRoomSize();
    }

    @Override
    protected void show() {
        draw();
        
        while (true){
            Key input = _terminal.readInput();
            if (input == null) continue;
            if (input.getKind() == Key.Kind.Enter){
                _presenter.setGameActivity(true);
                break;
            }
            if (input.getKind() == Key.Kind.Escape) {
                _presenter.setGameActivity(false);
                break;
            }
        }
    }
    
    private void draw(){
        _terminal.clearScreen();
        Size roomSize = _presenter.getRoomSize();
        
        String[] texts = new String[GAME_OVER_TEXTS_COUNT];
        fillTexts(texts);
        
        for (int i = 0; i < texts.length; i++) {
            String currentText = texts[i];
            int column = (_roomSize.getWidth() - currentText.length()) / 2;
            int row = _roomSize.getHeight()/2 - (texts.length/2 - i);
            
            writeLine(currentText, column, row);
        }
        
        _terminal.flush();
    }
    
    private void fillTexts(String[] texts) {
        texts[0] = GAME_OVER_MESSAGE;
        texts[1] = GAME_OVER_QUESTION;
        texts[2] = GAME_CONTINUE;
        texts[3] = GAME_QUIT;
    }
    
    private void writeLine(String text, int col, int row){
        _terminal.moveCursor(col, row);
        writeText(text);
    }
    
    private void writeText(String line){
        char[] chars = line.toCharArray();
        for (int i = 0; i < chars.length; i++){
            _terminal.putCharacter(chars[i]);
        }
    }
    
    private static final String GAME_OVER_MESSAGE =  "Game Over";
    private static final String GAME_OVER_QUESTION = "Do you want to play again?";
    private static final String GAME_CONTINUE =      "Press Enter to play again.";
    private static final String GAME_QUIT =          "Press ESC to quit.        ";
    
    private static final int GAME_OVER_TEXTS_COUNT = 4;

}

@@ -594,16 +594,22 @@
 private 
+final 
 Terminal
@@ -632,16 +632,22 @@
 private 
+final 
 GameOver
@@ -680,16 +680,22 @@
 private 
+final 
 Size _ro
@@ -1135,32 +1135,26 @@
 .get
-Kind() == Key.Kind.Enter
+Character() == 'y'
 )%7B%0A 
@@ -1289,16 +1289,63 @@
 d.Escape
+ %7C%7C%0A                input.getCharacter() == 'n'
 ) %7B%0A    
@@ -2762,13 +2762,9 @@
 ess 
-Enter
+Y
  to 
@@ -2774,16 +2774,20 @@
 y again.
+    
 %22;%0A    p
@@ -2841,16 +2841,21 @@
  %22Press 
+N or 
 ESC to q
@@ -2858,21 +2858,16 @@
 to quit.
-     
    %22;%0A  

correct keyboard inputs