import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/one_touch_bloc/one_touch_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/one_touch_bloc/one_touch_event.dart';
import 'package:syncrow_app/features/devices/bloc/one_touch_bloc/one_touch_state.dart';

import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_one_touch_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/one_touch/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 OneTouchWizard extends StatelessWidget {
  const OneTouchWizard({super.key, this.device});

  final DeviceModel? device;

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

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

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