﻿using System;
using UnityEngine;
using UnityEngine.UI;

public class DebugLogViewDialog : MonoBehaviour
{
    private static string logs = "";

    public GameObject panel;
    public Text messageText;

    private const int MAX_VISIBLE_LINES = 200;
    private const int MAX_CHARS = 8000;  

    public static void AddLog(string log)
    {
        if (string.IsNullOrEmpty(log))
            return;

        if (string.IsNullOrEmpty(logs))
            logs = log;
        else
            logs = logs + "\n" + log;

        // 1) 라인 수 제한
        string[] lines = logs.Split('\n');
        if (lines.Length > MAX_VISIBLE_LINES)
        {
            int start = lines.Length - MAX_VISIBLE_LINES;
            logs = string.Join("\n", lines, start, MAX_VISIBLE_LINES);
        }

        // 2) 문자 수 제한
        if (logs.Length > MAX_CHARS)
        {
            logs = logs.Substring(logs.Length - MAX_CHARS);
        }
    }

    public static void Show()
    {
        Present();
    }

    public static void Present()
    {
        GameObject debugLogDialogPanel = GameObject.Find("DebugLogDialogPanel");

        Image image = debugLogDialogPanel.GetComponent<Image>();
        image.enabled = true;

        DebugLogViewDialog debugLogDialog = debugLogDialogPanel.GetComponent<DebugLogViewDialog>();
        debugLogDialog.panel.SetActive(true);

        debugLogDialog.messageText.text = logs;
    }

    public static void Dismiss()
    {
        GameObject debugLogDialogPanel = GameObject.Find("DebugLogDialogPanel");

        Image image = debugLogDialogPanel.GetComponent<Image>();
        image.enabled = false;

        DebugLogViewDialog debugLogDialog = debugLogDialogPanel.GetComponent<DebugLogViewDialog>();
        debugLogDialog.panel.SetActive(false);
    }

    public void OnClick()
    {
        Dismiss();
    }

    public void Clear()
    {
        messageText.text = "";
        logs = "";
    }

    public void Copy()
    {
        GUIUtility.systemCopyBuffer = logs;
    }
}
