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.family_restroom,
      'label': 'Family Medicine',
      'value': 'Family Medicine',
      'description': 'Comprehensive healthcare for families and individuals',
    },
    {
      'icon': Icons.favorite,
      'label': 'Cardiologist',
      'value': 'Cardiologist',
      'description': 'Diagnosis and treatment of heart conditions',
    },
    {
      'icon': Icons.psychology,
      'label': 'Neurology',
      'value': 'Neurology',
      'description': 'Treatment of nervous system disorders',
    },
    {
      'icon': Icons.local_hospital,
      'label': 'Gastroenterology',
      'value': 'Gastroenterology',
      'description': 'Digestive system disorders and treatment',
    },
    {
      'icon': Icons.face,
      'label': 'Dermatologist',
      'value': 'Dermatologist',
      'description': 'Skin, hair, and nail conditions',
    },
    {
      'icon': Icons.wheelchair_pickup,
      'label': 'Orthopedic',
      'value': 'Orthopedic',
      'description': 'Musculoskeletal system and injury treatment',
    },
    {
      'icon': Icons.remove_red_eye,
      'label': 'Ophthalmology',
      'value': 'Ophthalmology',
      'description': 'Eye care and vision treatment',
    },
    {
      '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.pregnant_woman,
      'label': 'Gynecology',
      'value': 'Gynecology',
      'description': "Women's health and reproductive care",
    },
    {
      'icon': Icons.water_drop,
      'label': 'Urology',
      'value': 'Urology',
      'description': 'Urinary tract and male reproductive health',
    },
    {
      'icon': Icons.biotech,
      'label': 'Endocrinology',
      'value': 'Endocrinology',
      'description': 'Hormone and metabolic disorders',
    },
    {
      'icon': Icons.bloodtype,
      'label': 'Oncology',
      'value': 'Oncology',
      'description': 'Cancer diagnosis and treatment',
    },
    {
      'icon': Icons.accessibility,
      'label': 'Rheumatology',
      'value': 'Rheumatology',
      'description': 'Arthritis and autoimmune conditions',
    },
    {
      'icon': Icons.air,
      'label': 'Pulmonology',
      'value': 'Pulmonology',
      'description': 'Respiratory system disorders',
    },
    {
      'icon': Icons.water,
      'label': 'Nephrology',
      'value': 'Nephrology',
      'description': 'Kidney diseases and disorders',
    },
    {
      'icon': Icons.cleaning_services,
      'label': 'Dentistry',
      'value': 'Dentistry',
      'description': 'Oral health and dental care',
    },
    {
      'icon': Icons.accessibility_new,
      'label': 'Physical Therapy',
      'value': 'Physical Therapy',
      'description': 'Rehabilitation and physical medicine',
    },
    {
      'icon': Icons.sports,
      'label': 'Sports Medicine',
      'value': 'Sports Medicine',
      'description': 'Athletic injuries and performance',
    },
    {
      'icon': Icons.sick,
      'label': 'Allergy & Immunology',
      'value': 'Allergy & Immunology',
      'description': 'Allergies and immune system disorders',
    },
    {
      'icon': Icons.healing,
      'label': 'Pain Management',
      'value': 'Pain Management',
      'description': 'Chronic pain treatment',
    },
    {
      'icon': Icons.bedtime,
      'label': 'Sleep Medicine',
      'value': 'Sleep Medicine',
      'description': 'Sleep disorders and treatment',
    },
    {
      'icon': Icons.elderly,
      'label': 'Geriatrics',
      'value': 'Geriatrics',
      'description': 'Healthcare for elderly patients',
    }
    // {
    //   '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,
      ),
    );
  }

  bool _validateSelection() {
    if (selectedSpeciality == null) {
      _showError('Please select a 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: 1,
                crossAxisSpacing: 16,
                mainAxisSpacing: 16,
              ),
              itemCount: specialities.length,
              itemBuilder: (context, index) {
                final specialty = specialities[index];
                final isSelected = selectedSpeciality == specialty['value'];

                return GestureDetector(
                  onTap: () {
                    setState(() {
                      // _speciality = specialty['value'];
                      selectedSpeciality = specialty['value'];
                      widget.controller.updateSpeciality(selectedSpeciality!);
                    });

                    // _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: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Icon(
                          specialty['icon'],
                          size: 32,
                          color: isSelected ? Colors.white : Colors.grey,
                        ),
                        const SizedBox(height: 8),
                        Text(
                          specialty['label'],
                          style: TextStyle(
                            color: isSelected ? Colors.white : Colors.black87,
                            fontSize: 14,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        if (isSelected)
                          const Icon(
                            Icons.check_circle,
                            color: Colors.white,
                            size: 20,
                          ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
