import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../storage/base/settingsProvider.dart';
import '../centeredLeading.dart';
import 'jsonViewer.dart';

class DebugTile {
  BuildContext context;
  bool onlyInDebug;
  DebugTile(this.context, {this.onlyInDebug = false});

  bool devConditionFulfilled() => Provider.of<SettingsProvider>(context, listen: false).val().devToolsEnabled && (onlyInDebug ? kDebugMode : true);

  Widget jsonData(Map<String, dynamic> data, {bool ignoreConfig = false}) {
    return callback(
      title: "JSON daten anzeigen",
      onTab: () => JsonViewer.asDialog(context, data)
    );
  }

  Widget callback({String title = "Debugaktion", required void Function() onTab}) {
    return child(
      ListTile(
        leading: const CenteredLeading(Icon(Icons.developer_mode_outlined)),
        title: Text(title),
        subtitle: const Text("Entwicklermodus aktiviert"),
        onTap: onTab,
      )
    );
  }

  Widget child(Widget child) {
    return Visibility(
      visible: devConditionFulfilled(),
      child: child,
    );
  }

  void run(void Function() callback) {
    if(!devConditionFulfilled()) return;
    callback();
  }
}