/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package fr.gouv.finances.cp.xemelios.ui.ia.core;

/**
 *
 * @author christophe.marchand
 */
public class StateModel {
    private State state;
    private LocationInformation[][] locations;
    
    public StateModel(State state) {
        super();
        this.state=state;
        initialize();
    }
    protected void initialize() {
        int size = getGridWidth();
        locations = new LocationInformation[size][size];
        Template t = state.getTemplate();
        String[][] array = new String[t.maxValue][t.maxValue];
        for(int index=0; index<t.maxIndex; index++) {
            int row = t.indexToRow(index);
            int col = t.indexToColumn(index);
//            if(row<)
            if(!state.isFree(index)) {
                locations[row][col] = new LocationInformation(state.getValue(index)+1);
                //Integer.toString(st.getValue(index)+1);
            } else {
                locations[row][col] = new LocationInformation(-1);
            }
        }
    }
    public boolean isWritable(int row, int col) { return !locations[row][col].isOriginal(); }
//    public boolean isFree(int row, int col) { return state.isFree(locations[row][col].getIndex()); }
    public boolean isValueAllowed(int row, int col, String value) {
        try {
//            return state.isAllowed(locations[row][col].getIndex(), Integer.valueOf(value));
            int v = Integer.parseInt(value);
            if (0 < v && v < 10) {
                // check if row or col already contains value
                for(int i=0; i<9; i++) {
                    if(i!=row && value.equals(locations[i][col].getValue())) return false;
                    if(i!=col && value.equals(locations[row][i].getValue())) return false;
                }
                int sqStartRow = (row / 3)*3;
                int sqStartCol = (col / 3)*3;
                for(int i=sqStartRow;i<sqStartRow+3;i++) {
                    for(int j=sqStartCol;j<sqStartCol+3;j++) {
                        if(i!=row && j!= col && value.equals(locations[i][j].getValue())) return false;
                    }
                }
                return true;
            } else return false;
        } catch(Exception ex) {
            return false;
        }
    }
    public void setValueAt(int row, int col, String value) {
        locations[row][col].setValue(value);
    }
    public String getValueAt(int row, int col) { return locations[row][col].getValue(); }
    public int getGridWidth() { return state.getTemplate().maxValue; }
    public int getGridHeight() { return getGridWidth(); }
    
    private class LocationInformation {
//        private int stateIndex;
        private boolean original = false;
        private int value = -1;
        public LocationInformation(final int value) {
            super();
            this.value=value;
            original = (value!= -1);
        }
        public boolean isOriginal() { return original; }
        public boolean isWrittable() { return !isOriginal(); }
        public String getValue() { if(value!= -1) return Integer.toString(value); return null; }
        public void setValue(String v) {
            try {
                value = Integer.parseInt(v);
            } catch(Exception ex) {
                value = -1;
            }
        }
    }
}
