import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_event.dart';
import 'package:syncrow_app/features/devices/bloc/curtain_bloc/curtain_state.dart';

import 'package:syncrow_app/features/devices/model/group_curtain_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 CurtainsList extends StatelessWidget {
  const CurtainsList(
      {super.key, required this.curtainsList, required this.allSwitches});

  final List<GroupCurtainModel> curtainsList;
  final bool allSwitches;

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CurtainBloc, CurtainState>(
      builder: (context, state) {
        return SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              const SizedBox(height: 10),
              const BodySmall(text: 'All Curtains'),
              const SizedBox(height: 5),
              DevicesDefaultSwitch(
                off: 'Close',
                on: 'Open',
                switchValue: allSwitches,
                action: () {
                  BlocProvider.of<CurtainBloc>(context).add(GroupAllOnEvent());
                },
                secondAction: () {
                  BlocProvider.of<CurtainBloc>(context).add(GroupAllOffEvent());
                },
              ),
              ListView.builder(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                padding: const EdgeInsets.all(0),
                itemCount: curtainsList.length,
                itemBuilder: (context, index) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const SizedBox(height: 10),
                      BodySmall(text: curtainsList[index].deviceName),
                      const SizedBox(height: 5),
                      DevicesDefaultSwitch(
                        off: 'close',
                        on: 'open',
                        switchValue: curtainsList[index].percentControl > 1,
                        action: () {
                          // Toggle between 'open' and 'close' based on current value
                          final newValue =
                              curtainsList[index].percentControl > 1 ? 0 : 100;
                          BlocProvider.of<CurtainBloc>(context).add(
                            ChangeFirstWizardSwitchStatusEvent(
                              value: newValue,
                              deviceId: curtainsList[index].deviceId,
                            ),
                          );
                        },
                      ),
                    ],
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
