// Warning: Some assembly references could not be resolved automatically. This might lead to incorrect decompilation of some parts,
// for ex. property getter/setter access. To get optimal decompilation results, please manually add the missing references to the list of loaded assemblies.
// EdenAutoMorpherEditor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// Eden.AutoMorpher.AutoMorpherLogCollector
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;

public class AutoMorpherLogCollector
{
    private struct LogEntry
    {
        public DateTime utcTime;

        public LogType type;

        public string condition;

        public string stackTrace;
    }

    private bool _isCapturing;

    private DateTime _captureStartUtc;

    private readonly List<LogEntry> _entries = new List<LogEntry>(2048);

    private int entriesCapacity = 5000;

    private const string logSavePath = "Assets/@Eden_Tools/Eden_AutoMorpher/Logs";

    public void BeginCapture()
    {
        if (!this._isCapturing)
        {
            this._isCapturing = true;
            this._entries.Clear();
            this._captureStartUtc = DateTime.UtcNow;
            Application.logMessageReceived += this.OnLogMessageReceived;
        }
    }

    public void EndCapture()
    {
        if (this._isCapturing)
        {
            this._isCapturing = false;
            Application.logMessageReceived -= this.OnLogMessageReceived;
        }
    }

    public string SaveToTextFile(string defaultFileName, bool includeWarningsAndInfo)
    {
        this.EnsureLogDirectoryExists();
        string text = defaultFileName;
        if (string.IsNullOrEmpty(text))
        {
            text = "EdenAutoMorpher_Report.txt";
        }
        if (!text.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
        {
            text += ".txt";
        }
        string text2 = Path.Combine("Assets/@Eden_Tools/Eden_AutoMorpher/Logs", text);
        string contents = this.BuildReportText(includeWarningsAndInfo);
        File.WriteAllText(text2, contents, Encoding.UTF8);
        AssetDatabase.Refresh();
        Debug.Log("[AutoMorpher] Log report saved: " + text2);
        return text2;
    }

    private void EnsureLogDirectoryExists()
    {
        if (AssetDatabase.IsValidFolder("Assets/@Eden_Tools/Eden_AutoMorpher/Logs"))
        {
            return;
        }
        string[] array = "Assets/@Eden_Tools/Eden_AutoMorpher/Logs".Split('/', StringSplitOptions.None);
        string text = array[0];
        for (int i = 1; i < array.Length; i++)
        {
            string text2 = text + "/" + array[i];
            if (!AssetDatabase.IsValidFolder(text2))
            {
                AssetDatabase.CreateFolder(text, array[i]);
            }
            text = text2;
        }
    }

    public string BuildReportText(bool includeWarningsAndInfo)
    {
        StringBuilder stringBuilder = new StringBuilder(65536);
        stringBuilder.AppendLine("==== EDEN AUTO MORPHER LOG REPORT ====");
        stringBuilder.AppendLine($"Captured (UTC) : {this._captureStartUtc:yyyy-MM-dd HH:mm:ss} ~ {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss}");
        stringBuilder.AppendLine("Unity Version  : " + Application.unityVersion);
        stringBuilder.AppendLine($"Platform       : {Application.platform}");
        stringBuilder.AppendLine("Project        : " + Application.productName);
        stringBuilder.AppendLine();
        stringBuilder.AppendLine("---- Logs ----");
        for (int i = 0; i < this._entries.Count; i++)
        {
            LogEntry logEntry = this._entries[i];
            if (includeWarningsAndInfo || logEntry.type == LogType.Error || logEntry.type == LogType.Assert || logEntry.type == LogType.Exception)
            {
                stringBuilder.AppendLine($"[{i:0000}] - [{logEntry.type}] | {logEntry.utcTime:HH:mm:ss.fff} UTC");
                stringBuilder.AppendLine(logEntry.condition ?? string.Empty);
                if (!string.IsNullOrEmpty(logEntry.stackTrace))
                {
                    stringBuilder.AppendLine("Details:");
                    stringBuilder.AppendLine(logEntry.stackTrace);
                }
                stringBuilder.AppendLine();
            }
        }
        return stringBuilder.ToString();
    }

    private void OnLogMessageReceived(string condition, string stackTrace, LogType type)
    {
        if (this._isCapturing)
        {
            this._entries.Add(new LogEntry
            {
                utcTime = DateTime.UtcNow,
                type = type,
                condition = condition,
                stackTrace = stackTrace
            });
            if (this._entries.Count > this.entriesCapacity)
            {
                this._entries.RemoveRange(0, 1000);
            }
        }
    }
}
