package cnp.ew.grid;

import java.awt.*;
import java.util.*;
import java.io.*;

public class CpSparseGrid implements CpGridModel
{
    Vector cells;
    public Dimension sizeInCells;

    public CpSparseGrid(Dimension newSizeInCells)
    {
        cells = new Vector();
        sizeInCells = newSizeInCells;
    }

    public Dimension getSizeInCells()
    {
        return sizeInCells;
    }

    public CpGridCell getCell(Point cellRC)
    {
        return getCell(cellRC.x, cellRC.y);
    }

    public CpGridCell getCell(int col, int row)
    {
        Vector r;
        if ((r = row(row)) == null) {
            r = addRow(row);
        }
        CpGridCell cell;
        if ((cell = cell(r, col)) == null) {
            cell = addCell(r, col);
        }
        return cell;
    }

    // Added by ken - temporary
    void dumpCells()
    {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j ++) {
                System.out.println("row " + j + ", col: " + i +
                    getCell(new Point(i, j)));
            }
        }
    }



    public void putCell(Point cellRC, CpGridCell cell)
    {
        Vector r;
        if ((r = row(cellRC.y)) == null) {
            r = addRow(cellRC.y);
        }
        addCell(r, cellRC.x, cell);
    }

    public Vector row(int i)
    {
        if (i < cells.size()) {
            return (Vector)cells.elementAt(i);
        } else {
            return null;
        }
    }

    public int nonNullColumnsInRow(int i)
    {
        return row(i).size();
    }

    Vector addRow(int index)
    {
        Vector v = null;

        int numRows = cells.size();
        for (int i = numRows; i <= index; i++) {
            v = new Vector();
            cells.addElement(v);
        }
        return v;
    }

    CpGridCell cell(Vector row, int i)
    {
        if (i < row.size()) {
            return (CpGridCell)row.elementAt(i);
        } else {
            return null;
        }
    }

    CpGridCell addCell(Vector row, int index)
    {
        CpGridCell cell = null;

        int numColumns = row.size();
        for (int i = numColumns; i <= index; i++) {
            cell = new CpGridCell();
            row.addElement(cell);
        }
        return cell;
    }

    CpGridCell addCell(Vector row, int index, CpGridCell cell)
    {
        int numColumns = row.size();
        for (int i = numColumns; i <= index; i++) {
            row.addElement(cell);
        }
        return cell;
    }

    /************************** FILE SAVE/OPEN *****************************/

    public CpSparseGrid(RandomAccessFile file) throws IOException
    {
        cells = new Vector();
        sizeInCells = new Dimension(file.readInt(), file.readInt());
        //System.out.println("size of cells = " + sizeInCells);

        int row;
        while((row = file.readInt()) != -1) {
            int columns = file.readInt();
            for (int c = 0; c < columns; c++) {
                putCell(new Point(c, row), new CpGridCell(file, this));
            }
        }
    }

    public void writeTo(RandomAccessFile file)
    {
      /*  try {
            //System.out.println("Saving GridModel");
            file.writeInt(sizeInCells.width);
            file.writeInt(sizeInCells.height);
            Vector row;
            for (int r = 0; r < 10 sizeInCells.height  ; r++) {
    	        CpGridCell cell;
    	        //System.out.print("r = " + r);
                for (int c = 0; c < nonNullColumnsInRow(r); c++) {
                    if (c == 0) {
                        file.writeInt(r);
                        file.writeInt(nonNullColumnsInRow(r));
                    }
    	            //System.out.print("c = " + c);
    	            cell = getCell(new Point(c, r));
    	            if (cell == null) { //System.out.println("cell is null"); }
    	            //System.out.println("Saving cell: " + c + ", " + r);
    	            cell.writeTo(file);
                }
            }
            file.writeInt(-1);
        } catch (Exception e) { // why do i need to do this?
                System.out.println("Got exception " + e);
                e.printStackTrace();
        }
        */
    }
}
