package pathgame.algorithm;

public class Permutation {
    private int size;
    private int[] vals;
    private int[] minVals;
    private int minCost = Integer.MAX_VALUE;
    private int thingsPerPos;
    private boolean overFlow = false;
    //public int tempCounter1 = 0;
    //public int tempCounter2 = 0;

    public Permutation(int listSize, int thingsPerPos) {
        this.thingsPerPos = thingsPerPos;
        this.size = (listSize/thingsPerPos) - 1;
        vals = new int[size];

    }

    public int getValAtPos (int pos) {
        return vals[pos];
    }

    public boolean isPosAtMax (int pos) {
        if(vals[pos] == getMaxOfPos(pos)) {
            return true;
        }
        return false;
    }

    public int getMaxOfPos (int pos) {
        return thingsPerPos * (size - pos);
    }

    public int size() {
        return size;
    }

    public void increaseAtPos (int pos) {
        vals[pos]++;
        for(int i = pos+1; i < size; i++) {
            vals[i] = 0;
        }
    }

    public void increaseLowest() {
        boolean over = true;

        for(int i = size-1; i >= 0; i--) {
            if(getValAtPos(i) < getMaxOfPos(i)) {
                increaseAtPos(i);
                over = false;
                break;
            }
        }

        if(over) {
            overFlow = true;
        }
    }

    public int getMinCost() {
        return minCost;
    }

    public void setMinCost(int minCost) {
        this.minCost = minCost;
        minVals = vals.clone();
    }

    public void makePermutMinimal() {
        vals = minVals.clone();
    }

    public void printPermut() {
        for(int i = 0; i < size; i++) {
            System.out.print(vals[i] + " ");
        }
        System.out.println();
    }

    public boolean isOverFlowing() {
        return overFlow;
    }

    public void printMinPermut() {
        for(int i = 0; i < size; i++) {
            System.out.print(minVals[i] + " ");
        }
        System.out.println();
    }
}
