package lab;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

// Registro de notas de un curso. Funciona y tiene pruebas, pero está mal
// diseñado a propósito: es el punto de partida del laboratorio de la semana
// 13. No lo arreglen aquí; cópienlo a su repositorio y refactoricen allá.
//
// Cada fila de datos: código;nombre;sistema;nota1;nota2;nota3
//   sistema A: promedio simple de las tres notas
//   sistema B: la tercera nota (examen final) pesa doble
//   sistema C: la mejor de las dos primeras, promediada con la tercera
public class GradeBook {

    public List<String[]> rows = new ArrayList<>();

    public void load(String text) {
        for (String line : text.split("\n")) {
            if (!line.isBlank()) {
                rows.add(line.split(";"));
            }
        }
    }

    public String report() {
        String r = "";
        int ok = 0;
        int ko = 0;
        int sum = 0;
        for (String[] x : rows) {
            int a = Integer.parseInt(x[3]);
            int b = Integer.parseInt(x[4]);
            int c = Integer.parseInt(x[5]);
            double p;
            if (x[2].equals("A")) {
                p = (a + b + c) / 3.0;
            } else if (x[2].equals("B")) {
                p = (a + b + 2 * c) / 4.0;
            } else if (x[2].equals("C")) {
                p = (Math.max(a, b) + c) / 2.0;
            } else {
                p = 0;
            }
            int f = (int) Math.round(p);
            String st;
            if (f >= 11) {
                st = "aprobado";
                ok++;
            } else {
                st = "desaprobado";
                ko++;
            }
            r += x[0] + " " + x[1] + ": " + f + " (" + st + ")\n";
            sum += f;
        }
        int tenths = (int) Math.round(sum * 10.0 / rows.size());
        r += "aprobados: " + ok + ", desaprobados: " + ko + "\n";
        r += "promedio del curso: " + tenths / 10 + "." + tenths % 10 + "\n";
        return r;
    }

    public int finalGrade(String system, int a, int b, int c) {
        double p;
        if (system.equals("A")) {
            p = (a + b + c) / 3.0;
        } else if (system.equals("B")) {
            p = (a + b + 2 * c) / 4.0;
        } else if (system.equals("C")) {
            p = (Math.max(a, b) + c) / 2.0;
        } else {
            p = 0;
        }
        return (int) Math.round(p);
    }

    public void publish() {
        BulletinBoard board = new BulletinBoard();
        board.post("Notas al " + LocalDate.now() + "\n" + report());
    }
}
