import 'dart:convert';

import 'package:flutter/material.dart';

import '../../utils/clipboard_helper.dart';

class JsonViewer {
  static final _encoder = const JsonEncoder.withIndent('  ');

  static String format(Map<String, dynamic> jsonInput) =>
      _encoder.convert(jsonInput);

  static void asDialog(BuildContext context, Map<String, dynamic> dataMap) {
    showDialog(
      context: context,
      builder: (dialogCtx) => AlertDialog(
        scrollable: true,
        title: const Row(
          children: [Icon(Icons.bug_report_outlined), Text('Rohdaten')],
        ),
        content: Text(JsonViewer.format(dataMap)),
        actions: [
          TextButton(
            onPressed: () => copyToClipboard(
              dialogCtx,
              JsonViewer.format(dataMap),
              successMessage: 'Formatiertes JSON kopiert',
            ),
            child: const Text('Kopieren'),
          ),
          TextButton(
            onPressed: () => copyToClipboard(
              dialogCtx,
              dataMap.toString(),
              successMessage: 'Inline JSON kopiert',
            ),
            child: const Text('Inline Kopieren'),
          ),
          TextButton(
            onPressed: () => Navigator.of(dialogCtx).pop(),
            child: const Text('Schließen'),
          ),
        ],
      ),
    );
  }
}
