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

import '../../state/app/modules/settings/bloc/settings_cubit.dart';
import '../centered_leading.dart';
import 'json_viewer.dart';

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

  bool devConditionFulfilled() =>
      context.read<SettingsCubit>().val().devToolsEnabled &&
      (onlyInDebug ? kDebugMode : true);

  Widget jsonData(Map<String, dynamic> data) => callback(
        title: 'JSON daten anzeigen',
        onTab: () => JsonViewer.asDialog(context, data),
      );

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

  Widget child(Widget child) =>
      Visibility(visible: devConditionFulfilled(), child: child);

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