package simulator;

import java.util.Arrays;

class Positions {
    final int[] chi = new int[Tunny.CHI_NUM];
    final int[] psi = new int[Tunny.PSI_NUM];
    final int[] mu = new int[2];

    private Positions(int[] chi, int[] psi, int[] mu) {
        if (chi != null) {
            System.arraycopy(chi, 0, this.chi, 0, Tunny.CHI_NUM);
        } else {
            Arrays.fill(this.chi, 0);
        }
        if (psi != null) {
            System.arraycopy(psi, 0, this.psi, 0, Tunny.PSI_NUM);
        } else {
            Arrays.fill(this.psi, 0);
        }
        if (mu != null) {
            System.arraycopy(mu, 0, this.mu, 0, Tunny.MU_NUM);
        } else {
            Arrays.fill(this.mu, 0);
        }
    }
    Positions(Positions positions) {
        this(positions.chi, positions.psi, positions.mu);
    }
    Positions() {
        this(null, null, null);
    }

    void randomize(){
        for (int w = 0; w < Tunny.CHI_NUM; w++) {
            chi[w] = Utils.random.nextInt(Tunny.CHI_PERIOD[w]);
        }
        for (int w = 0; w < Tunny.MU_NUM; w++) {
            mu[w] = Utils.random.nextInt(Tunny.MU_PERIOD[w]);
        }
        for (int w = 0; w < Tunny.PSI_NUM; w++) {
            psi[w] = Utils.random.nextInt(Tunny.PSI_PERIOD[w]);
        }
    }

    private static String toString(int[] positions){
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < positions.length; i++) {
            int p = positions[i];
            s.append(p);
            if (i != positions.length - 1) {
                s.append(":");
            }
        }
        return s.toString();
    }

    @Override
    public String toString(){
        return toString(chi) + "|" + toString(psi) + "|" + toString(mu);
    }

    public void parse(String s) {
        String[] parts = s.split("\\|");
        parse(parts[0], chi);
        parse(parts[1], psi);
        parse(parts[2], mu);
    }

    // Private
    private static void parse(String s, int[] positions) {
        String[] parts = s.split(":");
        for (int i = 0; i < parts.length; i++) {
            positions[i] = Integer.valueOf(parts[i]);
        }
    }

}
