import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';

class DefaultAppBar extends StatelessWidget implements PreferredSizeWidget {
  const DefaultAppBar({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<HomeCubit, HomeState>(
      builder: (context, state) {
        return Container(
            padding: const EdgeInsets.only(
              top: 20,
            ),
            child: AppBar(
              backgroundColor: Colors.transparent,
              leadingWidth: 200,
              toolbarHeight: Constants.appBarHeight,
              leading: InkWell(
                onTap: () {
                  final spaces = HomeCubit.getInstance().spaces!;
                  showSpaceBottomSheet(context, spaces);
                },
                child: HomeCubit.getInstance().spaces!.isNotEmpty
                    ? AbsorbPointer(
                        absorbing: true,
                        child: HomeCubit.appBarLeading[HomeCubit
                            .bottomNavItems[HomeCubit.pageIndex].label]!,
                      )
                    : null,
              ),
              actions: HomeCubit.manageScene
                  ? HomeCubit.appBarActions[
                      HomeCubit.bottomNavItems[HomeCubit.pageIndex].label]
                  : null,
            ));
      },
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(Constants.appBarHeight);
}

void showSpaceBottomSheet(BuildContext context, List<SpaceModel> spaces) {
  showModalBottomSheet(
    isScrollControlled: true,
    context: context,
    builder: (BuildContext context) {
      return StatefulBuilder(
        builder: (BuildContext context, StateSetter setModalState) {
          String? selectedSpaceId = HomeCubit.getInstance().selectedSpace?.id;
          final bool shouldLimitHeight = spaces.length > 5;
          return Container(
            constraints: shouldLimitHeight
                ? BoxConstraints(
                    maxHeight: MediaQuery.of(context).size.height * 0.5,
                  )
                : const BoxConstraints(),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  const SizedBox(height: 10),
                  Container(
                    decoration: const BoxDecoration(color: Colors.black12),
                    height: 5,
                    width: 50,
                  ),
                  ListView.separated(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: spaces.length,
                    itemBuilder: (BuildContext context, int index) {
                      final space = spaces[index];
                      return Padding(
                        padding: const EdgeInsets.only(left: 30, right: 30),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Row(
                              children: [
                                Text(space.name),
                              ],
                            ),
                            Radio<String>(
                              value: space.id,
                              groupValue: selectedSpaceId,
                              onChanged: (String? newValue) {
                                if (newValue != null) {
                                  setModalState(() {
                                    selectedSpaceId = newValue;
                                  });
                                  HomeCubit.getInstance().changeSelectedSpace(
                                    spaces.firstWhere((s) => s.id == newValue),
                                  );
                                  Navigator.of(context).pop();
                                }
                              },
                            ),
                          ],
                        ),
                      );
                    },
                    separatorBuilder: (BuildContext context, int index) {
                      return Container(
                        padding: const EdgeInsets.symmetric(horizontal: 50),
                        child: const Divider(
                          color: Colors.grey,
                          thickness: 0.5,
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
          );
        },
      );
    },
  );
}
