import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

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

  @override
  Widget build(BuildContext context) {
    TextEditingController textEditingController = TextEditingController();
    return BlocConsumer<HomeCubit, HomeState>(
      listener: (context, state) {
        // if (state is ActivationError) {
        // }
      },
      builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            children: [
              const Text(
                'Join a Space',
                style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w700,
                    color: ColorsManager.secondaryColor),
              ),
              SizedBox(
                height: MediaQuery.of(context).size.height * 0.6,
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SvgPicture.asset(
                        Assets.assetsIconsMenuIconsHomeManagementIconsJoinAHome,
                        width: 70,
                        height: 70,
                        color: ColorsManager.grayButtonColors,
                      ),
                      const Padding(
                        padding: EdgeInsets.symmetric(
                          vertical: 30,
                        ),
                        child: BodyMedium(
                            fontWeight: FontWeight.w400,
                            textAlign: TextAlign.center,
                            text: 'Please enter your invitation code'),
                      ),
                      Column(
                        children: [
                          Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              border: Border.all(
                                color: state is ActivationError
                                    ? ColorsManager.red // Red border for error
                                    : ColorsManager
                                        .grayBox, // Default border color
                                width: 1.5, // Border width
                              ),
                              borderRadius:
                                  BorderRadius.circular(20.0), // Border radius
                            ),
                            padding: const EdgeInsets.symmetric(
                              vertical: 5,
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Flexible(
                                  child: TextFormField(
                                    validator: (value) {},
                                    controller: textEditingController,
                                    decoration: InputDecoration(
                                      hintText: 'Invitation code',
                                      hintStyle: context.bodyMedium.copyWith(
                                        color: Colors.grey,
                                      ),
                                      border: InputBorder.none,
                                    ),
                                  ),
                                ),
                                IconButton(
                                  onPressed: () async {
                                    if (textEditingController.text.isEmpty) {
                                      CustomSnackBar.displaySnackBar(
                                          'Please enter the invitation code');
                                      return;
                                    }
                                    if (await HomeCubit.getInstance()
                                        .activationCode(
                                            textEditingController.text)) {
                                      CustomSnackBar.displaySnackBar(
                                          'Done successfully');
                                      Future.delayed(
                                          const Duration(milliseconds: 500),
                                          () {
                                        Navigator.of(context).pop();
                                      });
                                    } 
                                  },
                                  icon: const Icon(
                                    Icons.arrow_right_alt,
                                  ),
                                ),
                              ],
                            ),
                          ),
                          state is ActivationError
                              ? Text(
                                  state.errMessage,
                                  style: const TextStyle(
                                      color: ColorsManager.red,
                                      fontWeight: FontWeight.w400),
                                )
                              : const SizedBox()
                        ],
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
        // return state is! GetSpacesLoading
        //     ? state is! GetSpaceRoomsLoading
        //         ? HomeCubit.getInstance().pages[HomeCubit.pageIndex]
        //         : const Center(child: CircularProgressIndicator())
        //     : const Center(child: CircularProgressIndicator());
      },
    );
  }
}

// return SizedBox(
//   width: MediaQuery.sizeOf(context).width,
//   height: MediaQuery.sizeOf(context).height,
//   child: Column(
//     mainAxisAlignment: MainAxisAlignment.center,
//     children: [
//       SvgPicture.asset(
//         Assets.noUnitsIconDashboard,
//         width: 100,
//         height: 100,
//       ),
//       const SizedBox(
//         height: 50,
//       ),
//       Flexible(
//         child: GestureDetector(
//           onTap: () {
//             Navigator.pushNamed(context, Routes.createUnit);
//           },
//           child: Container(
//             padding: const EdgeInsets.symmetric(horizontal: 34, vertical: 14),
//             decoration: ShapeDecoration(
//               color: const Color(0x99023DFE),
//               shape: RoundedRectangleBorder(
//                 borderRadius: BorderRadius.circular(20),
//               ),
//             ),
//             child: const TitleMedium(
//               text: 'Create a unit',
//               style: TextStyle(fontSize: 16, fontWeight: FontWeight.w400, color: Colors.white),
//             ),
//           ),
//         ),
//       ),
//     ],
//   ),
// );
//   }
// }
