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/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/pages/device_managment/three_g_glass_switch/bloc/three_gang_glass_switch_bloc.dart';
import 'package:syncrow_web/pages/device_managment/three_g_glass_switch/factories/three_gang_glass_switch_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/three_g_glass_switch/models/three_gang_glass_switch.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  const ThreeGangGlassSwitchBatchControlView({required this.deviceIds, super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => ThreeGangGlassSwitchBlocFactory.create(deviceId: deviceIds.first)
        ..add(ThreeGangGlassSwitchFetchBatchStatusEvent(deviceIds)),
      child: BlocBuilder<ThreeGangGlassSwitchBloc, ThreeGangGlassSwitchState>(
        builder: (context, state) {
          if (state is ThreeGangGlassSwitchLoading) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is ThreeGangGlassSwitchBatchStatusLoaded) {
            return _buildStatusControls(context, state.status);
          } else if (state is ThreeGangGlassSwitchError) {
            return const Center(child: Text('Error fetching status'));
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

  Widget _buildStatusControls(BuildContext context, ThreeGangGlassStatusModel 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: "Wall Light",
          onChange: (value) {
            context.read<ThreeGangGlassSwitchBloc>().add(
                  ThreeGangGlassSwitchBatchControl(
                    deviceIds: deviceIds,
                    code: 'switch_1',
                    value: value,
                  ),
                );
          },
        ),
        ToggleWidget(
          value: status.switch2,
          code: 'switch_2',
          deviceId: deviceIds.first,
          label: "Ceiling Light",
          onChange: (value) {
            context.read<ThreeGangGlassSwitchBloc>().add(
                  ThreeGangGlassSwitchBatchControl(
                    deviceIds: deviceIds,
                    code: 'switch_2',
                    value: value,
                  ),
                );
          },
        ),
        ToggleWidget(
          value: status.switch3,
          code: 'switch_3',
          deviceId: deviceIds.first,
          label: "SpotLight",
          onChange: (value) {
            context.read<ThreeGangGlassSwitchBloc>().add(
                  ThreeGangGlassSwitchBatchControl(
                    deviceIds: deviceIds,
                    code: 'switch_3',
                    value: value,
                  ),
                );
          },
        ),
        // FirmwareUpdateWidget(
        //   deviceId: deviceIds.first,
        //   version: 12, // adjust the version according to your requirement
        // ),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<ThreeGangGlassSwitchBloc>().add(
                  ThreeGangGlassFactoryReset(
                    deviceId: status.uuid,
                    factoryReset: FactoryResetModel(devicesUuid: deviceIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
