package library.step0;

import java.time.temporal.ChronoUnit;
import java.util.List;

// Mostrador de la biblioteca: calcula las multas de un socio y arma su estado
// de cuenta. Funciona y tiene pruebas, pero está mal diseñado a propósito:
// es el punto de partida del refactoring de la semana 13.
public class LibraryDesk {

    public String statement(Member m, List<Loan> l) {
        String s = "Multas de " + m.name() + " (" + m.type() + ")\n";
        int t = 0;
        for (Loan x : l) {
            int d = (int) ChronoUnit.DAYS.between(x.due(), x.returned());
            if (d < 0) {
                d = 0;
            }
            int a = 0;
            switch (m.type()) {
                case STUDENT:
                    if (d > 2) {
                        a = (d - 2) * (x.kind() == ItemKind.DVD ? 100 : 50);
                    }
                    if (a > 1000) {
                        a = 1000;
                    }
                    break;
                case TEACHER:
                    if (x.kind() == ItemKind.DVD) {
                        a = d * 100;
                    }
                    break;
                case VISITOR:
                    a = d * (x.kind() == ItemKind.DVD ? 100 : 50) * 2;
                    break;
            }
            t += a;
            s += String.format("  %-18s %3d días  S/ %d.%02d\n", x.title(), d, a / 100, a % 100);
        }
        s += String.format("Total: S/ %d.%02d\n", t / 100, t % 100);
        return s;
    }

    public int totalFine(Member m, List<Loan> l) {
        int t = 0;
        for (Loan x : l) {
            int d = (int) ChronoUnit.DAYS.between(x.due(), x.returned());
            if (d < 0) {
                d = 0;
            }
            int a = 0;
            switch (m.type()) {
                case STUDENT:
                    if (d > 2) {
                        a = (d - 2) * (x.kind() == ItemKind.DVD ? 100 : 50);
                    }
                    if (a > 1000) {
                        a = 1000;
                    }
                    break;
                case TEACHER:
                    if (x.kind() == ItemKind.DVD) {
                        a = d * 100;
                    }
                    break;
                case VISITOR:
                    a = d * (x.kind() == ItemKind.DVD ? 100 : 50) * 2;
                    break;
            }
            t += a;
        }
        return t;
    }
}
