/**
 * IMPORTANT !
 * Décommenter temporairement la ligne suivante (import { } from "./mock_jasmine")
 * Pour exécuter ce code dans le débugger.
 * Faire de même avec le fichier test_func.ts
 */
// import { describe, expect, it, xdescribe } from "../mock_jasmine";

import { ParamCalculability, ParamDefinition, ParamsEquation } from "../../src/param";
import { CreateStructure, StructureType } from "../../src/structure/factory_structure";
import { ParallelStructure } from "../../src/structure/parallel_structure";
import { ParallelStructureParams } from "../../src/structure/parallel_structure_params";
import { Structure } from "../../src/structure/structure";
import { Describer } from "../../src/util/describer";
import { EnumEx } from "../../src/util/enum";
import { Result } from "../../src/util/result";
import { checkResult } from "../test_func";
import { structTest } from "./structure_test";

const pstruct: ParallelStructure = new ParallelStructure(
    new ParallelStructureParams(30, 30, 15), // Q = 30, Z1 = 30, Z2 = 15
    false // debug
);

/*
 * Tests avec deux structures test identiques
 */

pstruct.addStructure(structTest);
pstruct.addStructure(structTest);

describe("Class ParallelStructure: ", () => {
    describe("Calc()", () => {
        itParallelStructure("Q", 30, 15);
        itParallelStructure("Z1", 30, 15);
        itParallelStructure("Z2", 15, 15);
    });
});

/**
 * Test sur ParallelStructure
 * @param sVarCalc Variable à calculer
 * @param rVcalc Valeur de référence à retrouver
 * @param Q Débit de chacune des structures (pour structures identiques uniquement)
 */
function itParallelStructure(sVarCalc: string, rVcalc: number, Q?: number) {
    it(`${sVarCalc} should be ${rVcalc}`, () => {
        const res: Result = pstruct.Calc(sVarCalc);
        checkResult(res, rVcalc);
        if (Q !== undefined) {
            for (let i = 1 ; i < res.nbResults ; i++) {
                checkResult(res.extractResult(i), Q);
            }
        }
    });
}

/*
 * Tests avec toutes les équations et toutes les variables (cf. jalhyd#38)
 */

const ps2: ParallelStructure = new ParallelStructure(
    new ParallelStructureParams(0, 102, 101.5), // Q = 0, Z1 = 102, Z2 = 101.5
    false // debug
);

// Ajout d'une structure de chaque type dans ParallelStructure
for (const i of EnumEx.getValues(StructureType)) {
    ps2.addStructure(CreateStructure(i));
}

ps2.prms.Q.v = ps2.Calc("Q").vCalc;

// tslint:disable-next-line:prefer-for-of
describe("Class ParallelStructure: ", () => {
    for (let i = 0 ; i < ps2.structures.length ; i++) {
        const st: Structure = ps2.structures[i];
        describe(`this.structures[${i}]: Structure${StructureType[i]}: `, () => {
            // tslint:disable-next-line:forin
            for (const prm of st.prms) {
                if (prm.calculability === ParamCalculability.DICHO &&
                    prm.symbol !== "Z1" && prm.symbol !== "Z2") {
                    const ref: number = prm.v;
                    const res: Result = ps2.Calc(i + "." + prm.symbol);
                    prm.v = ref; // Go back to initial value for following tests
                    if ((i === 2 || i === 4) && prm.symbol === "ZDV") {
                        xit(`Calc(${prm.symbol}) should return ${ref}`, () => {
                            checkResult(res, ref);
                        });
                    } else {
                        it(`Calc(${prm.symbol}) should return ${ref}`, () => {
                            checkResult(res, ref);
                        });
                    }
                }
            }
        });
    }
});