import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_event.dart';
import 'package:syncrow_app/features/devices/bloc/two_touch_bloc/two_touch_state.dart';

import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_two_touch_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/two_touch/two_touch_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

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

  final DeviceModel? device;

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

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

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