import 'package:flutter/material.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/menu/view/widgets/manage_home/home_settings.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/helpers/custom_page_route.dart';
import 'package:syncrow_app/utils/helpers/misc_string_helpers.dart';
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';

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

  @override
  Widget build(BuildContext context) {
    var spaces = HomeCubit.getInstance().spaces;
    return DefaultScaffold(
        title: 'Manage Your Home',
        height: MediaQuery.sizeOf(context).height,
        child: spaces.isEmpty
            ? const Center(
                child: BodyMedium(text: 'No spaces found'),
              )
            : DefaultContainer(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 25),
                child: ListView.builder(
                    itemCount: spaces.length,
                    itemBuilder: (context, index) {
                      if (index == spaces.length - 1) {
                        return InkWell(
                          onTap: () {
                            Navigator.of(context).push(CustomPageRoute(
                                builder: (context) => HomeSettingsView(
                                      space: spaces[index],
                                    )));
                          },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              BodyMedium(text: StringHelpers.toTitleCase(spaces[index].name)),
                              const Icon(
                                Icons.arrow_forward_ios,
                                color: ColorsManager.greyColor,
                                size: 15,
                              )
                            ],
                          ),
                        );
                      }
                      return InkWell(
                        onTap: () {
                          //TODO refactor the routing to use named routes
                          // Navigator.of(context).pushNamed(
                          //   '/home_settings',
                          //   arguments: spaces[index],
                          // );

                          Navigator.of(context).push(CustomPageRoute(
                              builder: (context) => HomeSettingsView(
                                    space: spaces[index],
                                  )));
                        },
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                BodyMedium(text: HomeCubit.getInstance().spaces[index].name),
                                const Icon(
                                  Icons.arrow_forward_ios,
                                  color: ColorsManager.greyColor,
                                  size: 15,
                                )
                              ],
                            ),
                            Container(
                              margin: const EdgeInsets.symmetric(vertical: 15),
                              height: 1,
                              color: ColorsManager.greyColor,
                            ),
                          ],
                        ),
                      );
                    }),
              ));
  }
}
