import 'package:didvan/config/design_config.dart';
import 'package:didvan/utils/extension.dart';
import 'package:flutter/material.dart';
import 'package:markdown_widget/config/configs.dart';
import 'package:markdown_widget/widget/blocks/leaf/heading.dart';
import 'package:markdown_widget/widget/blocks/leaf/paragraph.dart';
import 'package:markdown_widget/widget/markdown.dart';

class DidvanMarkdownText extends StatelessWidget {
  final String text;
  final Color? color;
  final double? width;

  const DidvanMarkdownText(
      {super.key, required this.text, this.color, this.width});

//  onTapLink: (text, href, title) {
//                                               if (href != null) {
//                                                 launchUrlString(
//                                                   href,
//                                                   mode: LaunchMode
//                                                       .inAppBrowserView,
//                                                 );
//                                               }
//                                             },
//  MarkdownStyleSheet defaultMarkdownStyleSheet = MarkdownStyleSheet(
//         pPadding: const EdgeInsets.all(0.8),
//         h1Padding: const EdgeInsets.all(0.8),
//         h2Padding: const EdgeInsets.all(0.8),
//         h3Padding: const EdgeInsets.all(0.8),
//         h4Padding: const EdgeInsets.all(0.8),
//         h5Padding: const EdgeInsets.all(0.8),
//         h6Padding: const EdgeInsets.all(0.8),
//         tablePadding: const EdgeInsets.all(0.8),
//         blockquotePadding: const EdgeInsets.all(0.8),
//         listBulletPadding: const EdgeInsets.all(0.8),
//         tableCellsPadding: const EdgeInsets.all(0.8),
//         codeblockPadding: const EdgeInsets.all(8),
//         code: TextStyle(
//           backgroundColor: Theme.of(context).colorScheme.black,
//           color: Theme.of(context).colorScheme.white,
//         ),
//         codeblockDecoration: BoxDecoration(
//             borderRadius: BorderRadius.circular(4),
//             color: Theme.of(context).colorScheme.black));

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection:
          text.startsWithEnglish() ? TextDirection.ltr : TextDirection.rtl,
      child: SizedBox(
        width: width,
        child: MarkdownWidget(
          data: text,
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          selectable: true,
          config: MarkdownConfig(configs: [
            H1Config(
                style: MarkdownConfig.defaultConfig.h1.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            H2Config(
                style: MarkdownConfig.defaultConfig.h2.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            H3Config(
                style: MarkdownConfig.defaultConfig.h3.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            H4Config(
                style: MarkdownConfig.defaultConfig.h4.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            H5Config(
                style: MarkdownConfig.defaultConfig.h5.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            H6Config(
                style: MarkdownConfig.defaultConfig.h6.style.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color)),
            PConfig(
                textStyle: MarkdownConfig.defaultConfig.p.textStyle.copyWith(
                    fontFamily: text.startsWithEnglish()
                        ? DesignConfig.fontFamily.replaceAll('-FA', '')
                        : null,
                    color: color))
          ]),
        ),
      ),
    );
  }
}
