import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/garage_door_bloc/garage_door_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/garage_door_bloc/garage_door_event.dart';
import 'package:syncrow_app/features/devices/bloc/garage_door_bloc/garage_door_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_garage_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/garage_door/garage_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

class GarageWizard extends StatelessWidget {
  const GarageWizard({super.key, this.device});

  final DeviceModel? device;

  @override
  Widget build(BuildContext context) {
    List<GroupGarageModel> groupGarageModel = [];

    return DefaultScaffold(
        title: 'Garage Door Opener',
        child: BlocProvider(
          create: (context) => GarageDoorBloc(GDId: device?.uuid ?? '')
            ..add(InitialWizardEvent()),
          child: BlocBuilder<GarageDoorBloc, GarageDoorSensorState>(
            builder: (context, state) {
              bool allSwitchesOn = false;

              if (state is UpdateGroupState) {
                groupGarageModel = state.garageList;
                allSwitchesOn = state.allSwitches;
              }
              return state is LoadingInitialState
                  ? const Center(
                      child: DefaultContainer(
                          width: 50,
                          height: 50,
                          child: CircularProgressIndicator()),
                    )
                  : GarageList(
                      garageList: groupGarageModel,
                      allSwitches: allSwitchesOn,
                    );
            },
          ),
        ));
  }
}
