import 'package:comwell_key_app/.generated/assets/assets.gen.dart';
import 'package:comwell_key_app/common/components/comwell_card_component.dart';
import 'package:comwell_key_app/common/components/round_icon_button.dart';
import 'package:comwell_key_app/presentation/screens/payment_processing/payment_processing_route.dart';
import 'package:comwell_key_app/presentation/screens/permission_overview/permission_overview_route.dart';
import 'package:comwell_key_app/presentation/screens/profile_settings/profile_settings_route.dart';
import 'card_content_widget.dart';
import 'comwell_club_container.dart';
import 'logout_dialog_widget.dart';
import 'profile_settings_item.dart';
import 'package:comwell_key_app/presentation/app/bloc/profile_cubit.dart';
import 'package:comwell_key_app/routing/app_routes.dart';
import 'package:comwell_key_app/themes/app_spaces.dart';
import 'package:comwell_key_app/themes/light_theme.dart';
import 'package:comwell_key_app/utils/l10n_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:url_launcher/url_launcher.dart';

class ProfilePageWidget extends StatelessWidget {
  final ProfileCubit cubit;

  const ProfilePageWidget({super.key, required this.cubit});

  @override
  Widget build(BuildContext context) {
    final user = cubit.state.user;
    final isActive = cubit.state.user.isClubMember == true;

    return SingleChildScrollView(
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 60, right: 10),
            child: Container(
              alignment: Alignment.centerRight,
              child: RoundIconButton(
                icon: Assets.icons.closeIcon.path,
                color: Colors.white,
                onPressed: () {
                  context.pop();
                },
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(16.0),
            child: Column(
              children: [
                ComwellCard(
                  content: CardContentWidget(cubit: cubit, isActive: isActive),
                  backgroundColor: isActive ? sandColor : sandColor[10]!,
                ),
              ],
            ),
          ),
          const SizedBox(height: 10),
          Container(
            color: Colors.white,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const SizedBox(height: 10),
                !isActive
                    ? ComwellClubContainer(
                        user: user,
                        onSignupClick: () {
                          cubit.init();
                        },
                      )
                    : const SizedBox(),
                const SizedBox(height: 10),
                profileSettingsItem(
                  context,
                  icon: Assets.icons.userCircleSvg.svg(),
                  text: context.strings.profile_settings_profile_menu,
                  trailingIcon: Icons.chevron_right,
                  onTap: () async {
                    ProfileSettingsRoute().push(context);
                  },
                ),
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Divider(color: colorDivider),
                ),
                profileSettingsItem(
                  context,
                  icon: Assets.icons.card.image(),
                  text: context.strings.payment_card_profile_menu,
                  trailingIcon: Icons.chevron_right,
                  onTap: () {
                    const PaymentCardsRoute(needsScaffold: true).push(context);
                  },
                ),
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Divider(color: colorDivider),
                ),
                profileSettingsItem(
                  context,
                  icon: const Icon(Icons.check),
                  trailingIcon: Icons.chevron_right,
                  text: context.strings.permissions,
                  onTap: () {
                    PermissionOverviewRoute().push(context);
                  },
                ),
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Divider(color: colorDivider),
                ),
                profileSettingsItem(
                  context,
                  icon: Assets.icons.bell.image(),
                  trailingIcon: Icons.chevron_right,
                  text: context.strings.notifications_profile_menu,
                  onTap: () {
                    context.push(AppRoutes.notifications);
                  },
                ),
                const Padding(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  child: Divider(color: colorDivider),
                ),
                const SizedBox(height: 10),
                if (isActive) ...[
                  profileSettingsItem(
                    context,
                    icon: Assets.icons.cLogo.svg(),
                    text: "Comwell Club",
                    trailingIcon: Icons.open_in_new,
                    onTap: () {
                      launchUrl(Uri.parse('https://comwell.com/profile'));
                    },
                  ),
                  const Padding(
                    padding: EdgeInsets.symmetric(horizontal: 16.0),
                    child: Divider(color: colorDivider),
                  ),
                ],
                const SizedBox(height: 10),
                Center(
                  child: OutlinedButton(
                    style: OutlinedButton.styleFrom(
                      side: const BorderSide(color: colorDivider),
                    ),
                    onPressed: () {
                      showDialog<void>(
                        context: context,
                        builder: (context) => LogoutDialogWidget(
                          cubit: context.read<ProfileCubit>(),
                        ),
                      );
                    },
                    child: Text(
                      context.strings.logout_profile_menu,
                      style: TextStyle(color: Colors.grey[600]),
                    ),
                  ),
                ),
                AppSpaces.gap16,
                FutureBuilder<String>(
                  future: context.read<ProfileCubit>().getVersion(),
                  builder: (context, snapshot) {
                    if (snapshot.hasData) {
                      return Text(
                        snapshot.requireData,
                        style: TextStyle(color: Colors.grey[500]),
                      );
                    }
                    return const SizedBox.shrink();
                  },
                ),
                AppSpaces.gap40,
              ],
            ),
          ),
        ],
      ),
    );
  }
}