import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/routines/bloc/create_routine_bloc/create_routine_bloc.dart';
import 'package:syncrow_web/pages/routines/bloc/create_routine_bloc/create_routine_event.dart';
import 'package:syncrow_web/pages/routines/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routines/create_new_routines/create_new_routines.dart';
import 'package:syncrow_web/pages/routines/view/create_new_routine_view.dart';
import 'package:syncrow_web/pages/routines/widgets/main_routine_view/fetch_routine_scenes_automation.dart';
import 'package:syncrow_web/pages/routines/widgets/main_routine_view/routine_view_card.dart';
import 'package:syncrow_web/pages/space_tree/view/space_tree_view.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class RoutinesView extends StatefulWidget {
  const RoutinesView({super.key});

  @override
  State<RoutinesView> createState() => _RoutinesViewState();
}

class _RoutinesViewState extends State<RoutinesView> {
  void _handleRoutineCreation(BuildContext context) async {
    final result = await showDialog<Map<String, dynamic>>(
      context: context,
      builder: (context) => const CreateNewRoutinesDialog(),
    );

    if (result == null) return;
    final communityId = result['community'];
    final spaceId = result['space'];
    final bloc = BlocProvider.of<CreateRoutineBloc>(context);
    final routineBloc = context.read<RoutineBloc>();
    bloc.add(SaveCommunityIdAndSpaceIdEvent(
        communityID: communityId, spaceID: spaceId));
    await Future.delayed(const Duration(seconds: 1));
    routineBloc.add(const CreateNewRoutineViewEvent(createRoutineView: true));
  }

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<RoutineBloc, RoutineState>(
      builder: (context, state) {
        if (state.createRoutineView) {
          return const CreateNewRoutineView();
        }

        return Row(
          children: [
            Expanded(
              child: SpaceTreeView(
                onSelect: () => context.read<RoutineBloc>()
                  ..add(const LoadScenes())
                  ..add(const LoadAutomation())
                  ..add(FetchDevicesInRoutine()),
              ),
            ),
            Expanded(
              flex: 4,
              child: SizedBox(
                height: context.screenHeight,
                width: context.screenWidth,
                child: SingleChildScrollView(
                  padding: const EdgeInsetsDirectional.all(16),
                  child: Padding(
                    padding: const EdgeInsets.only(left: 20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      spacing: 16,
                      children: [
                        Text(
                          "Create New Routines",
                          style:
                              Theme.of(context).textTheme.titleLarge?.copyWith(
                                    color: ColorsManager.grayColor,
                                    fontWeight: FontWeight.bold,
                                  ),
                        ),
                        RoutineViewCard(
                          isLoading: false,
                          onChanged: (v) {},
                          status: '',
                          spaceId: '',
                          automationId: '',
                          communityId: '',
                          sceneId: '',
                          cardType: '',
                          spaceName: '',
                          onTap: () => _handleRoutineCreation(context),
                          icon: Icons.add,
                          textString: '',
                        ),
                        const FetchRoutineScenesAutomation(),
                      ],
                    ),
                  ),
                ),
              ),
            )
          ],
        );
      },
    );
  }
}
