import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/states/user_state_notifier.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_background.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/premium_card.dart';
import 'package:kasy_kit/features/subscription/ui/widgets/unsubscribe_feedback_popup.dart';
import 'package:kasy_kit/i18n/translations.g.dart';

typedef OnTapUnSubscribe = void Function(String reason);
typedef OnExit = void Function();

class ActivePremiumContent extends ConsumerStatefulWidget {
  final OnTapUnSubscribe? onTap;
  final OnExit? onExit;

  const ActivePremiumContent({super.key, this.onExit, required this.onTap});

  @override
  ConsumerState<ActivePremiumContent> createState() =>
      _ActivePremiumContentState();
}

class _ActivePremiumContentState extends ConsumerState<ActivePremiumContent> {
  bool _isPopupShowing = false;

  Future<void> _showUnsubscribePopup(BuildContext context) async {
    if (_isPopupShowing) return;
    setState(() => _isPopupShowing = true);
    try {
      await UnsubscribeFeedbackPopup.show(
        context: context,
        onConfirm: (String reason) {
          Navigator.of(context).pop();
          widget.onTap?.call(reason);
        },
        onCancel: () {},
      );
    } finally {
      if (mounted) setState(() => _isPopupShowing = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    final userState = ref.watch(userStateNotifierProvider);

    return PremiumBackground(
      bgImagePath: "assets/images/premium-bg.jpg",
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          foregroundColor: context.colors.onPrimary,
          leading: widget.onExit != null
              ? KasyButton.iconOnly(
                  icon: KasyIcons.close,
                  variant: KasyButtonVariant.ghost,
                  foregroundColor: context.colors.onPrimary,
                  onPressed: widget.onExit,
                  semanticLabel:
                      MaterialLocalizations.of(context).closeButtonTooltip,
                )
              : null,
          automaticallyImplyLeading: widget.onExit == null,
        ),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: KasySpacing.pageHorizontalGutter),
              child: PremiumCard(
                bgColor: context.colors.onBackground.withValues(alpha: .54),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(bottom: KasySpacing.sm),
                      child: Chip(
                        backgroundColor: context.colors.primary,
                        label: Text(
                          "ACTIVE",
                          style: GoogleFonts.albertSans(
                            fontSize: 12,
                            fontWeight: FontWeight.w700,
                            color: context.colors.onPrimary,
                          ),
                        ),
                      ),
                    ),
                    Text(
                      "Premium",
                      textAlign: TextAlign.center,
                      style: GoogleFonts.albertSans(
                        fontSize: 32,
                        fontWeight: FontWeight.w700,
                        color: context.colors.onPrimary,
                      ),
                    ),
                    const SizedBox(height: KasySpacing.smd),
                    // for (var i = 0; i < features.length; i++)
                    //   Padding(
                    //     padding: const EdgeInsets.symmetric(horizontal: 8.0),
                    //     child: FeatureLine(
                    //       title: translations.features[i],
                    //       topPadding: i > 0 ? 4 : 0,
                    //       icon: switch (i) {
                    //         0 => HugeIcons.strokeRoundedTaskDone02,
                    //         1 => HugeIcons.strokeRoundedCheckmarkSquare01,
                    //         2 => HugeIcons.strokeRoundedGame,
                    //         _ => Icons.check,
                    //       },
                    //     ),
                    //   ),
                    const SizedBox(height: KasySpacing.xl),
                    if (userState.subscription?.isLifetime == true)
                      Padding(
                        padding: const EdgeInsets.all(KasySpacing.lg),
                        child: Text(
                          Translations.of(
                            context,
                          ).activePremium.lifetime_user_description,
                          style: context.textTheme.bodyMedium?.copyWith(
                            fontWeight: FontWeight.w700,
                          ),
                          textAlign: TextAlign.center,
                        ),
                      ),
                    // SliverToBoxAdapter(
                    //   child: Padding(
                    //     padding: hPadding,
                    //     child: ComparisonTable(
                    //       rows: ComparisonTableRow.getComparisonRows(
                    //         context,
                    //         ref.read(environmentProvider),
                    //       ),
                    //       freeColumnTitle:
                    //           ComparisonTableRow.getFreeColumnTitle(context),
                    //       premiumColumnTitle:
                    //           ComparisonTableRow.getPremiumColumnTitle(context),
                    //     ),
                    //   ),
                    // ),
                    if (userState.subscription?.isLifetime == false)
                      Padding(
                        padding: const EdgeInsets.fromLTRB(
                          KasySpacing.pageHorizontalGutter,
                          72,
                          KasySpacing.pageHorizontalGutter,
                          KasySpacing.lg,
                        ),
                        child: KasyButton(
                          label: Translations.of(context).activePremium.unsubscribe_button,
                          variant: KasyButtonVariant.soft,
                          expand: true,
                          onPressed: () => _showUnsubscribePopup(context),
                        ),
                      ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
