package io.github.lewismcgeary.androidgameoflife;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.FrameLayout;

public class MainActivity extends AppCompatActivity {
    GridPresenter worldGridPresenter;
    LifeGridLayout worldGridLayout;
    FloatingActionButton startResetFab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        worldGridLayout = (LifeGridLayout)findViewById(R.id.life_grid_layout);
        worldGridLayout.post(new Runnable() {
            @Override
            public void run() {
                setUpGrid();
            }
        });
        startResetFab = (FloatingActionButton)findViewById(R.id.start_reset_fab);
        final String startButtonText = getString(R.string.start_button_text);
        final String resetButtonText = getString(R.string.reset_button_text);
        final Drawable playIcon = getDrawable(R.drawable.ic_play_arrow_24dp);
        final Drawable resetIcon = getDrawable(R.drawable.ic_replay_24dp);
        startResetFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startResetFab.getTag().equals(startButtonText)) {
                    startResetFab.setTag(resetButtonText);
                    startResetFab.setImageDrawable(resetIcon);
                    worldGridPresenter.passLiveCellsToModelAndStartGame();
                } else {
                    startResetFab.setTag(startButtonText);
                    startResetFab.setImageDrawable(playIcon);
                    worldGridPresenter.resetGrid();
                }
            }
        });
    }
    private void setUpGrid() {
        int densityOfScreen = DisplayMetrics.DENSITY_DEFAULT;
        int cellSize = (int) getResources().getDimension(R.dimen.cell_size);
        int moveDuration = getResources().getInteger(R.integer.move_duration);
        //get the width and height of grid in dp
        int gridWidth = worldGridLayout.getMeasuredWidth() / (densityOfScreen / 160);
        int gridHeight = worldGridLayout.getMeasuredHeight() / (densityOfScreen / 160);
        //calculate number of columns and rows that will fit on screen
        int numberOfColumns = gridWidth/cellSize;
        int numberOfRows = gridHeight/cellSize;
        //calculate the margins needed to centre the cells in the layout
        float unusedWidthDp = gridWidth - numberOfColumns * cellSize;
        float unusedHeightDp = gridHeight - numberOfRows * cellSize;
        float unusedWidthPx = unusedWidthDp * (densityOfScreen / 160);
        float unusedHeightPx = unusedHeightDp * (densityOfScreen / 160);
        int leftRightMargin = (int) unusedWidthPx / 2;
        int topBottomMargin = (int) unusedHeightPx / 2;
        //apply the calculated values
        FrameLayout.LayoutParams worldGridLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        worldGridLayoutParams.setMargins(leftRightMargin, topBottomMargin, leftRightMargin, topBottomMargin);
        worldGridLayout.setLayoutParams(worldGridLayoutParams);
        worldGridLayout.setColumnCount(numberOfColumns);
        worldGridLayout.setRowCount(numberOfRows);
        worldGridPresenter = new GridPresenter(worldGridLayout, moveDuration);
        worldGridPresenter.setInitialState();
    }

    public void gameOver(){
        Snackbar.make(startResetFab, "They're dead, Jim", Snackbar.LENGTH_LONG).show();
    }
}

@@ -534,16 +534,120 @@
 setFab;%0A
+    String startButtonText;%0A    String resetButtonText;%0A    Drawable playIcon;%0A    Drawable resetIcon;%0A%0A
     @Ove
@@ -1125,29 +1125,16 @@
         
-final String 
 startBut
@@ -1194,21 +1194,8 @@
     
-final String 
 rese
@@ -1255,31 +1255,16 @@
         
-final Drawable 
 playIcon
@@ -1322,23 +1322,8 @@
     
-final Drawable 
 rese
@@ -1599,106 +1599,29 @@
    s
-tartResetFab.setTag(resetButtonText);%0A                    startResetFab.setImageDrawable(resetIcon
+howButtonInResetMode(
 );%0A 
@@ -1744,105 +1744,29 @@
    s
-tartResetFab.setTag(startButtonText);%0A                    startResetFab.setImageDrawable(playIcon
+howButtonInStartMode(
 );%0A 
@@ -3591,16 +3591,309 @@
 %0A    %7D%0A%0A
+    private void showButtonInStartMode()%7B%0A        startResetFab.setTag(startButtonText);%0A        startResetFab.setImageDrawable(playIcon);%0A    %7D%0A%0A    private void showButtonInResetMode()%7B%0A        startResetFab.setTag(resetButtonText);%0A        startResetFab.setImageDrawable(resetIcon);%0A    %7D%0A%0A
     publ
@@ -3996,20 +3996,53 @@
 NG).show();%0A
+        showButtonInStartMode();%0A
     %7D%0A%7D%0A

make button and grid reset when all cells are dead and Snackbar appears