import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/ac/bloc/ac_bloc.dart';
import 'package:syncrow_web/pages/device_managment/ac/bloc/ac_event.dart';
import 'package:syncrow_web/pages/device_managment/ac/bloc/ac_state.dart';
import 'package:syncrow_web/pages/device_managment/ac/factories/ac_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/batch_control_list/batch_ac_mode.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/batch_control_list/batch_current_temp.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/batch_control_list/batch_fan_speed.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/toggle_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  final List<String> devicesIds;

  @override
  Widget build(BuildContext context) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return BlocProvider(
      create: (context) => AcBlocFactory.create(
        deviceId: devicesIds.first,
      )..add(AcFetchBatchStatusEvent(devicesIds)),
      child: BlocBuilder<AcBloc, AcsState>(
        builder: (context, state) {
          if (state is ACStatusLoaded) {
            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: [
                ToggleWidget(
                  deviceId: devicesIds.first,
                  code: 'switch',
                  value: state.status.acSwitch,
                  label: 'Thermostat',
                  icon: Assets.ac,
                  onChange: (value) {
                    context.read<AcBloc>().add(AcBatchControlEvent(
                          devicesIds: devicesIds,
                          code: 'switch',
                          value: value,
                        ));
                  },
                ),
                BatchCurrentTemp(
                  currentTemp: state.status.currentTemp,
                  tempSet: state.status.tempSet,
                  code: 'temp_set',
                  devicesIds: devicesIds,
                  isBatch: true,
                ),
                BatchAcMode(
                  value: state.status.acMode,
                  code: 'mode',
                  devicesIds: devicesIds,
                ),
                BatchFanSpeedControl(
                  value: state.status.acFanSpeed,
                  code: 'level',
                  devicesIds: devicesIds,
                ),
                ToggleWidget(
                  label: '',
                  labelWidget: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      IconButton(
                        onPressed: () {},
                        icon: const Icon(
                          Icons.remove,
                          size: 28,
                          color: ColorsManager.greyColor,
                        ),
                      ),
                      Text(
                        '06',
                        style: context.textTheme.titleLarge!.copyWith(
                          color: ColorsManager.dialogBlueTitle,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text(
                        'h',
                        style: context.textTheme.bodySmall!
                            .copyWith(color: ColorsManager.blackColor),
                      ),
                      Text(
                        '30',
                        style: context.textTheme.titleLarge!.copyWith(
                          color: ColorsManager.dialogBlueTitle,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Text('m',
                          style: context.textTheme.bodySmall!
                              .copyWith(color: ColorsManager.blackColor)),
                      IconButton(
                        onPressed: () {},
                        icon: const Icon(
                          Icons.add,
                          size: 28,
                          color: ColorsManager.greyColor,
                        ),
                      ),
                    ],
                  ),
                  value: false,
                  code: 'ac_schedule',
                  deviceId: devicesIds.first,
                  icon: Assets.acSchedule,
                  onChange: (value) {},
                ),
                ToggleWidget(
                  deviceId: devicesIds.first,
                  code: 'child_lock',
                  value: state.status.childLock,
                  label: 'Child Lock',
                  icon: state.status.childLock ? Assets.acLock : Assets.unlock,
                  onChange: (value) {
                    context.read<AcBloc>().add(AcBatchControlEvent(
                          devicesIds: devicesIds,
                          code: 'child_lock',
                          value: value,
                        ));
                  },
                ),
                // FirmwareUpdateWidget(deviceId: devicesIds.first, version: 5),
                FactoryResetWidget(
                  callFactoryReset: () {
                    context.read<AcBloc>().add(AcFactoryResetEvent(
                          deviceId: state.status.uuid,
                          factoryResetModel:
                              FactoryResetModel(devicesUuid: devicesIds),
                        ));
                  },
                ),
              ],
            );
          } else if (state is AcsLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else {
            return const Center(child: Text('Error fetching status'));
          }
        },
      ),
    );
  }
}
