import 'package:boilerplate/core/i18n/translations.g.dart';
import 'package:boilerplate/core/styles/my_dimensions.dart';
import 'package:boilerplate/domain/entities/product/product_data.dart';
import 'package:boilerplate/presentation/widgets/ipm_html.dart';
import 'package:boilerplate/presentation/widgets/ipm_title_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/style.dart';
import 'package:html/parser.dart';

class ProductDetail extends StatelessWidget {
  final ProductData data;
  const ProductDetail(this.data, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // print(data.detail!);

    if (data.detail == null || data.detail!.isEmpty) {
      return Container();
    }

    return Container(
      padding: EdgeInsets.only(
        top: MyDimens.dimen10,
        left: MyDimens.dimen16,
        right: MyDimens.dimen16,
        bottom: MyDimens.dimen16,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          IPMTitleText(
            title1: AppLocalizations.products.detail_title_1,
            title2: AppLocalizations.products.detail_title_2,
          ),
          // IPMHtml(data.detail!, ''),
          SizedBox(
            height: 16,
          ),
          Text(
            '${_parseHtmlString(data.detail!)}',
            style: TextStyle(
              fontFamily: 'Prompt-Regular',
              fontSize: MyDimens.dimen16,
              fontWeight: FontWeight.w500,
              letterSpacing: 0.7,
              color: const Color(0xFF121212),
            ),
          ),
        ],
      ),
    );
  }

  String removeAllHtmlTags(String htmlText) {
    RegExp exp =
        RegExp(r'<[^>]*>|&[^;]+;', multiLine: true, caseSensitive: true);

    return htmlText.replaceAll(exp, '');
  }

  String formatHtmlString(String string) {
    return string
        .replaceAll("<p>", "  -") // Paragraphs
        .replaceAll("<p>&nbsp;", "") // Paragraphs
        .replaceAll("<p>&nbsp; ", "") // Paragraphs
        .replaceAll("<br>", "") // Line Breaks
        .replaceAll("&quot;", "") // Quote Marks
        .replaceAll("&apos;", "") // Apostrophe
        .replaceAll("&lt;", "") // Less-than Comparator (Strip Tags)
        .replaceAll("&gt;", "") // Greater-than Comparator (Strip Tags)
        .replaceAll("&nbsp;", "")
        .replaceAll("&nbsp; ", "")
        .replaceAll(";", "")
        .replaceAll("; ", "")
        .replaceAll("</p>", "")
        // .replaceAll("\n", "")
        // .replaceAll("\r", "")
        .trim(); // Whitespace
  }

  String _parseHtmlString(String htmlString) {
    final document = parse(htmlString);
    final String parsedString =
        parse(document.body!.text).documentElement!.text;

    return parsedString;
  }
}
