import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_touch_bloc/three_touch_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/three_touch_bloc/three_touch_event.dart';
import 'package:syncrow_app/features/devices/bloc/three_touch_bloc/three_touch_state.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/group_three_touch_model.dart';
import 'package:syncrow_app/features/devices/view/widgets/three_touch/three_touch_list.dart';
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
import 'package:syncrow_app/features/shared_widgets/default_scaffold.dart';

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

  final DeviceModel? device;

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

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