import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:design_system/design_system.dart';
import '../blocs/bank_account_cubit.dart';

class AddAccountForm extends StatefulWidget {
  final dynamic strings;
  final Function(String routing, String account, String type) onSubmit;
  final VoidCallback onCancel;

  const AddAccountForm({super.key, required this.strings, required this.onSubmit, required this.onCancel});

  @override
  State<AddAccountForm> createState() => _AddAccountFormState();
}

class _AddAccountFormState extends State<AddAccountForm> {
  final TextEditingController _routingController = TextEditingController();
  final TextEditingController _accountController = TextEditingController();
  String _selectedType = 'CHECKING';

  @override
  void dispose() {
    _routingController.dispose();
    _accountController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(UiConstants.space4),
      decoration: BoxDecoration(
        color: UiColors.bgPopup, // Was surface
        borderRadius: BorderRadius.circular(UiConstants.radiusBase),
        border: Border.all(color: UiColors.border),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Text(
            widget.strings.add_new_account,
            style: UiTypography.headline4m.copyWith(color: UiColors.textPrimary), // Was header4
          ),
          const SizedBox(height: UiConstants.space4),
          UiTextField(
            label: widget.strings.routing_number,
            hintText: widget.strings.routing_hint,
            controller: _routingController,
            keyboardType: TextInputType.number,
          ),
          const SizedBox(height: UiConstants.space4),
          UiTextField(
            label: widget.strings.account_number,
            hintText: widget.strings.account_hint,
            controller: _accountController,
            keyboardType: TextInputType.number,
          ),
          const SizedBox(height: UiConstants.space4),
          Padding(
            padding: const EdgeInsets.only(bottom: UiConstants.space2),
            child: Text(
              widget.strings.account_type,
              style: UiTypography.body2r.copyWith( // Was body2
                  color: UiColors.textSecondary, fontWeight: FontWeight.w500),
            ),
          ),
          Row(
            children: <Widget>[
              Expanded(
                  child: _buildTypeButton('CHECKING', widget.strings.checking)),
              const SizedBox(width: UiConstants.space2),
              Expanded(
                  child: _buildTypeButton('SAVINGS', widget.strings.savings)),
            ],
          ),
          const SizedBox(height: UiConstants.space6),
          Row(
            children: <Widget>[
              Expanded(
                child: UiButton.text(
                  text: widget.strings.cancel,
                  onPressed: () => widget.onCancel(),
                ),
              ),
              const SizedBox(width: UiConstants.space2),
              Expanded(
                child: UiButton.primary(
                  text: widget.strings.save,
                  onPressed: () {
                    widget.onSubmit(
                      _routingController.text,
                      _accountController.text,
                      _selectedType,
                    );
                  },
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildTypeButton(String type, String label) {
    final bool isSelected = _selectedType == type;
    return GestureDetector(
      onTap: () => setState(() => _selectedType = type),
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 12),
        decoration: BoxDecoration(
          color: isSelected
              ? UiColors.primary.withOpacity(0.05)
              : UiColors.bgPopup, // Was surface
          borderRadius: BorderRadius.circular(UiConstants.radiusBase),
          border: Border.all(
            color: isSelected ? UiColors.primary : UiColors.border,
            width: isSelected ? 2 : 1,
          ),
        ),
        child: Center(
          child: Text(
            label,
            style: UiTypography.body2r.copyWith( // Was body2
              fontWeight: FontWeight.w600,
              color: isSelected ? UiColors.primary : UiColors.textSecondary,
            ),
          ),
        ),
      ),
    );
  }
}
