import { Questionnaire as QuestionnaireInterface } from "../models/questionnaire";
import { Question } from './question';

export class Questionnaire implements QuestionnaireInterface {
    
    
    _enseignant: string;
    _cours: string;
    _nom: string;
    _description: string;
    _etat: boolean;
    _questions: Question[];
    _notes: any[];
    _questionnaireRemis: boolean;

    constructor(enseignant: string, cours: string, nom: string, description: string, etat: boolean) {
        this._enseignant = enseignant;
        this._cours = cours;
        this._nom = nom;
        this._description = description;
        this._etat = etat;
        this._questions = [];
        this._notes = [];
        this._questionnaireRemis = false;
    }

    public ajouterQuestion(question: Question) {
        this._questions.push(question);
    }

    public retirerQuestion(question: Question) {
        const index = this._questions.indexOf(question)
        delete this._questions[index];
    }

    public reordonner(ordreQuestion: string[]) {
        const newArray = []
        ordreQuestion.forEach(x => {
            const question = this._questions.find(ques => ques.getNom() === x)
            newArray.push(question)
        })
        this._questions = newArray
    }

    public genererNotes() {
        this._notes = [];
        
        for(let i = 1; i <= 10; i++) {
            let note = Math.floor(Math.random() * 100 + 1);
            let etudiant = `first_name.last_name+${i}@gmail.com`;
            this._notes.push({etudiant: etudiant, note: note});
        }

        return this._notes;
    }
    
    public setQuestionnaireRemis() {
        this._questionnaireRemis = true;
    }

    public toJSON() {
        return {
            nom: this._nom,
            description: this._description,
            questions: this._questions
        };
    }
}