// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename       : ConsoleHandler.cs
// Author(s)      : Natalia Portillo <claunia@claunia.com>
//
// Component      : Aaru GUI.
//
// --[ Description ] ----------------------------------------------------------
//
//     Receives AaruLogging events and stores them for showing in the console
//     window.
//
// --[ License ] --------------------------------------------------------------
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General public License as
//     published by the Free Software Foundation, either version 3 of the
//     License, or (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General public License for more details.
//
//     You should have received a copy of the GNU General public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2026 Natalia Portillo
// ****************************************************************************/

using System;
using System.Collections.ObjectModel;
using Aaru.Localization;
using Aaru.Logging;
using JetBrains.Annotations;

namespace Aaru.Gui;

static class ConsoleHandler
{
    static bool _verbose;

    public static bool Debug
    {
        set
        {
            if(field == value) return;

            field = value;

            if(field)
            {
                AaruLogging.DebugEvent          += OnDebugWriteHandler;
                AaruLogging.WriteExceptionEvent += OnWriteExceptionEvent;
            }
            else
            {
                AaruLogging.DebugEvent          -= OnDebugWriteHandler;
                AaruLogging.WriteExceptionEvent -= OnWriteExceptionEvent;
            }
        }
    }

    public static ObservableCollection<LogEntry> Entries { get; } = [];

    static void OnWriteExceptionEvent([NotNull] Exception ex, string message, params object[] objects) =>
        Entries.Add(new LogEntry
        {
            Message   = string.Format(message, objects),
            Module    = null,
            Timestamp = DateTime.Now,
            Type      = UI.LogEntry_Type_Exception
        });

    internal static void Init()
    {
        AaruLogging.WriteLineEvent += OnWriteHandler;
        AaruLogging.ErrorEvent     += OnErrorWriteHandler;
    }

    static void OnWriteHandler([CanBeNull] string format, [CanBeNull] params object[] arg)
    {
        if(format == null || arg == null) return;

        Entries.Add(new LogEntry
        {
            Message   = string.Format(format, arg),
            Module    = null,
            Timestamp = DateTime.Now,
            Type      = UI.LogEntry_Type_Info
        });
    }

    static void OnErrorWriteHandler([CanBeNull] string format, [CanBeNull] params object[] arg)
    {
        if(format == null || arg == null) return;

        Entries.Add(new LogEntry
        {
            Message   = string.Format(format, arg),
            Module    = null,
            Timestamp = DateTime.Now,
            Type      = UI.LogEntry_Type_Error
        });
    }

    static void OnDebugWriteHandler(string module, [CanBeNull] string format, [CanBeNull] params object[] arg)
    {
        if(format == null || arg == null) return;

        Entries.Add(new LogEntry
        {
            Message   = string.Format(format, arg),
            Module    = module,
            Timestamp = DateTime.Now,
            Type      = UI.LogEntry_Type_Debug
        });
    }
}

public sealed class LogEntry
{
    public string   Message   { get; set; }
    public string   Module    { get; set; }
    public DateTime Timestamp { get; set; }
    public string   Type      { get; set; }
}