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;


    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;
                int touchedCellIndex = xGridPosition + yGridPosition * getColumnCount();
                lifeCell = (LifeCellView) getChildAt(touchedCellIndex);
                lifeCell.makeCellViewLive();
                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;
    }
}

@@ -2630,32 +2630,244 @@
  cellPixelSize;%0A
+                //check that calculated positions are within range of grid%0A                if (0 %3C= xGridPosition && xGridPosition %3C getColumnCount() && 0 %3C= yGridPosition && yGridPosition %3C getRowCount()) %7B%0A    
                 
@@ -2947,32 +2947,36 @@
                 
+    
 lifeCell = (Life
@@ -3023,32 +3023,36 @@
                 
+ 
+   
 lifeCell.makeCel
@@ -3056,32 +3056,50 @@
 CellViewLive();%0A
+                %7D%0A
                 

add range check to touch listener to ensure expected coordinates are within size of grid