package hu.supercluster.gameoflife.app.activity.main;

import android.os.Bundle;

import com.squareup.otto.Subscribe;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import hu.supercluster.gameoflife.app.util.DisplayHelper;
import hu.supercluster.gameoflife.game.cell.Cell;
import hu.supercluster.gameoflife.game.cellularautomaton.Fill;
import hu.supercluster.gameoflife.game.cellularautomaton.GameOfLifeFactory;
import hu.supercluster.gameoflife.game.event.Pause;
import hu.supercluster.gameoflife.game.event.Reset;
import hu.supercluster.gameoflife.game.event.Restart;
import hu.supercluster.gameoflife.game.event.Resume;
import hu.supercluster.gameoflife.game.manager.GameManager;
import hu.supercluster.gameoflife.game.manager.GameParams;
import hu.supercluster.gameoflife.game.manager.GameParamsBuilder;
import hu.supercluster.gameoflife.game.visualization.cell.SimpleCellColors;
import hu.supercluster.gameoflife.game.rule.NeighborCountBasedRule;
import hu.supercluster.gameoflife.app.view.ChangeRulesDialogFragment;
import hu.supercluster.gameoflife.util.EventBus;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

@EBean
public class MainPresenter {
    GameManager gameManager;

    @RootContext
    MainActivity activity;

    @Bean
    DisplayHelper displayHelper;

    void onActivityResume() {
        EventBus.getInstance().register(this);
    }

    void onActivityPause() {
        EventBus.getInstance().unregister(this);
    }

    public Bundle saveGameState() {
        return gameManager.saveGameState();
    }

    void startGame() {
        GameParams gameParams = GameParamsBuilder.create()
                .setScreenOrientation(displayHelper.getScreenOrientation())
                .setDisplaySize(displayHelper.getDisplaySize())
                .setCellSizeInPixels(6)
                .setFill(new Fill(0.10f, Cell.STATE_ALIVE))
                .setCellColors(new SimpleCellColors())
                .setFps(15)
                .startPaused(activity.paused)
                .build()
        ;

        gameManager = new GameManager(
                activity.gameState,
                activity.automatonView,
                new GameOfLifeFactory(),
                gameParams
        );

        gameManager.createGame();

        if (activity.paused) {
            onPause();
        }
    }

    void onPause() {
        EventBus.getInstance().post(new Pause());
        setUiPausedState(true);
    }

    void onResume() {
        EventBus.getInstance().post(new Resume());
        setUiPausedState(false);
    }

    private void setUiPausedState(boolean paused) {
        activity.resume.setVisibility(paused ? VISIBLE : GONE);
        activity.pause.setVisibility(paused ? GONE : VISIBLE);
        activity.paused = paused;
    }

    void onResetGame() {
        onPause();
        EventBus.getInstance().post(new Reset());
    }

    void onRestartGame() {
        EventBus.getInstance().post(new Restart());
    }

    void onChangeRules() {
        ChangeRulesDialogFragment dialogFragment = new ChangeRulesDialogFragment();
        dialogFragment.setRule((NeighborCountBasedRule) gameManager.getAutomaton().getRule());
        dialogFragment.show(activity.getFragmentManager(), "rules");
    }

    @Subscribe
    public void onRulesChanged(NeighborCountBasedRule rule) {
        gameManager.getAutomaton().setRule(rule);
    }
}

@@ -48,16 +48,47 @@
 .main;%0A%0A
+import android.graphics.Point;%0A
 import a
@@ -350,24 +350,94 @@
 playHelper;%0A
+import hu.supercluster.gameoflife.app.view.ChangeRulesDialogFragment;%0A
 import hu.su
@@ -1013,84 +1013,8 @@
 er;%0A
-import hu.supercluster.gameoflife.game.visualization.cell.SimpleCellColors;%0A
 impo
@@ -1115,42 +1115,48 @@
 ife.
-app.view.ChangeRulesDialogFragment
+game.visualization.cell.SimpleCellColors
 ;%0Aim
@@ -1709,32 +1709,149 @@
 d startGame() %7B%0A
+        Point displaySize = displayHelper.getDisplaySize();%0A        int cellSize = getOptimalCellSize(displaySize);%0A%0A
         GamePara
@@ -2008,39 +2008,20 @@
 (display
-Helper.getDisplay
 Size
-()
 )%0A      
@@ -2055,9 +2055,16 @@
 els(
-6
+cellSize
 )%0A  
@@ -2579,32 +2579,264 @@
        %7D%0A    %7D%0A%0A
+    private int getOptimalCellSize(Point displaySize) %7B%0A        int cellSize = 1;%0A        while (displaySize.x / cellSize %3E 160 %7C%7C displaySize.y / cellSize %3E 200) %7B%0A            cellSize++;%0A        %7D%0A%0A        return cellSize;%0A    %7D%0A%0A
     void onPause

Determine optimum cell size