import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_event.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_three_gang_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/three_gang/three_gang_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

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

  final DeviceModel? device;

  @override
  Widget build(BuildContext context) {
    return DefaultScaffold(
        title: 'Lights',
        child: BlocProvider(
          create: (context) =>
              ThreeGangBloc(switchCode: '', threeGangId: device?.uuid ?? '')
                ..add(InitialEvent(groupScreen: device != null ? false : true)),
          child: BlocBuilder<ThreeGangBloc, ThreeGangState>(
            builder: (context, state) {
              List<GroupThreeGangModel> groupThreeGangModel = [];
              bool allSwitchesOn = false;

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