import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/core/utils/validators.dart';
import 'package:flux/core/widgets/flux_text_field.dart';
import 'package:flux/features/onboarding/blocs/onboarding_cubit.dart';
import 'package:flux/features/onboarding/blocs/onboarding_state.dart';

class CompanyOnboardingForm extends StatefulWidget {
  final OnboardingState state;
  const CompanyOnboardingForm({super.key, required this.state});

  @override
  State<CompanyOnboardingForm> createState() => _CompanyOnboardingFormState();
}

class _CompanyOnboardingFormState extends State<CompanyOnboardingForm> {
  final _formKey = GlobalKey<FormState>();
  final _nameCtrl = TextEditingController();

  @override
  void dispose() {
    _nameCtrl.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(32),
      child: Form(
        key: _formKey,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            const Text(
              "Iniziamo! 🏢",
              style: TextStyle(fontSize: 32, fontWeight: FontWeight.bold),
            ),
            const SizedBox(height: 8),
            const Text(
              "Come si chiama la tua Azienda? \n(Potrai inserire i dati di fatturazione in seguito).",
              style: TextStyle(fontSize: 16, color: Colors.grey),
            ),
            const SizedBox(height: 48),

            FluxTextField(
              label: 'Ragione Sociale / Nome Azienda',
              controller: _nameCtrl,
              validator: notEmptyValidator,
              keyboardType: TextInputType.name,
              textCapitalization: TextCapitalization.words,
              autocorrect: false,
              onSubmitted: (_) => _submit(),
            ),

            const Spacer(),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                padding: const EdgeInsets.symmetric(vertical: 16),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(12),
                ),
              ),
              onPressed: () => _submit(),
              child: const Text(
                "Salva e Prosegui",
                style: TextStyle(fontSize: 16),
              ),
            ),
            const SizedBox(height: 16),
          ],
        ),
      ),
    );
  }

  void _submit() {
    if (_formKey.currentState!.validate()) {
      context.read<OnboardingCubit>().saveCompany(_nameCtrl.text.trim());
    }
  }
}
