import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/water_heater_bloc/water_heater_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/water_heater_bloc/water_heater_event.dart';
import 'package:syncrow_app/features/devices/bloc/water_heater_bloc/water_heater_state.dart';
import 'package:syncrow_app/features/devices/model/GroupWHModel.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 WHList extends StatelessWidget {
  const WHList({
    required this.whList,
    required this.allSwitches,
    super.key,
  });

  final List<GroupWHModel> whList;
  final bool allSwitches;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<WaterHeaterBloc, WaterHeaterState>(
      builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const SizedBox(height: 10),
              const BodySmall(text: 'All Water Heaters'),
              const SizedBox(height: 5),
              DevicesDefaultSwitch(
                off: 'OFF',
                on: 'ON',
                switchValue: allSwitches,
                action: () => context.read<WaterHeaterBloc>().add(
                      GroupAllOnEvent(),
                    ),
                secondAction: () => context.read<WaterHeaterBloc>().add(
                      GroupAllOffEvent(),
                    ),
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: EdgeInsetsDirectional.zero,
                itemCount: whList.length,
                itemBuilder: (context, index) {
                  final waterHeater = whList[index];
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 10),
                      BodySmall(text: waterHeater.deviceName),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'OFF',
                        on: 'ON',
                        switchValue: waterHeater.firstSwitch,
                        action: () => context.read<WaterHeaterBloc>().add(
                              ChangeFirstWizardSwitchStatusEvent(
                                value: waterHeater.firstSwitch,
                                deviceId: waterHeater.deviceId,
                              ),
                            ),
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
