/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import API.Instruction;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Eugenio
 */
public class Log implements ErrorReport {

//    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_BLACK = "\u001B[30m";
    public static final String ANSI_RED = "\u001B[31m";
//    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_BLUE = "\u001B[34m";
//    public static final String ANSI_PURPLE = "\u001B[35m";
//    public static final String ANSI_CYAN = "\u001B[36m";
//    public static final String ANSI_WHITE = "\u001B[37m";

    protected List<Instruction> items = new ArrayList<>();
    protected static String format = "%s{%s}[%s:%s|%s\t] : %s %s";
    protected int ThrowOnNErrors = 10;
    protected int errorCount = 0;

    public Log() {

    }

//    public void Add(int line, int colum, String classe, String msg) {
//        items.add(String.format(format, line, colum, classe, msg));
//    }
    public static void PrintError(String tag, Instruction error) {
        Print(tag, error.S("color", ANSI_RED));
    }

    public static void PrintInfo(String tag, Instruction info) {
        Print(tag, info.S("color", ANSI_BLUE));
    }

    public static void PrintWarning(String tag, Instruction war) {
        Print(tag, war.S("color", ANSI_YELLOW));
    }

    public void AddError(Instruction error) {
        Add(error.S("color", ANSI_RED), true);
    }

    public void AddInfo(Instruction info) {
        Add(info.S("color", ANSI_BLUE), false);
    }

    public void AddWarning(Instruction war) {
        Add(war.S("color", ANSI_YELLOW), false);
    }

    protected static void Print(String tag, Instruction log) {
        String f = log.get("format", Log.format);
        System.out.println(String.format(f,
                log.G("color"),
                tag,
                log.G("line"),
                log.G("col"),
                log.G("class"),
                log.G("msg"),
                ANSI_BLACK
        ));
    }

    protected void Add(Instruction error, boolean inc) {
        items.add(error);
        if (inc) {
            errorCount++;
        }
        if (errorCount == ThrowOnNErrors) {
            PrintErros();
//            throw new Exception("Many erros");
        }
    }

    @Override
    public void PrintErros() {
        for (Instruction error : items) {
            Print("", error);
        }
    }

    @Override
    public boolean HasError() {
        return errorCount > 0;
    }

}
