import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_bloc.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_state.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/helper/garage_door_helper.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/models/garage_door_model.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/schedule_view/schedule_garage_header.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/schedule_view/schedule_garage_managment_ui.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/schedule_view/schedule_garage_mode_buttons.dart';

class BuildGarageDoorScheduleView extends StatefulWidget {
  const BuildGarageDoorScheduleView({super.key, required this.status});

  final GarageDoorStatusModel status;

  @override
  State<BuildGarageDoorScheduleView> createState() => _BuildScheduleViewState();
}

class _BuildScheduleViewState extends State<BuildGarageDoorScheduleView> {
  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<GarageDoorBloc>(context);

    return BlocProvider.value(
      value: bloc,
      child: Dialog(
        backgroundColor: Colors.white,
        insetPadding: const EdgeInsets.all(20),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(20),
        ),
        child: SizedBox(
          width: 700,
          child: SingleChildScrollView(
            child: Padding(
              padding:
                  const EdgeInsets.symmetric(horizontal: 40.0, vertical: 20),
              child: BlocBuilder<GarageDoorBloc, GarageDoorState>(
                builder: (context, state) {
                  if (state is GarageDoorLoadedState) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const ScheduleGarageHeader(),
                        const SizedBox(height: 20),
                        ScheduleGarageManagementUI(
                          state: state,
                          onAddSchedule: () {
                            GarageDoorDialogHelper
                                .showAddGarageDoorScheduleDialog(context,
                                    schedule: null, index: null, isEdit: false);
                          },
                        ),
                        const SizedBox(height: 20),
                        ScheduleGarageModeButtons(
                          onSave: () {
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    );
                  }
                  if (state is ScheduleGarageLoadingState) {
                    return SizedBox(
                        height: 200,
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            const ScheduleGarageHeader(),
                            const SizedBox(
                              height: 50,
                            ),
                            const Center(child: CircularProgressIndicator()),
                            const SizedBox(
                              height: 20,
                            ),
                            ScheduleGarageModeButtons(
                              onSave: () {},
                            ),
                          ],
                        ));
                  }
                  return const SizedBox(
                    height: 200,
                    child: ScheduleGarageHeader(),
                  );
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}
