'use strict';
const AddonSocketOn = require('../addon/socket_on');
const Evaluator = require('./models/evaluator');

class EvaluationSocketOn extends AddonSocketOn {
    static get socketEventKeys () {
        return ['fetchEvaluation', 'solveEvaluationProblem', 'solveEvaluationSolver'];
    }

    static async fetchEvaluation (socketProject, user) {
        await socketProject.logging(user.username, 'fetchEvaluation');
        const evaluator = new Evaluator({projectId: socketProject.projectId});
        user.socket.emit('evaluation', await evaluator.serialize());
        this.startEvaluateInterval(socketProject, user);
    }

    static async solveEvaluationProblem (socketProject, user, {problemName}) {
        await socketProject.logging(user.username, 'solveEvaluationProblem', {problemName});

        const Problem = Evaluator.findProblem(problemName);
        const problem = new Problem({projectId: socketProject.projectId});
        await problem.updateStatus({isOccurred: false});

        this._emitUpdateEvaluation(user, {problems: [await problem.serialize()]})
    }

    static async solveEvaluationSolver (socketProject, user, {solverName}) {
        await socketProject.logging(user.username, 'solveEvaluationSolver', {solverName});

        const Solver = Evaluator.findSolver(solverName);
        const solver = new Solver({projectId: socketProject.projectId});
        await solver.updateStatus({isSolved: true});

        this._emitUpdateEvaluation(user, {solvers: [await solver.serialize()]})
    }

    static startEvaluateInterval (socketProject, user) {
        this.evaluate(socketProject, user);
        const _id = setInterval(() => {
            if (user.active) {
                this.evaluate(socketProject, user);
            } else {
                clearInterval(_id);
            }
        }, 60 * 1000);
        return _id;
    }

    static evaluate (socketProject, user) {
        (async () => {
            const evaluator = new Evaluator({projectId: socketProject.projectId});
            const changes = await evaluator.evaluate();
            this._emitUpdateEvaluation(user, changes);
        })().catch(err => console.error(err));
    }

    static _emitUpdateEvaluation (user, {problems = [], causes = [], solvers = []}) {
        user.socket.emit('updateEvaluation', {
            problems,
            causes,
            solvers
        });
    }
}

module.exports = EvaluationSocketOn;

@@ -1026,24 +1026,39 @@
 eEvaluation(
+socketProject, 
 user, %7Bprobl
@@ -1483,24 +1483,39 @@
 eEvaluation(
+socketProject, 
 user, %7Bsolve
@@ -2144,16 +2144,31 @@
 luation(
+socketProject, 
 user, ch
@@ -2263,16 +2263,31 @@
 uation (
+socketProject, 
 user, %7Bp
@@ -2338,33 +2338,42 @@
         
-user.socket.emit(
+socketProject.emits(user, 
 'updateE

fix evaliation socket sync