import 'package:flutter/material.dart';

class LogManager {
  static final LogManager _instance = LogManager._internal();

  factory LogManager() {
    return _instance;
  }

  LogManager._internal();

  final List<String> _logs = [];
  final List<VoidCallback> _listeners = [];

  final List<String> _operationLogs = [];
  List<String> get operationLogs => _operationLogs;

  List<String> get logs => _logs;

  void addLog(String log) {
    _logs.add(log);
    _notifyListeners(); // Notify all listeners
  }

  void clearLogs() {
    _logs.clear();
    _notifyListeners(); // Notify all listeners
  }

  void addOperationLog(String log) {
    _operationLogs.add(log);
    _notifyListeners();
  }

  void clearOperationLogs() {
    _operationLogs.clear();
    _notifyListeners();
  }

  void addListener(VoidCallback listener) {
    _listeners.add(listener);
  }

  void removeListener(VoidCallback listener) {
    _listeners.remove(listener);
  }

  void _notifyListeners() {
    for (var listener in _listeners) {
      listener();
    }
  }
}

class DebugWidget extends StatefulWidget {
  const DebugWidget({super.key});

  @override
  State<DebugWidget> createState() => _DebugWidgetState();
}

class _DebugWidgetState extends State<DebugWidget> {
  final LogManager logManager = LogManager();

  @override
  void initState() {
    super.initState();
    logManager.addListener(_updateLogs);
  }

  @override
  void dispose() {
    logManager.removeListener(_updateLogs);
    super.dispose();
  }

  void _updateLogs() {
    Future.delayed(Duration.zero, () {
      if (mounted) {
        setState(() {});
      }
    });
  }

  void toggleExpanded() {
    setState(() {
      isExpanded = !isExpanded;
    });
  }

  void clearLogs() {
    logManager.clearLogs();
  }

  bool isExpanded = false;

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 0,
      child: GestureDetector(
        onTap: toggleExpanded,
        child: AnimatedContainer(
          duration: const Duration(milliseconds: 200),
          color: Colors.black54,
          height: isExpanded ? 450.0 : 50.0, // Adjust sizes as needed
          child: Column(
            children: [
              // Top bar with clear button
              if (isExpanded)
                Container(
                  padding: const EdgeInsets.symmetric(horizontal: 10.0),
                  color: Colors.blueGrey, // Adjust color as needed
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      const Text('Debug Logs', style: TextStyle(color: Colors.white)),
                      IconButton(
                        icon: const Icon(Icons.clear, color: Colors.white),
                        onPressed: clearLogs,
                      ),
                    ],
                  ),
                ),

              // Log messages
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: logManager.logs.reversed
                        .map((log) => Text(
                            "${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}:${DateTime.now().microsecond} - $log",
                            style: const TextStyle(color: Colors.white)))
                        .toList(),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
