import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_bloc.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_event.dart';
import 'package:syncrow_app/features/menu/bloc/profile_bloc/profile_state.dart';
// import 'package:syncrow_app/features/menu/view/widgets/profile/region_page.dart';
// import 'package:syncrow_app/features/menu/view/widgets/profile/time_zone_screen_page.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

class ProfileView extends StatelessWidget {
  const ProfileView({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (BuildContext context) => ProfileBloc()..add(InitialProfileEvent()),
      child: BlocConsumer<ProfileBloc, ProfileState>(
        listener: (context, state) {},
        builder: (context, state) {
          final profileBloc = BlocProvider.of<ProfileBloc>(context);
          return DefaultScaffold(
            title: 'Syncrow Account',
            child: state is LoadingInitialState
                ? const Center(child: CircularProgressIndicator())
                : SizedBox(
                    height: MediaQuery.sizeOf(context).height,
                    child: ListView(
                      children: [
                        SizedBox(
                          height: MediaQuery.of(context).size.height * 0.05,
                        ),
                        InkWell(
                          onTap: () {
                            profileBloc.add(SelectImageEvent(context: context, isSelected: false));
                          },
                          child: SizedBox.square(
                            dimension: 125,
                            child: CircleAvatar(
                              backgroundColor: Colors.white,
                              child: SizedBox.square(
                                dimension: 120,
                                child: CircleAvatar(
                                  backgroundColor: Colors.white,
                                  backgroundImage: profileBloc.image == null
                                      ? null
                                      : FileImage(profileBloc.image!),
                                  child: profileBloc.image != null
                                      ? null
                                      : HomeCubit.user!.profilePicture != null
                                          ? ClipOval(
                                              child: Image.memory(
                                                HomeCubit.user!.profilePicture!,
                                                fit: BoxFit.cover,
                                                width: 120,
                                                height: 120,
                                              ),
                                            )
                                          : null,
                                ),
                              ),
                            ),
                          ),
                        ),
                        const SizedBox(height: 20),
                        SizedBox(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              IntrinsicWidth(
                                child: ConstrainedBox(
                                  constraints: const BoxConstraints(maxWidth: 200),
                                  child: TextFormField(
                                    maxLength: 30,
                                    style: const TextStyle(
                                      color: Colors.black,
                                    ),
                                    textAlign: TextAlign.center,
                                    focusNode: profileBloc.focusNode,
                                    controller: profileBloc.nameController,
                                    enabled: profileBloc.editName,
                                    onEditingComplete: () {
                                      profileBloc.add(SaveNameEvent(context: context));
                                    },
                                    decoration: const InputDecoration(
                                      hintText: "Your Name",
                                      border: InputBorder.none,
                                      fillColor: Colors.white10,
                                      counterText: '',
                                    ),
                                  ),
                                ),
                              ),
                              const SizedBox(width: 5),
                              InkWell(
                                onTap: () {
                                  profileBloc.add(const ChangeNameEvent(value: true));
                                },
                                child: const Padding(
                                  padding: EdgeInsets.symmetric(horizontal: 10),
                                  child: Icon(
                                    Icons.edit_outlined,
                                    size: 20,
                                    color: ColorsManager.textPrimaryColor,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        const SizedBox(height: 10),
                        DefaultContainer(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 25,
                              vertical: 5,
                            ),
                            child: Column(
                              mainAxisSize: MainAxisSize.min,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(top: 20, bottom: 20),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      const BodyMedium(text: 'email '),
                                      Flexible(
                                          child: BodyMedium(
                                              text: HomeCubit.user!.email ?? 'No Email')),
                                    ],
                                  ),
                                ),
                                // Container(
                                //   height: 1,
                                //   color: ColorsManager.greyColor,
                                // ),
                                // InkWell(
                                //   onTap: () {
                                //     profileBloc.add(const ChangeNameEvent(value: false));
                                //     Navigator.push(
                                //       context,
                                //       MaterialPageRoute(
                                //         builder: (context) => const RegionPage(),
                                //       ),
                                //     ).then((result) {
                                //       profileBloc.add(InitialProfileEvent());
                                //     });
                                //   },
                                //   child: Padding(
                                //     padding: const EdgeInsets.only(top: 20, bottom: 20),
                                //     child: Row(
                                //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                //       children: [
                                //         const BodyMedium(text: 'Region '),
                                //         Flexible(
                                //             child: BodyMedium(
                                //                 text: HomeCubit.user!.regionName ?? 'No Region')),
                                //       ],
                                //     ),
                                //   ),
                                // ),
                                // Container(
                                //   height: 1,
                                //   color: ColorsManager.greyColor,
                                // ),
                                // InkWell(
                                //   onTap: () {
                                //     profileBloc.add(const ChangeNameEvent(value: false));
                                //     Navigator.push(
                                //       context,
                                //       MaterialPageRoute(
                                //         builder: (context) => const TimeZoneScreenPage(),
                                //       ),
                                //     ).then((result) {
                                //       profileBloc.add(InitialProfileEvent());
                                //     });
                                //   },
                                //   child: Padding(
                                //     padding: const EdgeInsets.only(top: 15, bottom: 15),
                                //     child: Row(
                                //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                //       children: [
                                //         const BodyMedium(text: 'Time Zone '),
                                //         Flexible(
                                //           child: BodyMedium(
                                //               text: HomeCubit.user!.timeZone ?? "No Time Zone"),
                                //         ),
                                //       ],
                                //     ),
                                //   ),
                                // ),
                              ],
                            )),
                      ],
                    ),
                  ),
          );
        },
      ),
    );
  }
}
