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

class DoctorAddressScreen extends StatefulWidget {
  final DoctorController? controller;
  const DoctorAddressScreen({
    super.key,
    required this.controller,
  });
  @override
  State<DoctorAddressScreen> createState() => _DoctorAddressScreenState();
}

class _DoctorAddressScreenState extends State<DoctorAddressScreen> {
  late DoctorController _controller;
  late TextEditingController _floorBuildingController;
  late TextEditingController _streetController;
  late TextEditingController _cityController;
  late TextEditingController _stateController;
  late TextEditingController _countryController;
  late TextEditingController _postalCodeController;
  late TextEditingController _addressTypeController;
  final _formKey = GlobalKey<FormState>();
  String? selectedAddressType;
  bool showCustomTypeField = false;

  final List<String> addressTypes = ['Home', 'Office', 'Others'];

  @override
  void initState() {
    super.initState();
    _controller = widget.controller ?? DoctorController();
    _floorBuildingController = TextEditingController();
    _streetController = TextEditingController();
    _cityController = TextEditingController();
    _stateController = TextEditingController();
    _countryController = TextEditingController();
    _postalCodeController = TextEditingController();
    _addressTypeController = TextEditingController();
    _loadSavedData();
  }

  void _loadSavedData() {
    final doctor = _controller.model;
    _floorBuildingController.text = doctor.floorBuilding ?? '';
    _streetController.text = doctor.street ?? '';
    _cityController.text = doctor.city ?? '';
    _stateController.text = doctor.state ?? '';
    _countryController.text = doctor.country ?? '';
    _postalCodeController.text = doctor.postalCode ?? '';

    // Proper handling of address type
    if (doctor.addressType != null) {
      final savedType = doctor.addressType!;
      if (addressTypes.contains(savedType)) {
        selectedAddressType = savedType;
        showCustomTypeField = false;
      } else {
        selectedAddressType = 'Others';
        showCustomTypeField = true;
        _addressTypeController.text = savedType;
      }
    }
  }

  void _handleAddressTypeSelection(String type) {
    setState(() {
      if (type == 'Others') {
        showCustomTypeField = true;
        selectedAddressType = type;
        // Don't clear the custom field if it was previously set
        if (_addressTypeController.text.isEmpty) {
          _controller.updateAddressType(''); // Clear the controller value
        }
      } else {
        showCustomTypeField = false;
        selectedAddressType = type;
        _addressTypeController.clear();
        _controller.updateAddressType(
            type); // Update controller with the selected type
      }
    });
  }

  bool _validateAndProceed() {
    if (_formKey.currentState!.validate()) {
      // Save all form data
      _controller.updateFloorBuilding(_floorBuildingController.text);
      _controller.updateStreet(_streetController.text);
      _controller.updateCity(_cityController.text);
      _controller.updateState(_stateController.text);
      _controller.updateCountry(_countryController.text);
      _controller.updatePostalCode(_postalCodeController.text);

      // Handle address type saving
      if (showCustomTypeField && _addressTypeController.text.isNotEmpty) {
        _controller.updateAddressType(_addressTypeController.text);
      } else if (selectedAddressType != null &&
          selectedAddressType != 'Others') {
        _controller.updateAddressType(selectedAddressType!);
      }

      return true;
    }
    return false;
  }

  Widget _buildAddressTypeChips() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          'Address Type',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w500,
            color: Colors.black87,
          ),
        ),
        const SizedBox(height: 8),
        LayoutBuilder(
          builder: (context, constraints) {
            final chipMaxWidth = constraints.maxWidth;

            return Wrap(
              spacing: 6,
              runSpacing: 6,
              children: addressTypes.map((type) {
                final isSelected = selectedAddressType == type;

                IconData icon;
                switch (type) {
                  case 'Home':
                    icon = Icons.home;
                    break;
                  case 'Office':
                    icon = Icons.work;
                    break;
                  case 'Others':
                    icon = Icons.more_horiz;
                    break;
                  default:
                    icon = Icons.location_on;
                }

                return Material(
                  color: Colors.transparent,
                  child: InkWell(
                    onTap: () => _handleAddressTypeSelection(type),
                    borderRadius: BorderRadius.circular(20),
                    child: Container(
                      constraints: BoxConstraints(
                        maxWidth:
                            (chipMaxWidth - (addressTypes.length - 1) * 6) /
                                addressTypes.length,
                      ),
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 8,
                      ),
                      decoration: BoxDecoration(
                        color: isSelected
                            ? Colors.blue.withOpacity(0.2)
                            : Colors.grey.withOpacity(0.1),
                        borderRadius: BorderRadius.circular(20),
                        border: Border.all(
                          color: isSelected ? Colors.blue : Colors.transparent,
                        ),
                      ),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Icon(icon, color: Colors.blue, size: 16),
                          const SizedBox(width: 4),
                          Text(
                            type,
                            style: TextStyle(
                              fontSize: 14,
                              color: isSelected ? Colors.blue : Colors.black87,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                );
              }).toList(),
            );
          },
        ),
        if (showCustomTypeField) ...[
          const SizedBox(height: 16),
          TextFormField(
            controller: _addressTypeController,
            decoration: InputDecoration(
              labelText: 'Custom Address Type',
              prefixIcon:
                  const Icon(Icons.edit_location_alt, color: Colors.blue),
              contentPadding:
                  const EdgeInsets.symmetric(vertical: 16, horizontal: 12),
              enabledBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: BorderSide(color: Colors.grey.shade300, width: 1),
              ),
              focusedBorder: OutlineInputBorder(
                borderRadius: BorderRadius.circular(8),
                borderSide: const BorderSide(color: Colors.blue, width: 1.5),
              ),
            ),
            validator: (value) {
              if (showCustomTypeField && (value == null || value.isEmpty)) {
                return 'Please enter address type';
              }
              return null;
            },
            onChanged: (value) {
              if (value.isNotEmpty) {
                _controller.updateAddressType(value);
              }
            },
          ),
        ],
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        actions: [
          IconButton(
            onPressed: () {
              if (_validateAndProceed()) {
                Navigator.pushNamed(
                  context,
                  RouteNames.profileDescriptionScreen,
                  arguments: _controller,
                );
              }
            },
            icon: const Icon(Icons.arrow_forward),
          ),
        ],
        title: const Text('Address Details'),
      ),
      body: Form(
        key: _formKey,
        child: SingleChildScrollView(
          padding: const EdgeInsets.all(16.0),
          child: Container(
            padding: const EdgeInsets.all(16.0),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
              boxShadow: [
                BoxShadow(
                  color: Colors.grey.withOpacity(0.5),
                  blurRadius: 10,
                  offset: const Offset(0, 4),
                ),
              ],
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _buildTextField(
                  label: 'Floor, Building',
                  controller: _floorBuildingController,
                  onChanged: (value) =>
                      widget.controller!.updateFloorBuilding(value),
                  icon: Icons.apartment,
                  isMandatory: true,
                ),
                _buildTextField(
                  label: 'Street or Road',
                  controller: _streetController,
                  onChanged: (value) => widget.controller!.updateStreet(value),
                  icon: Icons.streetview,
                  isMandatory: true,
                ),
                _buildTextField(
                  label: 'City',
                  controller: _cityController,
                  onChanged: (value) => widget.controller!.updateCity(value),
                  icon: Icons.location_city,
                  isMandatory: true,
                ),
                _buildTextField(
                  label: 'State',
                  controller: _stateController,
                  onChanged: (value) => widget.controller!.updateState(value),
                  icon: Icons.map,
                  isMandatory: true,
                ),
                _buildTextField(
                  label: 'Country',
                  controller: _countryController,
                  onChanged: (value) => widget.controller!.updateCountry(value),
                  icon: Icons.flag,
                  isMandatory: true,
                ),
                _buildPostalCodeField(
                  label: 'Postal Code',
                  controller: _postalCodeController,
                  onChanged: (value) =>
                      widget.controller!.updatePostalCode(value),
                  icon: Icons.mail,
                  isMandatory: true,
                ),
                const SizedBox(height: 16),
                _buildAddressTypeChips(),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildTextField({
    required String label,
    required TextEditingController controller,
    required Function(String) onChanged,
    required IconData icon,
    bool isMandatory = false,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: TextFormField(
        controller: controller,
        onChanged: onChanged,
        validator: (value) {
          if (isMandatory && (value == null || value.isEmpty)) {
            return '$label is required';
          }
          if (value != null &&
              value.isNotEmpty &&
              !RegExp(r"^[a-zA-Z0-9\s]+$").hasMatch(value)) {
            return 'Please enter valid text';
          }
          return null;
        },
        decoration: InputDecoration(
          labelText: label,
          prefixIcon: Icon(icon, color: Colors.blue),
          // filled: true,
          // fillColor: Colors.grey[100],
          contentPadding:
              const EdgeInsets.symmetric(vertical: 20, horizontal: 12),
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide(color: Colors.grey.shade300, width: 1),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.blue, width: 1.5),
          ),
          errorBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.red, width: 1.5),
          ),
          focusedErrorBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.red, width: 1.5),
          ),
        ),
      ),
    );
  }

  Widget _buildPostalCodeField({
    required String label,
    required TextEditingController controller,
    required Function(String) onChanged,
    required IconData icon,
    bool isMandatory = false,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: TextFormField(
        controller: controller,
        onChanged: onChanged,
        keyboardType: TextInputType.number,
        maxLength: 6,
        validator: (value) {
          if (isMandatory && (value == null || value.isEmpty)) {
            return '$label is required';
          }
          if (value != null && !RegExp(r'^(?!0{6})\d{6}$').hasMatch(value)) {
            return 'Please enter numbers only';
          }
          return null;
        },
        decoration: InputDecoration(
          labelText: label,
          prefixIcon: Icon(icon, color: Colors.blue),
          // filled: true,
          // fillColor: Colors.grey[100],
          contentPadding:
              const EdgeInsets.symmetric(vertical: 20, horizontal: 12),
          counterText: '',
          enabledBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: BorderSide(color: Colors.grey.shade300, width: 1),
          ),
          focusedBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.blue, width: 1.5),
          ),
          errorBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.red, width: 1.5),
          ),
          focusedErrorBorder: OutlineInputBorder(
            borderRadius: BorderRadius.circular(8),
            borderSide: const BorderSide(color: Colors.red, width: 1.5),
          ),
        ),
      ),
    );
  }
}
