import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/bloc/curtain_module_bloc.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/widgets/curtain_movment_widget.dart';
import 'package:syncrow_web/pages/device_managment/curtain_module/widgets/prefrences_dialog.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/icon_name_status_container.dart';
import 'package:syncrow_web/services/batch_control_devices_service.dart';
import 'package:syncrow_web/services/control_device_service.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/helpers/responsice_layout_helper/responsive_layout_helper.dart';

class CurtainModuleItems extends StatelessWidget with HelperResponsiveLayout {
  final String deviceId;
  const CurtainModuleItems({
    super.key,
    required this.deviceId,
  });

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => CurtainModuleBloc(
          controlDeviceService: RemoteControlDeviceService(),
          batchControlDevicesService: RemoteBatchControlDevicesService())
        ..add(FetchCurtainModuleStatusEvent(deviceId: deviceId)),
      child: BlocBuilder<CurtainModuleBloc, CurtainModuleState>(
        builder: (context, state) {
          return _buildStatusControls(context);
        },
      ),
    );
  }

  Widget _buildStatusControls(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 30),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ControlCurtainMovementWidget(
            devicesId: [deviceId],
          ),
          const SizedBox(
            height: 10,
          ),
          SizedBox(
            height: 140,
            width: 350,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: ScheduleControlButton(
                    onTap: () {
                      showDialog<void>(
                          context: context,
                          builder: (ctx) => BlocProvider.value(
                                value:
                                    BlocProvider.of<CurtainModuleBloc>(context),
                                child: BuildScheduleView(
                                  deviceUuid: deviceId,
                                  category: 'Timer',
                                  code: 'control',
                                  countdownCode: 'Timer',
                                  deviceType: 'CUR_2',
                                ),
                              ));
                    },
                    mainText: '',
                    subtitle: 'Scheduling',
                    iconPath: Assets.scheduling,
                  ),
                ),
                const SizedBox(
                  width: 10,
                ),
                Expanded(
                  child: BlocBuilder<CurtainModuleBloc, CurtainModuleState>(
                    builder: (context, state) {
                      if (state is CurtainModuleLoading) {
                        return const Center(
                          child: CircularProgressIndicator(),
                        );
                      } else if (state is CurtainModuleStatusLoaded) {
                        return IconNameStatusContainer(
                          isFullIcon: false,
                          name: 'Preferences',
                          icon: Assets.preferences,
                          onTap: () => showDialog(
                            context: context,
                            builder: (_) => BlocProvider.value(
                              value: context.read<CurtainModuleBloc>(),
                              child: CurtainModulePrefrencesDialog(
                                curtainModuleBloc:
                                    context.watch<CurtainModuleBloc>(),
                                deviceId: deviceId,
                                curtainModuleStatusModel:
                                    state.curtainModuleStatus,
                              ),
                            ),
                          ),
                          status: false,
                          textColor: ColorsManager.blackColor,
                        );
                      } else {
                        return const SizedBox();
                      }
                    },
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
