import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/device_managment/schedule_device/bloc/schedule_bloc.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/water_heater_status_model.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/extension/build_context_x.dart';

class ScheduleModeSelector extends StatelessWidget {
  final ScheduleModes currentMode;
  final String countdownCode;

  const ScheduleModeSelector({
    super.key,
    required this.currentMode,
    required this.countdownCode,
  });

  @override
  Widget build(BuildContext context) {
    final currentMode = context.select<ScheduleBloc, ScheduleModes>(
      (bloc) => bloc.state is ScheduleLoaded &&
              (bloc.state as ScheduleLoaded).scheduleMode != null
          ? (bloc.state as ScheduleLoaded).scheduleMode
          : ScheduleModes.schedule,
    );
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          'Type:',
          style: context.textTheme.bodySmall!.copyWith(
            fontSize: 13,
            color: ColorsManager.grayColor,
          ),
        ),
        const SizedBox(height: 4),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            _buildRadioTile(
                context, 'Countdown', ScheduleModes.countdown, currentMode),
            _buildRadioTile(
                context, 'Schedule', ScheduleModes.schedule, currentMode),
            // _buildRadioTile(
            //     context, 'Circulate', ScheduleModes.circulate, currentMode),
            // _buildRadioTile(
            //     context, 'Inching', ScheduleModes.inching, currentMode),
          ],
        ),
      ],
    );
  }

  Widget _buildRadioTile(
    BuildContext context,
    String label,
    ScheduleModes mode,
    ScheduleModes currentMode,
  ) {
    return Flexible(
      child: ListTile(
        contentPadding: EdgeInsets.zero,
        title: Text(
          label,
          style: context.textTheme.bodySmall!.copyWith(
            fontSize: 13,
            color: ColorsManager.blackColor,
          ),
        ),
        leading: Radio<ScheduleModes>(
          value: mode,
          groupValue: currentMode,
          onChanged: (ScheduleModes? value) {
            if (value != null) {
              context.read<ScheduleBloc>().add(
                    UpdateScheduleModeEvent(
                        scheduleMode: value, countdownCode: countdownCode),
                  );
              if (value == ScheduleModes.schedule) {
                context.read<ScheduleBloc>().add(
                      const ScheduleGetEvent(category: 'switch_1'),
                    );
              }
            }
          },
        ),
      ),
    );
  }
}
