import 'package:flutter/material.dart';
import 'package:telemednet/screens/doctor_screens/profile_description_screen.dart';
import '../../common/custom_style.dart';
import '../../controllers/doctor_controller.dart';
import '../../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> {
  final _floorBuildingController = TextEditingController();
  final _streetController = TextEditingController();
  final _cityController = TextEditingController();
  final _stateController = TextEditingController();
  final _countryController = TextEditingController();
  final _postalCodeController = TextEditingController();

  late AddressController addressController;

  @override
  void initState() {
    super.initState();

    // Initialize the AddressController
    addressController = widget.controller.addressController;

    // Set the initial values from the address model
    _floorBuildingController.text = addressController.model.floorBuilding ?? '';
    _streetController.text = addressController.model.street ?? '';
    _cityController.text = addressController.model.city ?? '';
    _stateController.text = addressController.model.state ?? '';
    _countryController.text = addressController.model.country ?? '';
    _postalCodeController.text = addressController.model.postalCode ?? '';
  }

  @override
  void dispose() {
    _floorBuildingController.dispose();
    _streetController.dispose();
    _cityController.dispose();
    _stateController.dispose();
    _countryController.dispose();
    _postalCodeController.dispose();
    super.dispose();
  }

  bool validateAndProceed() {
    // Update the address model
    addressController.updateFloorBuilding(_floorBuildingController.text);
    addressController.updateStreet(_streetController.text);
    addressController.updateCity(_cityController.text);
    addressController.updateState(_stateController.text);
    addressController.updateCountry(_countryController.text);
    addressController.updatePostalCode(_postalCodeController.text);

    // Validate the address fields
    if (_areFieldsValid()) {
      return true;
    }

    showErrorSnackBar(context, 'Please fill in all required fields');
    return false;
  }

  bool _areFieldsValid() {
    return _floorBuildingController.text.isNotEmpty &&
        _streetController.text.isNotEmpty &&
        _cityController.text.isNotEmpty &&
        _stateController.text.isNotEmpty &&
        _countryController.text.isNotEmpty &&
        _postalCodeController.text.isNotEmpty;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Address Details'),
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildTextField(
              'Floor, Building',
              'Enter floor and building',
              _floorBuildingController,
            ),
            _buildTextField(
              'Street or Road',
              'Enter street or road',
              _streetController,
            ),
            _buildTextField(
              'City',
              'Enter city',
              _cityController,
            ),
            _buildTextField(
              'State',
              'Enter state',
              _stateController,
            ),
            _buildTextField(
              'Country',
              'Enter country',
              _countryController,
            ),
            _buildTextField(
              'Postal Code',
              'Enter postal code',
              _postalCodeController,
            ),
          ],
        ),
      ),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.all(16),
        child: ElevatedButton(
          onPressed: () {
            Navigator.pushNamed(
              context,
              RouteNames.profileDescriptionScreen,
              arguments: ProfileDescriptionScreen(
                controller:
                    widget.controller, // Pass the same controller instance
              ),
            );
          },
          style: ElevatedButton.styleFrom(
            padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
            backgroundColor: const Color(0xFF5BC0DE),
          ),
          child: const Icon(
            Icons.arrow_forward_ios,
            color: Colors.white,
          ),
        ),
      ),
    );
  }

  Widget _buildTextField(
    String label,
    String hint,
    TextEditingController controller,
  ) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 16.0),
      child: TextField(
        controller: controller,
        decoration: InputDecoration(
          labelText: label,
          hintText: hint,
          border: const OutlineInputBorder(),
        ),
      ),
    );
  }
}
