import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/effective_period/effect_period_bloc.dart';
import 'package:syncrow_app/features/scene/bloc/effective_period/effect_period_event.dart';
import 'package:syncrow_app/features/scene/bloc/effective_period/effect_period_state.dart';
import 'package:syncrow_app/features/scene/enum/effective_period_options.dart';
import 'package:syncrow_app/features/scene/helper/effect_period_helper.dart';
import 'package:syncrow_app/utils/context_extension.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart';

class PeriodOptions extends StatelessWidget {
  final Future<List<String>?> Function(BuildContext) showCustomTimePicker;

  const PeriodOptions({
    required this.showCustomTimePicker,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<EffectPeriodBloc, EffectPeriodState>(
      builder: (context, state) {
        return Column(
          children: [
            _buildRadioOption(
                context, EnumEffectivePeriodOptions.allDay, '24 Hours'),
            _buildRadioOption(context, EnumEffectivePeriodOptions.daytime,
                'Sunrise to Sunset'),
            _buildRadioOption(
                context, EnumEffectivePeriodOptions.night, 'Sunset to Sunrise'),
            ListTile(
              contentPadding: EdgeInsets.zero,
              onTap: () => showCustomTimePicker(context),
              title: const BodyMedium(
                text: 'Custom',
              ),
              subtitle:
                  state.customStartTime != null && state.customEndTime != null
                      ? BodySmall(
                          text:
                              '${"${state.customStartTime}"} - ${"${state.customEndTime}"}',
                          style: context.bodySmall.copyWith(fontSize: 10),
                        )
                      : BodySmall(
                          text: '00:00 - 23:59',
                          style: context.bodySmall.copyWith(fontSize: 10),
                        ),
              trailing: Radio<EnumEffectivePeriodOptions>(
                value: EnumEffectivePeriodOptions.custom,
                groupValue: state.selectedPeriod,
                onChanged: (value) async {
                  if (value != null) {
                    context.read<EffectPeriodBloc>().add(SetPeriod(value));
                  }
                },
              ),
            ),
          ],
        );
      },
    );
  }

  Widget _buildRadioOption(
      BuildContext context, EnumEffectivePeriodOptions value, String subtitle) {
    return BlocBuilder<EffectPeriodBloc, EffectPeriodState>(
      builder: (context, state) {
        return ListTile(
          contentPadding: EdgeInsets.zero,
          onTap: () {
            context.read<EffectPeriodBloc>().add(SetPeriod(value));
          },
          title: BodyMedium(text: EffectPeriodHelper.formatEnumValue(value)),
          subtitle: BodySmall(
            text: subtitle,
            style: context.bodySmall.copyWith(fontSize: 10),
          ),
          trailing: Radio<EnumEffectivePeriodOptions>(
            value: value,
            groupValue: state.selectedPeriod,
            onChanged: (value) {
              if (value != null) {
                context.read<EffectPeriodBloc>().add(SetPeriod(value));
              }
            },
          ),
        );
      },
    );
  }
}
