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

class ScheduleModeSelector extends StatelessWidget {
  final WaterHeaterDeviceStatusLoaded state;

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

  @override
  Widget build(BuildContext context) {
    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, state),
            _buildRadioTile(context, 'Schedule', ScheduleModes.schedule, state),
            _buildRadioTile(
                context, 'Circulate', ScheduleModes.circulate, state),
            _buildRadioTile(context, 'Inching', ScheduleModes.inching, state),
          ],
        ),
      ],
    );
  }

  Widget _buildRadioTile(BuildContext context, String label, ScheduleModes mode,
      WaterHeaterDeviceStatusLoaded state) {
    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: state.scheduleMode,
          onChanged: (ScheduleModes? value) {
            if (value != null) {
              if (value == ScheduleModes.countdown) {
                context.read<WaterHeaterBloc>().add(UpdateScheduleEvent(
                      scheduleMode: value,
                      hours: state.countdownHours ?? 0,
                      minutes: state.countdownMinutes ?? 0,
                    ));
              } else if (value == ScheduleModes.inching) {
                context.read<WaterHeaterBloc>().add(UpdateScheduleEvent(
                      scheduleMode: value,
                      hours: state.inchingHours ?? 0,
                      minutes: state.inchingMinutes ?? 0,
                    ));
              }

              if (value == ScheduleModes.schedule) {
                context.read<WaterHeaterBloc>().add(
                      GetSchedulesEvent(
                        category: 'switch_1',
                        uuid: state.status.uuid,
                      ),
                    );
              }
            }
          },
        ),
      ),
    );
  }
}
