package Common.Utils;
import Common.Visual.UI;
import Common.Visual.Windows.ReadOnlyMultilineTextForm;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Paths;
import java.util.Date;
public interface Loggable {
    String getLogHomePath();
    String getLogName();
    default File getLogFile() {
        return Paths.get(getLogHomePath(), (getLogName() + "_log.txt")).toFile();
    }
    default void ClearLog() {
        try {
            Utils_.forceDeleteWithCheck(getLogFile());
        } catch (Exception ignored) {
        }
    }
    default void Print(String message) {
        try {
            FileWriter Log = new FileWriter(getLogFile(), true);
            String datedMessage = Utils_.Brackets(new Date()) + " " + message;
            Log.write(datedMessage + "\n");
            Log.close();
        } catch (Exception ignored) {
        }
    }
    default void PrintException(Exception ex) {
        StringWriter out = new StringWriter();
        PrintWriter writer = new PrintWriter(out);
        ex.printStackTrace(writer);
        writer.flush();
        Print(out.toString());
        if (UI.isActive()) {
            Utils_.CopyToClipboard(out.toString());
            ReadOnlyMultilineTextForm ff = new ReadOnlyMultilineTextForm();
            ff.ShowDialog("Возникло исключение", out.toString());
        }
    }
}
