import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_bloc.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_event.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/bloc/garage_door_state.dart';
import 'package:syncrow_web/pages/device_managment/garage_door/models/garage_door_model.dart';
import 'package:syncrow_web/pages/device_managment/shared/batch_control/factory_reset.dart';
// import 'package:syncrow_web/pages/device_managment/shared/batch_control/firmware_update.dart';
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class GarageDoorBatchControlView extends StatelessWidget with HelperResponsiveLayout {
  final List<String> deviceIds;

  const GarageDoorBatchControlView({Key? key, required this.deviceIds}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => GarageDoorBloc(deviceId: deviceIds.first)
        ..add(GarageDoorFetchBatchStatusEvent(deviceIds)),
      child: BlocBuilder<GarageDoorBloc, GarageDoorState>(
        builder: (context, state) {
          if (state is GarageDoorLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is GarageDoorBatchStatusLoaded) {
            return _buildStatusControls(context, state.status);
          } else if (state is GarageDoorBatchControlError) {
            return Center(child: Text('Error: ${state.message}'));
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

  Widget _buildStatusControls(BuildContext context, GarageDoorStatusModel status) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 50),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge
            ? 3
            : isMedium
                ? 2
                : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        ToggleWidget(
          value: status.switch1,
          code: 'switch_1',
          deviceId: deviceIds.first,
          label: 'Garage Door',
          icon: status.switch1 ? Assets.openedDoor : Assets.closedDoor,
          onChange: (value) {
            context.read<GarageDoorBloc>().add(
                  GarageDoorBatchControlEvent(
                    deviceIds: deviceIds,
                    code: 'switch_1',
                    value: value,
                  ),
                );
          },
        ),
        // FirmwareUpdateWidget(
        //   deviceId: deviceIds.first,
        //   version: 12,
        // ),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<GarageDoorBloc>().add(
                  GarageDoorFactoryResetEvent(
                    deviceId: deviceIds.first,
                    factoryReset: FactoryResetModel(devicesUuid: deviceIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
