import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart'
    show FaIcon, FontAwesomeIcons;
import 'package:go_router/go_router.dart';
import 'package:twonly/src/constants/routes.keys.dart';
import 'package:twonly/src/database/daos/contacts.dao.dart';
import 'package:twonly/src/database/twonly.db.dart';
import 'package:twonly/src/utils/misc.dart';
import 'package:twonly/src/visual/components/profile_qr_code.comp.dart';
import 'package:twonly/src/visual/elements/my_button.element.dart';
import 'package:twonly/src/visual/elements/svg_icon.element.dart';

const colorVerificationBadgeYellow = Color.fromARGB(255, 0, 182, 238);

class VerificationBadgeInfo extends StatelessWidget {
  const VerificationBadgeInfo({
    this.displayButtons = false,
    this.contact,
    super.key,
  });
  final bool displayButtons;
  final Contact? contact;

  @override
  Widget build(BuildContext context) {
    final scanButton = MyButton(
      variant: MyButtonVariant.primaryDense,
      onPressed: () => context.push(
        Routes.cameraQRScanner,
        extra: {
          if (contact != null) 'contact': contact,
          'openToVerify': true,
        },
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          const FaIcon(FontAwesomeIcons.camera),
          const SizedBox(width: 6),
          Text(
            contact != null
                ? context.lang.scanUserQrCode(
                    getContactDisplayName(contact!),
                  )
                : context.lang.scanNow,
          ),
        ],
      ),
    );

    final openButton = MyButton(
      variant: MyButtonVariant.primaryDense,
      onPressed: () => ProfileQrCodeComp.showSheet(
        context,
        contact: contact,
        openToVerify: true,
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.min,
        children: [
          const FaIcon(FontAwesomeIcons.qrcode),
          const SizedBox(width: 6),
          Text(
            contact != null
                ? context.lang.openOwnQrCode
                : context.lang.openQrCode,
          ),
        ],
      ),
    );

    return Column(
      children: [
        RichText(
          text: TextSpan(
            children: formattedText(
              context,
              context.lang.verificationBadgeGeneralDesc,
            ),
            style: const TextStyle(fontSize: 16),
          ),
          textAlign: TextAlign.center,
        ),

        const SizedBox(height: 30),
        _buildItem(
          context,
          icon: const SvgIcon(assetPath: SvgIcons.verifiedGreen, size: 40),
          description: context.lang.verificationBadgeGreenDesc,
          boldTextColor: context.color.primary,
          onTap: () => context.push(
            Routes.cameraQRScanner,
            extra: {
              if (contact != null) 'contact': contact,
              'openToVerify': true,
            },
          ),
        ),
        if (displayButtons)
          Padding(
            padding: const EdgeInsets.only(bottom: 20),
            child: contact != null
                ? Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      scanButton,
                      const SizedBox(height: 8),
                      openButton,
                    ],
                  )
                : Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      IntrinsicWidth(child: scanButton),
                      const SizedBox(width: 8),
                      IntrinsicWidth(child: openButton),
                    ],
                  ),
          ),
        _buildItem(
          context,
          icon: const SvgIcon(
            assetPath: SvgIcons.verifiedGreen,
            size: 40,
            color: colorVerificationBadgeYellow,
          ),
          description: context.lang.verificationBadgeYellowDesc,
          boldTextColor: colorVerificationBadgeYellow,
        ),
        _buildItem(
          context,
          icon: const SvgIcon(assetPath: SvgIcons.verifiedRed, size: 40),
          description: context.lang.verificationBadgeRedDesc,
          boldTextColor: const Color(0xffff0000),
        ),
      ],
    );
  }

  Widget _buildItem(
    BuildContext context, {
    required Widget icon,
    required String description,
    required Color boldTextColor,
    VoidCallback? onTap,
  }) {
    final item = Padding(
      padding: const EdgeInsets.symmetric(vertical: 25),
      child: Row(
        children: [
          icon,
          const SizedBox(width: 20),
          Expanded(
            child: RichText(
              text: TextSpan(
                children: formattedText(
                  context,
                  description,
                  boldTextColor: boldTextColor,
                ),
                style: const TextStyle(fontSize: 16),
              ),
            ),
          ),
        ],
      ),
    );

    if (onTap != null) {
      return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: onTap,
        child: item,
      );
    }
    return item;
  }
}
