import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.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/toggle_widget.dart';
import 'package:syncrow_web/pages/device_managment/two_g_glass_switch/bloc/two_gang_glass_switch_bloc.dart';
import 'package:syncrow_web/pages/device_managment/two_g_glass_switch/factories/two_gang_glass_switch_bloc_factory.dart';
import 'package:syncrow_web/pages/device_managment/two_g_glass_switch/models/two_gang_glass_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 TwoGangGlassSwitchControlView extends StatelessWidget
    with HelperResponsiveLayout {
  final String deviceId;

  const TwoGangGlassSwitchControlView({required this.deviceId, super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
        create: (context) =>
            TwoGangGlassSwitchBlocFactory.create(deviceId: deviceId)
              ..add(TwoGangGlassSwitchFetchDeviceEvent(deviceId)),
        child: BlocBuilder<TwoGangGlassSwitchBloc, TwoGangGlassSwitchState>(
          builder: (context, state) {
            if (state is TwoGangGlassSwitchLoading) {
              return const Center(child: CircularProgressIndicator());
            } else if (state is TwoGangGlassSwitchStatusLoaded) {
              return _buildStatusControls(context, state.status);
            } else if (state is TwoGangGlassSwitchError) {
              return Center(child: Text(state.message));
            } else {
              return const Center(child: CircularProgressIndicator());
            }
          },
        ));
  }

  Widget _buildStatusControls(
      BuildContext context, TwoGangGlassStatusModel status) {
    final isExtraLarge = isExtraLargeScreenSize(context);
    final isLarge = isLargeScreenSize(context);
    final isMedium = isMediumScreenSize(context);
    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: [
        ToggleWidget(
          value: status.switch1,
          code: 'switch_1',
          deviceId: deviceId,
          label: 'Wall Light',
          onChange: (value) {
            context.read<TwoGangGlassSwitchBloc>().add(
                  TwoGangGlassSwitchControl(
                    deviceId: deviceId,
                    code: 'switch_1',
                    value: value,
                  ),
                );
          },
        ),
        ToggleWidget(
          value: status.switch2,
          code: 'switch_2',
          deviceId: deviceId,
          label: 'Ceiling Light',
          onChange: (value) {
            context.read<TwoGangGlassSwitchBloc>().add(
                  TwoGangGlassSwitchControl(
                    deviceId: deviceId,
                    code: 'switch_2',
                    value: value,
                  ),
                );
          },
        ),
        ToggleWidget(
          value: false,
          code: '',
          deviceId: deviceId,
          label: 'Preferences',
          icon: Assets.preferences,
          onChange: (value) {},
          showToggle: false,
        ),
        ScheduleControlButton(
          onTap: () {
            showDialog<void>(
                context: context,
                builder: (ctx) => BlocProvider.value(
                      value: BlocProvider.of<TwoGangGlassSwitchBloc>(context),
                      child: BuildScheduleView(
                        deviceType: '2GT',
                        countdownCode: 'countdown_1',
                        deviceUuid: deviceId,
                        category: 'switch_1',
                      ),
                    ));
          },
          mainText: 'Wall Light',
          subtitle: 'Scheduling',
          iconPath: Assets.scheduling,
        ),
        ScheduleControlButton(
          onTap: () {
            showDialog<void>(
                context: context,
                builder: (ctx) => BlocProvider.value(
                      value: BlocProvider.of<TwoGangGlassSwitchBloc>(context),
                      child: BuildScheduleView(
                        deviceType: '2GT',
                        countdownCode: 'countdown_2',
                        deviceUuid: deviceId,
                        category: 'switch_2',
                      ),
                    ));
          },
          mainText: 'Ceiling Light',
          subtitle: 'Scheduling',
          iconPath: Assets.scheduling,
        ),
      ],
    );
  }
}
