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

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

  final DeviceModel? device;

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

    return DefaultScaffold(
        title: 'Water Heater',
        child: BlocProvider(
          create: (context) =>
              WaterHeaterBloc(switchCode: '', whId: device?.uuid ?? '')..add(InitialWizardEvent()),
          child: BlocBuilder<WaterHeaterBloc, WaterHeaterState>(
            builder: (context, state) {
              bool allSwitchesOn = false;

              if (state is UpdateGroupState) {
                groupModel = state.twoGangList;
                allSwitchesOn = state.allSwitches;
              }
              return state is WHLoadingState
                  ? const Center(
                      child: DefaultContainer(
                          width: 50, height: 50, child: CircularProgressIndicator()),
                    )
                  : WHList(
                      whList: groupModel,
                      allSwitches: allSwitchesOn,
                    );
            },
          ),
        ));
  }
}
