import 'package:auto_route/auto_route.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:krow/core/presentation/gen/assets.gen.dart';
import 'package:krow/core/presentation/styles/kw_text_styles.dart';
import 'package:krow/core/presentation/styles/theme.dart';
import 'package:krow/core/presentation/widgets/restart_widget.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/profile_menu_widget.dart';
import 'package:krow/features/profile/profile_main/presentation/widgets/user_profile_card.dart';

@RoutePage()
class ProfileMainScreen extends StatefulWidget implements AutoRouteWrapper {
  const ProfileMainScreen({super.key});

  @override
  State<ProfileMainScreen> createState() => _ProfileMainScreenState();

  @override
  Widget wrappedRoute(BuildContext context) {
    return BlocProvider(
      create: (context) => ProfileBloc()..add(ProfileEventInit()),
      child: this,
    );
  }
}

class _ProfileMainScreenState extends State<ProfileMainScreen> {
  final ScrollController _scrollController = ScrollController();
  bool _isScrolled = false;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_scrollListener);
  }

  void _scrollListener() {
    if (_scrollController.offset > 50 && !_isScrolled) {
      setState(() {
        _isScrolled = true;
      });
    } else if (_scrollController.offset <= 50 && _isScrolled) {
      setState(() {
        _isScrolled = false;
      });
    }
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      extendBodyBehindAppBar: true,
      body: Stack(
        children: [
          SingleChildScrollView(
            controller: _scrollController,
            physics: const ClampingScrollPhysics(),
            child: BlocBuilder<ProfileBloc, ProfileState>(
              builder: (context, state) {
                return Column(
                  children: [
                    UserProfileCard(state: state),
                    ProfileMenuWidget(state: state),
                  ],
                );
              },
            ),
          ),
          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: AppBar(
              titleSpacing: 0,
              leadingWidth: 0,
              automaticallyImplyLeading: false,
              title: _buildAppBar(context),
              actionsPadding:  EdgeInsets.zero,
              elevation: 0,
              backgroundColor: _isScrolled? AppColors.bgColorDark : Colors.transparent,
              scrolledUnderElevation: 0,

              shadowColor: Colors.black,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildAppBar(BuildContext context) {
    return Container(
      padding: 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)),
          ),
        ],
      ),
    );
  }
}
