import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/common/curtain_toggle.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/factory_reset_model.dart';
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_bloc.dart';
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_event.dart';
import 'package:syncrow_web/pages/device_managment/curtain/bloc/curtain_state.dart';
import 'package:syncrow_web/pages/device_managment/curtain/factories/curtain_bloc_factory.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/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class CurtainBatchStatusView extends StatelessWidget with HelperResponsiveLayout {
  const CurtainBatchStatusView({super.key, required this.devicesIds});

  final List<String> devicesIds;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) =>
          CurtainBlocFactory.create(deviceId: devicesIds.first)..add(CurtainFetchBatchStatus(devicesIds)),
      child: BlocBuilder<CurtainBloc, CurtainState>(
        builder: (context, state) {
          if (state is CurtainStatusLoading) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is CurtainStatusLoaded) {
            return _buildStatusControls(context, state.status);
          } else if (state is CurtainError || state is CurtainControlError) {
            return const Center(child: Text('Error fetching status'));
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

  Widget _buildStatusControls(BuildContext context, bool status) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 50, vertical: 20),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge
            ? 3
            : isMedium
                ? 2
                : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        CurtainToggle(
          value: status,
          code: 'control',
          deviceId: devicesIds.first,
          label: 'Curtains',
          onChanged: (value) {
            context.read<CurtainBloc>().add(CurtainBatchControl(
                  devicesIds: devicesIds,
                  code: 'control',
                  value: value,
                ));
          },
        ),
        // FirmwareUpdateWidget(deviceId: devicesIds.first, version: 5),
        FactoryResetWidget(
          callFactoryReset: () {
            context.read<CurtainBloc>().add(
                  CurtainFactoryReset(
                    deviceId: devicesIds.first,
                    factoryReset: FactoryResetModel(devicesUuid: devicesIds),
                  ),
                );
          },
        ),
      ],
    );
  }
}
