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/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_two_gang_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/two_gang/two_gang_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

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

  final DeviceModel? device;

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

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

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