import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_gang_bloc/two_gang_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_gang_bloc/two_gang_event.dart';
import 'package:syncrow_app/features/devices/bloc/two_gang_bloc/two_gang_state.dart';
import 'package:syncrow_app/features/devices/model/group_two_gang_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 TwoGangList extends StatelessWidget {
  const TwoGangList(
      {super.key, required this.twoGangList, required this.allSwitches});

  final List<GroupTwoGangModel> twoGangList;
  final bool allSwitches;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<TwoGangBloc, TwoGangState>(
      builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const SizedBox(height: 10),
              const BodySmall(text: 'All Lights'),
              const SizedBox(height: 5),
              DevicesDefaultSwitch(
                off: 'OFF',
                on: 'ON',
                switchValue: allSwitches,
                action: () {
                  BlocProvider.of<TwoGangBloc>(context).add(GroupAllOnEvent());
                },
                secondAction: () {
                  BlocProvider.of<TwoGangBloc>(context).add(GroupAllOffEvent());
                },
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: const EdgeInsets.all(0),
                itemCount: twoGangList.length,
                itemBuilder: (context, index) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 10),
                      BodySmall(text: twoGangList[index].deviceName),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'OFF',
                        on: 'ON',
                        switchValue: twoGangList[index].firstSwitch,
                        action: () {
                          BlocProvider.of<TwoGangBloc>(context).add(
                              ChangeFirstWizardSwitchStatusEvent(
                                  value: twoGangList[index].firstSwitch,
                                  deviceId: twoGangList[index].deviceId));
                        },
                      ),
                      const SizedBox(height: 10),
                      BodySmall(text: twoGangList[index].deviceName),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'OFF',
                        on: 'ON',
                        switchValue: twoGangList[index].secondSwitch,
                        action: () {
                          BlocProvider.of<TwoGangBloc>(context).add(
                              ChangeSecondWizardSwitchStatusEvent(
                                  value: twoGangList[index].secondSwitch,
                                  deviceId: twoGangList[index].deviceId));
                        },
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
