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/schedule_device/schedule_widgets/schedual_view.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/schedule_widgets/schedule_control_button.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/pages/device_managment/two_gang_switch/bloc/two_gang_switch_bloc.dart';
import 'package:syncrow_web/pages/device_managment/two_gang_switch/bloc/two_gang_switch_event.dart';
import 'package:syncrow_web/pages/device_managment/two_gang_switch/bloc/two_gang_switch_state.dart';
import 'package:syncrow_web/pages/device_managment/two_gang_switch/factories/two_gang_switch_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/two_gang_switch/models/two_gang_status_model.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

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

  final List<String> deviceIds;

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) =>
          TwoGangSwitchBlocFactory.create(deviceId: deviceIds.first)
            ..add(TwoGangSwitchFetchBatchEvent(deviceIds)),
      child: BlocBuilder<TwoGangSwitchBloc, TwoGangSwitchState>(
        builder: (context, state) {
          if (state is TwoGangSwitchLoading) {
            return const Center(child: CircularProgressIndicator());
          } else if (state is TwoGangSwitchStatusLoaded) {
            return _buildStatusControls(context, state.status);
          } else if (state is TwoGangSwitchError ||
              state is TwoGangSwitchControlError) {
            return const Center(child: Text('Error fetching status'));
          } else {
            return const Center(child: CircularProgressIndicator());
          }
        },
      ),
    );
  }

  Widget _buildStatusControls(BuildContext context, TwoGangStatusModel status) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    return SizedBox(
      child: 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<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
                    deviceId: deviceIds,
                    code: 'switch_1',
                    value: value,
                  ));
            },
          ),
          ToggleWidget(
            value: status.switch2,
            code: 'switch_2',
            deviceId: deviceIds.first,
            label: 'Ceiling Light',
            onChange: (value) {
              context.read<TwoGangSwitchBloc>().add(TwoGangSwitchBatchControl(
                    deviceId: deviceIds,
                    code: 'switch_2',
                    value: value,
                  ));
            },
          ),
          ScheduleControlButton(
            onTap: () {
              showDialog<void>(
                  context: context,
                  builder: (ctx) => BlocProvider.value(
                        value: BlocProvider.of<TwoGangSwitchBloc>(context),
                        child: BuildScheduleView(
                          category: 'switch_1',
                          deviceUuid: deviceIds.first,
                          countdownCode: 'countdown_1',
                          deviceType: '2G',
                        ),
                      ));
            },
            mainText: 'Wall Light',
            subtitle: 'Scheduling',
            iconPath: Assets.scheduling,
          ),

          ScheduleControlButton(
            onTap: () {
              showDialog<void>(
                  context: context,
                  builder: (ctx) => BlocProvider.value(
                        value: BlocProvider.of<TwoGangSwitchBloc>(context),
                        child: BuildScheduleView(
                          category: 'switch_2',
                          deviceUuid: deviceIds.first,
                          countdownCode: 'countdown_2',
                          deviceType: '2G',
                        ),
                      ));
            },
            mainText: 'Ceiling Light',
            subtitle: 'Scheduling',
            iconPath: Assets.scheduling,
          ),
  
          FactoryResetWidget(callFactoryReset: () {
            context.read<TwoGangSwitchBloc>().add(
                  TwoGangFactoryReset(
                    deviceId: status.uuid,
                    factoryReset: FactoryResetModel(devicesUuid: deviceIds),
                  ),
                );
          }),
        ],
      ),
    );
  }
}
