import 'package:flutter/material.dart';
import 'package:medora/controllers/doctor_controller.dart';
import 'package:medora/route/route_names.dart';

class SpecialitiesScreen extends StatefulWidget {
  final DoctorController controller;

  const SpecialitiesScreen({
    super.key,
    required this.controller,
  });
  @override
  State<SpecialitiesScreen> createState() => _SpecialitiesScreenState();
}

class _SpecialitiesScreenState extends State<SpecialitiesScreen> {
  String? selectedSpeciality;
  late final DoctorController _controller;

  final List<Map<String, dynamic>> specialities = [
    {
      'icon': Icons.child_care,
      'label': 'Pediatric',
      'value': 'Pediatric',
      'description': 'Medical care for infants, children, and adolescents',
    },
    {
      'icon': Icons.medical_services,
      'label': 'General Medicine',
      'value': 'General Medicine',
      'description':
          'Primary healthcare for adults and general medical conditions',
    },
    {
      'icon': Icons.favorite,
      'label': 'Cardiology',
      'value': 'Cardiology',
      'description': 'Diagnosis and treatment of heart conditions',
    },
    {
      'icon': Icons.face,
      'label': 'Dermatolology',
      'value': 'Dermatolology',
      'description': 'Skin, hair, and nail conditions',
    },
    {
      'icon': Icons.hearing,
      'label': 'ENT',
      'value': 'ENT',
      'description': 'Ear, nose, and throat specialist',
    },
    {
      'icon': Icons.psychology_outlined,
      'label': 'Psychiatry',
      'value': 'Psychiatry',
      'description': 'Mental health and behavioral disorders',
    },
    {
      'icon': Icons.food_bank,
      'label': 'Clinical Nutrition',
      'value': 'Clinical Nutrition',
      'description': 'Nutrition counseling and management',
    },
    {
      'icon': Icons.medical_services_outlined,
      'label': 'Dentistry',
      'value': 'Dentistry',
      'description': 'Nutrition counseling and management',
    }
    // {
    //   'icon': Icons.child_care,
    //   'label': 'Pediatric',
    //   'value': 'Pediatric',
    //   'description': 'Specialist in child healthcare',
    // },
    // {
    //   'icon': Icons.medical_services,
    //   'label': 'Casual',
    //   'value': 'casual',
    //   'description': 'General healthcare provider',
    // },
    // {
    //   'icon': Icons.coronavirus,
    //   'label': 'Corona',
    //   'value': 'corona',
    //   'description': 'COVID-19 specialist',
    // },
    // {
    //   'icon': Icons.pregnant_woman,
    //   'label': 'Gynecology',
    //   'value': 'gynecology',
    //   'description': 'Women\'s health specialist',
    // },
    // {
    //   'icon': Icons.medical_services_outlined,
    //   'label': 'Orthopedic',
    //   'value': 'orthopedic',
    //   'description': 'Bone and joint specialist',
    // },
    // {
    //   'icon': Icons.remove_red_eye,
    //   'label': 'Eye',
    //   'value': 'eye',
    //   'description': 'Eye care specialist',
    // },
    // {
    //   'icon': Icons.psychology,
    //   'label': 'Psychiatrist',
    //   'value': 'psychiatrist',
    //   'description': 'Mental health specialist',
    // },
    // {
    //   'icon': Icons.medical_information,
    //   'label': 'Dentistry',
    //   'value': 'dental',
    //   'description': 'Dental care specialist',
    // },
    // {
    //   'icon': Icons.person,
    //   'label': 'General Medicine',
    //   'value': 'general',
    //   'description': 'General practitioner',
    // },
  ];

  @override
  void initState() {
    super.initState();
    _controller = widget.controller;
    _loadSavedData();
  }

  void _loadSavedData() {}

  void _showError(String message) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
        backgroundColor: Colors.red,
        behavior: SnackBarBehavior.floating,
      ),
    );
  }

  List<String> selectedSpecialities = [];
  bool _validateSelection() {
    if (selectedSpecialities.isEmpty) {
      _showError('Please select at least one speciality to continue');
      return false;
    }
    return true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            icon: const Icon(Icons.arrow_forward),
            onPressed: () {
              if (_validateSelection()) {
                Navigator.pushNamed(
                  context,
                  RouteNames.achievementsScreen,
                  arguments: _controller,
                );
              }
            },
          )
        ],
        title: const Text('Choose Speciality'),
      ),
      body: Column(
        children: [
          const Padding(
            padding: EdgeInsets.all(16.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Select Your Specialization',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 8),
              ],
            ),
          ),
          Expanded(
            child: GridView.builder(
              padding: const EdgeInsets.all(16),
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                childAspectRatio: 0.9,
                crossAxisSpacing: 16,
                mainAxisSpacing: 16,
              ),
              itemCount: specialities.length,
              itemBuilder: (context, index) {
                final specialty = specialities[index];
                // final isSelected = selectedSpeciality == specialty['value'];
                final isSelected =
                    selectedSpecialities.contains(specialty['value']);

                return GestureDetector(
                  onTap: () {
                    setState(() {
                      final value = specialty['value'] as String;
                      if (selectedSpecialities.contains(value)) {
                        selectedSpecialities.remove(value);
                      } else if (selectedSpecialities.length < 2) {
                        selectedSpecialities.add(value);
                      } else {
                        _showError('You can only select two specialities');
                      }

                      widget.controller
                          .updateSpeciality(selectedSpecialities.join(', '));
                    });

                    // _controller.updateSpeciality(specialty['value']);
                  },
                  child: Container(
                    decoration: BoxDecoration(
                      color: isSelected
                          ? const Color(0xFF5BC0DE)
                          : Colors.grey.shade200,
                      borderRadius: BorderRadius.circular(12),
                      border: isSelected
                          ? Border.all(
                              color: Colors.white,
                              width: 2,
                            )
                          : null,
                    ),
                    child: Center(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Icon(
                              specialty['icon'],
                              size: 32,
                              color: isSelected ? Colors.white : Colors.grey,
                            ),
                            const SizedBox(height: 8),
                            Flexible(
                              child: Text(
                                specialty['label'],
                                textAlign: TextAlign.center,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                  color: isSelected
                                      ? Colors.white
                                      : Colors.black87,
                                  fontSize: 13,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                            if (isSelected)
                              const Padding(
                                padding: EdgeInsets.only(top: 4),
                                child: Icon(
                                  Icons.check_circle,
                                  color: Colors.white,
                                  size: 20,
                                ),
                              ),
                          ],
                        ),
                      ),
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
