import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/visitor_password/bloc/visitor_password_bloc.dart';

class AccessTypeRadioGroup extends StatelessWidget {
  final String? selectedType;
  final String? accessTypeSelected;
  final Function(String) onTypeSelected;
  final VisitorPasswordBloc visitorBloc;

  const AccessTypeRadioGroup({
    super.key,
    required this.selectedType,
    required this.accessTypeSelected,
    required this.onTypeSelected,
    required this.visitorBloc,
  });

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    final text = Theme.of(context)
        .textTheme
        .bodySmall!
        .copyWith(color: Colors.black, fontSize: 13);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Text(
              '* ',
              style: Theme.of(context)
                  .textTheme
                  .bodyMedium!
                  .copyWith(color: Colors.red),
            ),
            Text('Access Type', style: text),
          ],
        ),
        const SizedBox(height: 8),
        if (size.width < 800)
          Column(
            children: [
              _buildRadioTile(
                context,
                'Online Password',
                selectedType ?? accessTypeSelected,
                onTypeSelected,
              ),
              const SizedBox(height: 8),
              _buildRadioTile(
                context,
                'Offline Password',
                selectedType ?? accessTypeSelected,
                onTypeSelected,
              ),
            ],
          )
        else
          Row(
            children: [
              Expanded(
                flex: 2,
                child: Row(
                  children: [
                    _buildRadioTile(
                      context,
                      'Online Password',
                      selectedType ?? accessTypeSelected,
                      onTypeSelected,
                      width: size.width * 0.12,
                    ),
                    _buildRadioTile(
                      context,
                      'Offline Password',
                      selectedType ?? accessTypeSelected,
                      onTypeSelected,
                      width: size.width * 0.12,
                    ),
                  ],
                ),
              ),
              const Spacer(flex: 2),
            ],
          ),
      ],
    );
  }

  Widget _buildRadioTile(
    BuildContext context,
    String value,
    String? groupValue,
    Function(String) onChanged, {
    double? width,
  }) {
    return SizedBox(
      width: width,
      child: RadioListTile<String>(
        contentPadding: EdgeInsets.zero,
        title: Text(value,
            style: Theme.of(context).textTheme.bodySmall!.copyWith(
                  color: Colors.black,
                  fontSize: 13,
                )),
        value: value,
        groupValue: groupValue,
        onChanged: (value) {
          if (value != null) {
            onChanged(value);
            if (value == 'Dynamic Password') {
              visitorBloc.usageFrequencySelected = '';
            }
          }
        },
      ),
    );
  }
}
