package org.rubyspa.droidgol;

import android.app.Activity;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import static org.rubyspa.droidgol.GamePrinter.print;

public class GameActivity extends Activity implements View.OnClickListener {

    private TextView boardView;
    private Pair<Integer, Integer> boardDimensions;
    private volatile boolean gameRunning = false;
    private Game game;
    private Thread gameThread;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initialize();
        gameThread = new Thread() {
            @Override
            public void run() {
                try {
                    while (gameRunning && !isInterrupted()) {
                        Thread.sleep(150, 0);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Game nextGameState = game.evolve();
                                boardView.setText(print(nextGameState));
                            }
                        });
                    }
                } catch (InterruptedException e) {
                }
            }
        };
    }

    private void initialize() {
        this.boardView = (TextView) findViewById(R.id.gameBoard);
        this.boardDimensions = computeDimensions();
        this.game = new Game(this.boardDimensions.first, this.boardDimensions.second);
        this.boardView.setText(String.format(
                "\n\n\n\n\n\n                " +
                        "\n     Game will extend over" +
                        "\n     %d columns and" +
                        "\n     %d rows" +
                        "\n     (touch to start)", boardDimensions.first, boardDimensions.second));
    }

    private Pair<Integer, Integer> computeDimensions() {
        // TODO: How do you reliably find out how many 'o's per line and how many lines we can fit on the screen?
        Integer width = 38;
        Integer height = 28;
        return Pair.create(width, height);
    }


    @Override
    public void onClick(View v) {
        if (!gameRunning) gameThread.start();
        this.gameRunning = true;
        this.boardView.setEnabled(false);
    }

}

@@ -53,16 +53,56 @@
 tivity;%0A
+import android.content.pm.ActivityInfo;%0A
 import a
@@ -743,16 +743,91 @@
 .main);%0A
+        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);%0A
         

disable landscape mode