import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:kasy_kit/components/components.dart';
import 'package:kasy_kit/core/config/features.dart';
import 'package:kasy_kit/core/theme/theme.dart';
import 'package:kasy_kit/environnements.dart';
import 'package:kasy_kit/features/subscription/ui/component/premium_page_factory.dart';
import 'package:kasy_kit/i18n/translations.g.dart';
import 'package:url_launcher/url_launcher.dart';

/// The bottom menu of the premium page
/// - It contains the restore button, terms and privacy buttons
/// You must show this on your paywall pages to be compliant with the app store guidelines
/// -------------------------------
/// Privacy    Restore     Terms
/// -------------------------------
class BottomPremiumMenu extends StatelessWidget {
  final OnTap? onTapRestore;
  final Color? textColor;

  const BottomPremiumMenu({super.key, this.onTapRestore, this.textColor});

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        _buildPrivacyButton(context),
        if (!kIsWeb || !revenuecatWeb) _buildRestoreButton(context),
        _buildTermsButton(context),
      ],
    );
  }

  Widget _buildTermsButton(BuildContext context) {
    final translations = Translations.of(context).premium;
    final color = textColor ?? context.colors.muted;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: KasySpacing.sm),
      child: KasyButton(
        variant: KasyButtonVariant.ghost,
        label: translations.terms,
        size: KasyButtonSize.small,
        foregroundColor: color,
        fontWeight: FontWeight.w500,
        onPressed: () {
          switch (defaultTargetPlatform) {
            case TargetPlatform.iOS:
              launchUrl(Uri.parse(
                "https://www.apple.com/legal/internet-services/itunes/dev/stdeula/",
              ));
            case TargetPlatform.android:
            default:
              launchUrl(Uri.parse(kTermsUrl));
          }
        },
      ),
    );
  }

  Widget _buildPrivacyButton(BuildContext context) {
    final translations = Translations.of(context).premium;
    final color = textColor ?? context.colors.muted;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: KasySpacing.sm),
      child: KasyButton(
        variant: KasyButtonVariant.ghost,
        label: translations.privacy,
        size: KasyButtonSize.small,
        foregroundColor: color,
        fontWeight: FontWeight.w500,
        onPressed: () => launchUrl(Uri.parse(kPrivacyUrl)),
      ),
    );
  }

  Widget _buildRestoreButton(BuildContext context) {
    final translations = Translations.of(context).premium;
    final color = textColor ?? context.colors.muted;
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: KasySpacing.sm),
      child: onTapRestore != null
          ? KasyButton(
              variant: KasyButtonVariant.ghost,
              label: translations.restore_action,
              size: KasyButtonSize.small,
              foregroundColor: color,
              fontWeight: FontWeight.w500,
              onPressed: onTapRestore,
            )
          : KasyButton.iconOnly(
              icon: KasyIcons.refresh,
              variant: KasyButtonVariant.ghost,
              foregroundColor: color,
              isLoading: true,
              onPressed: null,
              semanticLabel: translations.restore_action,
              size: KasyButtonSize.small,
            ),
    );
  }
}
