package com.bernard.mcqinator.api.fragenStats;

import com.bernard.mcqinator.api.quiz.FormQuiz;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author samy
 * @date on 26/05/17.
 */

public class FormQuizStat extends QuizStat<FormQuiz> {
    /* TODO pass to formquiz */
    protected volatile int failSum;
    protected volatile int[] failSums;
    protected volatile int[][] failsSum;
    protected volatile int[][][] failsSums;
    protected volatile int[][][][] fails;


    @Override
    public void genDefault(FormQuiz q) {
        FormQuiz.Item[] items = q.getItems();
        fails = new int[items.length][][][];
        for (int i = 0; i < items.length; i++) {
            fails[i] = new int[items[i].getForms().length][][];
            for (int j = 0; j < items[i].getForms().length; j++) {
                fails[i][j] = new int[items[i].getForms().length - 1][];
                for (int k = 0; k < items[i].getForms().length - 1; k++) {
                    int[] current = new int[items[i].getForms()[j].getFalshe().length];
                    Arrays.fill(current,1);
                    fails[i][j][k] = current;
                }
            }
        }
        genVolatiles();
    }

    @Override
    public void read(DataInputStream dis) throws IOException {
        fails = new int[dis.readInt()][][][];
        for (int i = 0; i < fails.length; i++) {
            fails[i] = new int[dis.readInt()][][];
            for (int j = 0; j < fails[i].length; j++) {
                fails[i][j] = new int[dis.readInt()][];
                for (int k = 0; k < fails[i][j].length; k++) {
                    fails[i][j][k] = new int[dis.readInt()];
                    for (int l = 0; l < fails[i][j][k].length; l++)
                        fails[i][j][k][l] = dis.readInt();
                }
            }
        }
        genVolatiles();
    }

    @Override
    public void write(DataOutputStream dos) throws IOException {
        dos.writeInt(fails.length);
        for (int[][][] fails1:fails) {
            dos.writeInt(fails1.length);
            for (int[][] fails2:fails1) {
                dos.writeInt(fails2.length);
                for (int[] fails3:fails2) {
                    dos.writeInt(fails3.length);
                    for (int fails4 : fails3)
                        dos.writeInt(fails4);
                }
            }
        }
    }

    public void genVolatiles(){
        failSum = 0;
        failSums = new int[fails.length];
        failsSum = new int[fails.length][];
        failsSums = new int[fails.length][][];
        for (int i = 0; i < fails.length; i++) {
            failSums[i] = 0;
            failsSum[i] = new int[fails[i].length];
            failsSums[i] = new int[fails[i].length][];
            for (int j = 0; j < fails[i].length; j++) {
                failsSum[i][j] = 0;
                failsSums[i][j] = new int[fails[i][j].length];
                for (int k = 0; k < fails[i][j].length; k++) {
                    failsSums[i][j][k] = 0;
                    for (int l = 0; l < fails[i][j][k].length; l++)
                        failsSums[i][j][k] += fails[i][j][k][l];
                    failsSum[i][j] += failsSums[i][j][k];
                }
                failSums[i] += failsSum[i][j];
            }
            failSum += failSums[i];
        }
    }


    public int failSum(){
        return failSum;
    }

    public int failSum(int item){
        return failSums[item];
    }

    public int failSum(int item,int form){
        return failsSum[item][form];
    }

    public int failSum(int item,int form,int frageForm){
        return failsSums[item][form][frageForm];
    }

    public int fails(int item,int form,int frageForm,int falshe){
        return fails[item][form][frageForm][falshe];
    }


    public int[] failSums(){
        return failSums;
    }

    public int[] failSums(int item){
        return failsSum[item];
    }

    public int[] failSums(int item,int form){
        return failsSums[item][form];
    }

    public int[] failSums(int item,int form,int falsheForm){
        return fails[item][form][falsheForm];
    }

    public void addAFail(int item, int form,int frageForm, int falshe) {
        this.fails[item][form][frageForm][falshe]++;
    }
}
