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

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

  final DeviceModel? device;

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

    return DefaultScaffold(
        title: 'Curtain',
        child: BlocProvider(
          create: (context) =>
              CurtainBloc(device?.uuid ?? '')..add(InitialWizardEvent()),
          child: BlocBuilder<CurtainBloc, CurtainState>(
            builder: (context, state) {
              bool allSwitchesOn = false;
              if (state is UpdateGroupState) {
                groupOneTouchModel = state.curtainList;
                allSwitchesOn = state.allSwitches;
              }
              return state is LoadingInitialState
                  ? const Center(
                      child: DefaultContainer(
                          width: 50,
                          height: 50,
                          child: CircularProgressIndicator()),
                    )
                  : CurtainsList(
                      curtainsList: groupOneTouchModel,
                      allSwitches: allSwitchesOn,
                    );
            },
          ),
        ));
  }
}
