// lib/presentation/pages/user_info_page.dart
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:proxibuy/presentation/auth/bloc/auth_bloc.dart';
import 'package:proxibuy/presentation/pages/notification_preferences_page.dart';
import '../../core/config/app_colors.dart';
import '../../core/gen/assets.gen.dart';

class UserInfoPage extends StatefulWidget {
  const UserInfoPage({super.key});

  @override
  State<UserInfoPage> createState() => _UserInfoPageState();
}

class _UserInfoPageState extends State<UserInfoPage> {
  final _nameController = TextEditingController();
  String _selectedGender = 'مرد';

  @override
  void dispose() {
    _nameController.dispose();
    super.dispose();
  }

  Widget _buildGenderRadio(String title, String value) {
    return InkWell(
      onTap: () => setState(() => _selectedGender = value),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Radio<String>(
            value: value,
            groupValue: _selectedGender,
            onChanged: (newValue) => setState(() => _selectedGender = newValue!),
            activeColor: AppColors.primary, 
          ),
          Text(title, style: const TextStyle(color: Colors.grey)),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;

    return Scaffold(
      body: Stack(
        children: [
          Positioned.fill(
            child: Image.asset(
              Assets.images.userinfo.path,
              fit: BoxFit.cover,
            ),
          ),

          DraggableScrollableSheet(
            initialChildSize: 0.50,
            minChildSize: 0.50,
            maxChildSize: 0.50,
            builder: (context, scrollController) {
              return Container(
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.vertical(top: Radius.circular(32)),
                ),
                child: SingleChildScrollView(
                  controller: scrollController,
                  padding: const EdgeInsets.all(24.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Center(
                        child: Container(
                          width: 50,
                          height: 5,
                          decoration: BoxDecoration(
                            color:Colors.grey.withOpacity(0.5),
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                      ),
                      const SizedBox(height: 40),
                      TextField(
                        controller: _nameController,
                        textAlign: TextAlign.right,
                        decoration: const InputDecoration(
                          labelText: "دوست داری با چه اسمی صدات کنیم؟",
                          labelStyle: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: 20,
                          ),
                          hintText: "مثلا نام کوچک شما",
                          hintStyle: TextStyle(fontSize: 15, color: Colors.grey),
                        ),
                      ),
                      const SizedBox(height: 24),

                      Text(
                        "جنسیت",
                        style: textTheme.titleMedium?.copyWith(
                          color: Colors.black,
                        ),
                      ),
                      const SizedBox(height: 8),
                      Wrap(
                        spacing: 10.0,
                        runSpacing: 8.0,
                        children: [
                          _buildGenderRadio('مرد', 'مرد'),
                          _buildGenderRadio('زن', 'زن'),
                          _buildGenderRadio('تمایلی به پاسخ ندارم', 'نامشخص'),
                        ],
                      ),
                      const SizedBox(height: 70),

                      BlocConsumer<AuthBloc, AuthState>(
                        listener: (context, state) {
                          if (state is UserInfoSaved) {
                            Navigator.of(context).pushReplacement(
                              MaterialPageRoute(builder: (_) => const NotificationPreferencesPage()),
                            );
                          } else if (state is AuthFailure) {
                            ScaffoldMessenger.of(context).showSnackBar(
                              SnackBar(content: Text(state.message), backgroundColor: Colors.red),
                            );
                          }
                        },
                        builder: (context, state) {
                          if (state is AuthLoading) {
                            return const Center(child: CircularProgressIndicator());
                          }
                          return SizedBox(
                            width: double.infinity,
                            child: ElevatedButton(
                              style: ElevatedButton.styleFrom(
                                backgroundColor: AppColors.confirm, 
                                foregroundColor: Colors.white,
                              ),
                              onPressed: () {
                                context.read<AuthBloc>().add(SaveUserInfoEvent(
                                      name: _nameController.text,
                                      gender: _selectedGender,
                                    ));
                              },
                              child: const Text("اعمال"),
                            ),
                          );
                        },
                      ),
                      const SizedBox(height: 9),

                      Center(
                        child: TextButton(
                          onPressed: () {
                            Navigator.of(context).push(NotificationPreferencesPage.route());
                          },
                          child: const Text(
                            "رد شدن",
                            style: TextStyle(color: Colors.black),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              );
            },
          ),
        ],
      ),
    );
  }
}