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/group_garage_model.dart';
import 'package:syncrow_app/features/shared_widgets/devices_default_switch.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';

class GarageList extends StatelessWidget {
  const GarageList({super.key, required this.garageList, required this.allSwitches});

  final List<GroupGarageModel> garageList;
  final bool allSwitches;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<GarageDoorBloc, GarageDoorSensorState>(
      builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const SizedBox(height: 10),
              const BodySmall(text: 'All Garages'),
              const SizedBox(height: 5),
              DevicesDefaultSwitch(
                off: 'Close',
                on: 'Open',
                switchValue: allSwitches,
                action: () => BlocProvider.of<GarageDoorBloc>(context).add(
                  GroupAllOnEvent(),
                ),
                secondAction: () => BlocProvider.of<GarageDoorBloc>(context).add(
                  GroupAllOffEvent(),
                ),
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: EdgeInsetsDirectional.zero,
                itemCount: garageList.length,
                itemBuilder: (context, index) {
                  final garageDoor = garageList[index];
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 10),
                      BodySmall(text: garageDoor.deviceName),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'Close',
                        on: 'Open',
                        switchValue: garageDoor.firstSwitch,
                        action: () => BlocProvider.of<GarageDoorBloc>(context).add(
                          ChangeFirstWizardSwitchStatusEvent(
                            value: garageDoor.firstSwitch,
                            deviceId: garageDoor.deviceId,
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
