package io.github.lewismcgeary.androidgameoflife;

import android.os.AsyncTask;
import android.os.Handler;

import java.util.List;

/**
 * Created by Lewis on 16/11/15.
 */
public class GridPresenter {
    Grid worldGrid;
    LifeGridLayout worldGridLayout;
    int moveDuration;
    private CalculateUpdateTask calculateUpdateTask;

    public GridPresenter(LifeGridLayout newWorldGridLayout, int moveDuration) {
        worldGridLayout = newWorldGridLayout;
        worldGrid = new Grid(worldGridLayout.getColumnCount(), worldGridLayout.getRowCount());
        this.moveDuration = moveDuration;

    }

    public Grid getWorldGrid() {
        return worldGrid;
    }

    public void setInitialState() {
        worldGridLayout.initialiseLifeGridLayout();
    }

    public void passLiveCellsToModelAndStartGame(){
        worldGrid.setInitialLiveCells(worldGridLayout.getUserSetLiveCellCoordinates());
        startConstantUpdate();
    }

    public List<GridCoordinates> getLiveCells(){
        return worldGrid.getLiveCells();
    }

    public void updateGrid(){
        new CalculateUpdateTask().execute();

    }

    public void resetGrid(){
        calculateUpdateTask.cancel(true);
        worldGrid.killAllCells();
        worldGridLayout.killAllCells();
    }

    public void startConstantUpdate() {
        calculateUpdateTask = new CalculateUpdateTask();
        calculateUpdateTask.execute();
    }

    private class CalculateUpdateTask extends AsyncTask<Void, List<GridCoordinates>, Void> {
        Handler asyncHandler = new Handler();
        final Runnable asyncRunnable = new Runnable() {
            @Override
            public void run() {
                worldGrid.calculateNextStateOfCells();
                worldGrid.switchCellsToNextState();
                if(isCancelled()){
                    asyncHandler.removeCallbacks(asyncRunnable);
                } else {
                    publishProgress(worldGrid.getLiveCells());
                }
                asyncHandler.postDelayed(asyncRunnable, moveDuration);
            }
        };

        @Override
        protected void onCancelled() {
            super.onCancelled();
            asyncHandler.removeCallbacks(asyncRunnable);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {

            asyncHandler.postDelayed(asyncRunnable, 0);
            return null;

        }

        @Override
        protected void onProgressUpdate(List<GridCoordinates>... newListOfLiveCells) {
            super.onProgressUpdate(newListOfLiveCells);
            worldGridLayout.setNewLiveCells(newListOfLiveCells[0]);
        }

    }
}

@@ -1054,20 +1054,30 @@
 oid 
-u
+startConstantU
 pdate
-Grid
 ()
+ 
 %7B%0A  
@@ -1081,16 +1081,38 @@
 %0A       
+ calculateUpdateTask =
  new Cal
@@ -1129,16 +1129,45 @@
 teTask()
+;%0A        calculateUpdateTask
 .execute
@@ -1170,17 +1170,16 @@
 cute();%0A
-%0A
     %7D%0A%0A 
@@ -1333,151 +1333,8 @@
  %7D%0A%0A
-    public void startConstantUpdate() %7B%0A        calculateUpdateTask = new CalculateUpdateTask();%0A        calculateUpdateTask.execute();%0A    %7D%0A%0A
     

remove unused update method