import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_control_button.dart';
import 'package:syncrow_web/pages/device_managment/shared/device_controls_container.dart';
import 'package:syncrow_web/pages/device_managment/shared/toggle_widget.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/bloc/water_heater_bloc.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/factories/water_heater_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedual_view.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 WaterHeaterDeviceControlView extends StatelessWidget
    with HelperResponsiveLayout {
  const WaterHeaterDeviceControlView({super.key, required this.device});

  final AllDevicesModel device;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) => WaterHeaterBlocFactory.create(
              deviceId: device.uuid ?? '',
            )..add(WaterHeaterFetchStatusEvent(device.uuid!)),
        child: BlocBuilder<WaterHeaterBloc, WaterHeaterState>(
          builder: (context, state) {
            if (state is WaterHeaterLoadingState) {
              return const Center(child: CircularProgressIndicator());
            } else if (state is WaterHeaterDeviceStatusLoaded) {
              return _buildStatusControls(context, state.status);
            } else if (state is WaterHeaterFailedState ||
                state is WaterHeaterBatchFailedState) {
              return const Center(child: Text('Error fetching status'));
            } else {
              return const SizedBox(
                  height: 200, child: Center(child: SizedBox()));
            }
          },
        ));
  }

  Widget _buildStatusControls(
    BuildContext context,
    WaterHeaterStatusModel status,
  ) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return GridView(
      padding: const EdgeInsets.symmetric(horizontal: 150),
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: isLarge || isExtraLarge || isMedium ? 2 : 1,
        mainAxisExtent: 140,
        crossAxisSpacing: 12,
        mainAxisSpacing: 12,
      ),
      children: [
        ToggleWidget(
          deviceId: device.uuid!,
          code: 'switch_1',
          value: status.heaterSwitch,
          icon: Assets.waterHeater,
          label: 'Water Heater',
          onChange: (value) {
            context.read<WaterHeaterBloc>().add(ToggleWaterHeaterEvent(
                  deviceId: device.uuid!,
                  code: 'switch_1',
                  value: value,
                ));
          },
        ),
        ScheduleControlButton(
          onTap: () {
            showDialog<void>(
                context: context,
                builder: (ctx) => BlocProvider.value(
                      value: BlocProvider.of<WaterHeaterBloc>(context),
                      child: BuildScheduleView(
                        deviceUuid: device.uuid ?? '',
                        category: 'switch_1',
                        countdownCode: 'countdown_1',
                        deviceType: 'WH',
                      ),
                    ));
          },
          mainText: '',
          subtitle: 'Scheduling',
          iconPath: Assets.scheduling,
        ),
      ],
    );
  }
}
