import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';
import 'package:krow/app.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_box_decorations.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_bloc.dart';
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_event.dart';
import 'package:krow/features/profile/profile_main/domain/bloc/user_profile_state.dart';
import 'package:krow/features/profile/profile_main/presentation/widgets/user_avatar_widget.dart';
import 'package:krow/features/profile/profile_main/presentation/widgets/user_roles_widget.dart';

import '../../../../../core/presentation/widgets/restart_widget.dart';

class UserProfileCard extends StatefulWidget {
  final ProfileState state;

  const UserProfileCard({super.key, required this.state});

  @override
  State<UserProfileCard> createState() => _UserProfileCardState();
}

class _UserProfileCardState extends State<UserProfileCard> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      fit: StackFit.loose,
      children: [
        _buildProfileBackground(context),
        SafeArea(
          bottom: false,
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                const Gap(32+ kToolbarHeight),
                UserAvatarWidget(
                  imageUrl: widget.state.staff?.avatar,
                  userName:
                      '${widget.state.staff?.firstName ?? ''} ${widget.state.staff?.lastName ?? ''}',
                  rating: widget.state.staff?.averageRating ?? 5,
                ),
                const Gap(16),
                _buildUserInfo(context),
                const Gap(24),
                _buildAvailabilitySwitcher(context),
                const Gap(8),
                UserRolesWidget(roles: widget.state.roles),
                const Gap(24),
              ],
            ),
          ),
        )
      ],
    );
  }

  Column _buildUserInfo(BuildContext context) {
    return Column(
      children: [
        Text(
          '${widget.state.staff?.firstName ?? ''} ${widget.state.staff?.lastName ?? ''}',
          style: AppTextStyles.headingH3.copyWith(color: Colors.white),
          textAlign: TextAlign.center,
        ),
        if (widget.state.staff?.email?.isNotEmpty ?? false) ...[
          const Gap(8),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Assets.images.userProfile.sms.svg(),
              const Gap(4),
              Text(
                widget.state.staff!.email!,
                style: AppTextStyles.bodyMediumReg
                    .copyWith(color: AppColors.darkBgInactive),
              )
            ],
          ),
        ],
        if (widget.state.staff?.phone != null) ...[
          const Gap(8),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Assets.images.userProfile.call.svg(),
              const Gap(4),
              Text(
                widget.state.staff!.phone!,
                style: AppTextStyles.bodyMediumReg
                    .copyWith(color: AppColors.darkBgInactive),
              )
            ],
          ),
        ]
      ],
    );
  }

  Widget _buildAppBar(BuildContext context) {
    return Container(
      height: 48,
      margin: const EdgeInsets.symmetric(horizontal: 16),
      // child: Row(
      //   crossAxisAlignment: CrossAxisAlignment.center,
      //   children: [
      //     Text('Profile'.tr(),
      //         style: Theme.of(context)
      //             .textTheme
      //             .headlineSmall
      //             ?.copyWith(color: Colors.white)),
      //     const Spacer(),
      //     GestureDetector(
      //       onTap: () async{
      //         context.setLocale(
      //           context.locale == Locale('es') ? Locale('en') : Locale('es'),
      //         );
      //        await  Future.delayed(Duration(microseconds: 500));
      //         RestartWidget.restartApp(context);
      //       },
      //       child: Row(
      //         children: [
      //           Text(context.locale == Locale('en')?'ENG':'ESP',style: AppTextStyles.bodyMediumMed.copyWith(color: Colors.white)),
      //         ],
      //       ),
      //     ),
      //     Container(
      //       width: 48,
      //       height: 48,
      //       alignment: Alignment.center,
      //       child: Assets.images.appBar.notification.svg(
      //           colorFilter:
      //               const ColorFilter.mode(Colors.white, BlendMode.srcIn)),
      //     ),
      //   ],
      // ),
    );
  }

  Widget _buildProfileBackground(BuildContext context) {
    return Positioned.fill(
      child: ClipRRect(
        borderRadius: const BorderRadius.only(
          bottomLeft: Radius.circular(12),
          bottomRight: Radius.circular(12),
        ),
        child: Container(
          width: MediaQuery.of(context).size.width,
          color: AppColors.bgColorDark,
          child: Assets.images.bg
              .svg(fit: BoxFit.fitWidth, alignment: Alignment.topCenter),
        ),
      ),
    );
  }

  Widget _buildAvailabilitySwitcher(BuildContext context) {
    return Container(
      height: 52,
      margin: const EdgeInsets.symmetric(horizontal: 16),
      padding: const EdgeInsets.symmetric(horizontal: 12),
      decoration: KwBoxDecorations.primaryDark,
      child: Row(
        children: [
          Text(
            'available_right_away'.tr(),
            style: AppTextStyles.bodyMediumReg
                .copyWith(color: Colors.white, fontWeight: FontWeight.w500),
          ),
          const Spacer(),
          CupertinoSwitch(
            value: widget.state.isAvailableNow,
            onChanged: (value) {
              BlocProvider.of<ProfileBloc>(context)
                  .add(ProfileSwitchAvailability(available: value));
            },
          ),
        ],
      ),
    );
  }
}
