package com.teinvdlugt.android.brainfuckinterpreter;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

public class CellsAdapter extends RecyclerView.Adapter<CellsAdapter.ViewHolder> {
    private Activity context;
    private byte[] data;
    private int pointedCellPosition = 0;
    private Cell pointedCell;
    private LinearLayoutManager layoutManager;
    private Runnable notifyItemChangedRunnable = new Runnable() {
        @Override
        public void run() {
            notifyItemChanged(pointedCellPosition);
        }
    };

    public CellsAdapter(Activity context, LinearLayoutManager layoutManager) {
        this.context = context;
        this.layoutManager = layoutManager;
        this.data = new byte[CellsLayout.MAX_CELL_AMOUNT];
    }

    /*public byte[] getData() {
        return data;
    }*/

    public void incrementPointedCellValue() {
        data[pointedCellPosition]++;
        context.runOnUiThread(notifyItemChangedRunnable);
    }

    public void decrementPointedCellValue() {
        data[pointedCellPosition]--;
        context.runOnUiThread(notifyItemChangedRunnable);
    }

    public void setPointedCellValue(byte value) {
        data[pointedCellPosition] = value;
        context.runOnUiThread(notifyItemChangedRunnable);
    }

    public byte getPointedCellValue() {
        return data[pointedCellPosition];
    }

    public void clearMemory() {
        this.data = new byte[CellsLayout.MAX_CELL_AMOUNT];
        pointedCellPosition = 0;
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                layoutManager.scrollToPosition(0);
                notifyDataSetChanged();
            }
        });
    }

    public void movePointer(int position) {
        this.pointedCellPosition = position;

        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (pointedCell != null) pointedCell.setPointed(false);
                // TODO Try: if (layoutManager.findFirstVisibleItemPosition() <= position && layoutManager.findLastVisibleItemPosition() >= position) {
                notifyItemChanged(pointedCellPosition);
                // }
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(new Cell(context));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.cell.setText(Byte.toString(data[position]));
        if (position == pointedCellPosition) {
            holder.cell.setPointed(true);
            pointedCell = holder.cell;
        } else {
            holder.cell.setPointed(false);
        }
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        Cell cell;

        public ViewHolder(View itemView) {
            super(itemView);
            cell = (Cell) itemView;
        }
    }

}

@@ -901,125 +901,132 @@
     
-/*
 public 
-byte%5B%5D getData() %7B%0A        return data;%0A    %7D*/%0A%0A    public void incrementPointedCellValue() %7B%0A        data%5B
+void incrementPointedCellValue() %7B%0A        data%5BpointedCellPosition%5D++;%0A        final int pointedCellPositionFinal = 
 poin
@@ -1032,35 +1032,32 @@
 ntedCellPosition
-%5D++
 ;%0A        contex
@@ -1077,338 +1077,860 @@
 ad(n
-otifyItemChangedRunnable);%0A    %7D%0A%0A    public void decrementPointedCellValue() %7B%0A        data%5BpointedCellPosition%5D--;%0A        context.runOnUiThread(notifyItemChangedRunnable);%0A    %7D%0A%0A    public void setPointedCellValue(byte value) %7B%0A        data%5BpointedCellPosition%5D = value;%0A        context.runOnUiThread(notifyItemChangedRunnable
+ew Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                notifyItemChanged(pointedCellPositionFinal);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void decrementPointedCellValue() %7B%0A        data%5BpointedCellPosition%5D--;%0A        final int pointedCellPositionFinal = pointedCellPosition;%0A        context.runOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                notifyItemChanged(pointedCellPositionFinal);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void setPointedCellValue(byte value) %7B%0A        data%5BpointedCellPosition%5D = value;%0A        final int pointedCellPositionFinal = pointedCellPosition;%0A        context.runOnUiThread(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                notifyItemChanged(pointedCellPositionFinal);%0A            %7D%0A        %7D
 );%0A 

Fix bug in CellsAdapter