import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/one_gang_bloc/one_gang_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/one_gang_bloc/one_gang_event.dart';
import 'package:syncrow_app/features/devices/bloc/one_gang_bloc/one_gang_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_one_gang_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/one_gang/one_gang_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

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

  final DeviceModel? device;

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

    return DefaultScaffold(
        title: 'Lights',
        child: BlocProvider(
          create: (context) =>
              OneGangBloc(switchCode: '', oneGangId: device?.uuid ?? '')
                ..add(InitialWizardEvent()),
          child: BlocBuilder<OneGangBloc, OneGangState>(
            builder: (context, state) {
              bool allSwitchesOn = false;

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