package io.github.lewismcgeary.androidgameoflife;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lewis on 17/11/15.
 */
public class LifeGridLayout extends GridLayout {
    Context context;
    AttributeSet attrs;
    LifeCellView lifeCell;
    float leftOrigin;
    float topOrigin;
    int cellPixelSize;
    boolean bringingCellsToLife;


    public LifeGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.attrs = attrs;
        this.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
    }




    public void initialiseLifeGridLayout(){
        OnTouchListener cellTouchListener = new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LifeCellView touchedCell = (LifeCellView)v;
                //cells can be toggled between living or dead by user at start of game
                if(touchedCell.getState()) {
                    touchedCell.makeCellViewDead();
                } else {
                    touchedCell.makeCellViewLive();
                }
                return false;
            }
        };
        for (int x=0; x<getColumnCount(); x++){
            for(int y=0; y<getRowCount(); y++){
                lifeCell = new LifeCellView(context, attrs);
                //lifeCell.setOnTouchListener(cellTouchListener);
                this.addView(lifeCell);
            }
        }
        //get position and size on screen of first cell
        //based on this can calculate grid coordinates from on screen pixel coordinates

        this.post(new Runnable() {
            @Override
            public void run() {
                lifeCell = (LifeCellView) getChildAt(0);
                leftOrigin = lifeCell.getX();
                topOrigin = lifeCell.getY();
                cellPixelSize = lifeCell.getWidth();
            }
        });
        this.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //use x and y from event to calculate which cell is touched
                //subtract left or top origin to get baseline position from origin
                //divide by cellPixelSize to get x and y gridlayout coordinates
                int xGridPosition = (int) (event.getX() - leftOrigin) / cellPixelSize;
                int yGridPosition = (int) (event.getY() - topOrigin) / cellPixelSize;
                //check that calculated positions are within range of grid
                if (0 <= xGridPosition && xGridPosition < getColumnCount() && 0 <= yGridPosition && yGridPosition < getRowCount()) {
                    int touchedCellIndex = xGridPosition + yGridPosition * getColumnCount();
                    lifeCell = (LifeCellView) getChildAt(touchedCellIndex);
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            if (lifeCell.getState()) {
                                lifeCell.makeCellViewDead();
                                bringingCellsToLife = false;
                            } else {
                                lifeCell.makeCellViewLive();
                                bringingCellsToLife = true;
                            }
                        case MotionEvent.ACTION_MOVE:
                            if (bringingCellsToLife) {
                                lifeCell.makeCellViewLive();
                            } else {
                                lifeCell.makeCellViewDead();
                            }
                    }
                }
                return true;
            }
        });
    }

    public List<GridCoordinates> getUserSetLiveCellCoordinates() {
        List<GridCoordinates> userSelectedLiveCells = new ArrayList<>();
        for (int i=0; i < getChildCount(); i++) {
            lifeCell = (LifeCellView)getChildAt(i);
            //disable clicking of cells during game
            lifeCell.setEnabled(false);
            if(lifeCell.getState()){
                int x = i % getColumnCount();
                int y = i/getColumnCount();
                userSelectedLiveCells.add(new GridCoordinates(x, y));

            }
        }
        return userSelectedLiveCells;
    }

    public void setNewLiveCells(List<GridCoordinates> newLiveCells) {
        List<Integer> indexOfLiveCells = new ArrayList<>();
        for (int j=0; j<newLiveCells.size(); j++) {
            indexOfLiveCells.add(newLiveCells.get(j).getX()+(getColumnCount()*newLiveCells.get(j).getY()));
        }
        for (int i=0; i < getChildCount(); i++) {
            if (indexOfLiveCells.contains(i)) {
                lifeCell = (LifeCellView)getChildAt(i);
                lifeCell.makeCellViewLive();
            } else {
                lifeCell = (LifeCellView)getChildAt(i);
                lifeCell.makeCellViewDead();
            }
        }
    }

    public void killAllCells(){
        for(int i=0; i<getChildCount(); i++) {
            lifeCell = (LifeCellView)getChildAt(i);
            //re-enable clicking of cells for next game
            lifeCell.makeCellViewDead();
            lifeCell.setEnabled(true);
        }
    }

    public void noCellsWereSelected(){
        if(context instanceof LifeGameActivity){
            LifeGameActivity activity = (LifeGameActivity)context;
            activity.showMessageThatNoCellsWereSelected();
        }
        reEnableCellClicking();
    }

    public void reEnableCellClicking(){
        for(int i=0; i<getChildCount(); i++) {
            lifeCell = (LifeCellView)getChildAt(i);
            lifeCell.setEnabled(true);
        }
    }

    public void cellsDiedGameOver(){
        if(context instanceof LifeGameActivity){
            LifeGameActivity activity = (LifeGameActivity)context;
            activity.gameOver();
        }
    }
    //can disable this as no scrolling needed
    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }
}

@@ -825,556 +825,8 @@
 ()%7B%0A
-        OnTouchListener cellTouchListener = new OnTouchListener() %7B%0A            @Override%0A            public boolean onTouch(View v, MotionEvent event) %7B%0A                LifeCellView touchedCell = (LifeCellView)v;%0A                //cells can be toggled between living or dead by user at start of game%0A                if(touchedCell.getState()) %7B%0A                    touchedCell.makeCellViewDead();%0A                %7D else %7B%0A                    touchedCell.makeCellViewLive();%0A                %7D%0A                return false;%0A            %7D%0A        %7D;%0A
     
@@ -982,74 +982,8 @@
 s);%0A
-                //lifeCell.setOnTouchListener(cellTouchListener);%0A
     
@@ -1042,24 +1042,25 @@
       %7D%0A
+%0A
         
 //get po
@@ -1055,233 +1055,185 @@
     
-//get position and size on screen of first cell%0A        //based on this can calculate grid coordinates from on screen pixel coordinates%0A%0A        this.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B
+this.post(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                //after layout is drawn, use first cell as an anchor to work out any coordinates
 %0A   
@@ -1620,228 +1620,74 @@
   //
-use x and y from event to calculate which cell is touched%0A                //subtract left or top origin to get baseline position from origin%0A                //divide by cellPixelSize to get x and y gridlayout coordinates
+calculate the grid position based on finger location on the screen
 %0A   
@@ -2005,16 +2005,40 @@
 nCount()
+%0A                       
  && 0 %3C=
@@ -2084,24 +2084,25 @@
 wCount()) %7B%0A
+%0A
             
@@ -3372,100 +3372,8 @@
 i);%0A
-            //disable clicking of cells during game%0A            lifeCell.setEnabled(false);%0A
     
@@ -3565,17 +3565,16 @@
 x, y));%0A
-%0A
         
@@ -4421,64 +4421,8 @@
 i);%0A
-            //re-enable clicking of cells for next game%0A
     
@@ -4462,47 +4462,8 @@
 ();%0A
-            lifeCell.setEnabled(true);%0A
     
@@ -4703,235 +4703,8 @@
   %7D%0A
-        reEnableCellClicking();%0A    %7D%0A%0A    public void reEnableCellClicking()%7B%0A        for(int i=0; i%3CgetChildCount(); i++) %7B%0A            lifeCell = (LifeCellView)getChildAt(i);%0A            lifeCell.setEnabled(true);%0A        %7D%0A
     

remove obsolete code for individual cell touch listeners