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/view/control_list/ac_mode.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/control_list/ac_toggle.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/control_list/current_temp.dart';
import 'package:syncrow_web/pages/device_managment/ac/view/control_list/fan_speed.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/devices_model.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  final AllDevicesModel device;

  @override
  Widget build(BuildContext context) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return BlocProvider(
      create: (context) => AcBloc(deviceId: device.uuid!)
        ..add(AcFetchDeviceStatus(device.uuid!)),
      child: BlocBuilder<AcBloc, AcsState>(
        builder: (context, state) {
          if (state is ACStatusLoaded) {
            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: [
                AcToggle(
                  value: state.status.acSwitch,
                  code: 'switch',
                  deviceId: device.uuid!,
                ),
                CurrentTemp(
                  currentTemp: state.status.currentTemp,
                  tempSet: state.status.tempSet,
                  code: 'temp_set',
                  deviceId: device.uuid!,
                ),
                AcMode(
                  value: state.status.acMode,
                  code: 'mode',
                  deviceId: device.uuid!,
                ),
                FanSpeedControl(
                  value: state.status.acFanSpeed,
                  code: 'level',
                  deviceId: device.uuid!,
                ),
                AcToggle(
                  value: state.status.childLock,
                  code: 'child_lock',
                  deviceId: device.uuid!,
                  description: 'Child Lock',
                  icon:
                      state.status.childLock ? Assets.childLock:Assets.unlock ,
                ),
              ],
            );
          } else if (state is AcsLoadingState) {
            return const Center(child: CircularProgressIndicator());
          } else {
            return const Center(child: Text('Error fetching status'));
          }
        },
      ),
    );
  }
}
