import 'package:flutter/material.dart';
import 'package:country_state_city_picker/country_state_city_picker.dart';

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

  @override
  State<PatientAddressScreen> createState() => _PatientAddressScreenState();
}

class _PatientAddressScreenState extends State<PatientAddressScreen> {
  final TextEditingController _houseNoController = TextEditingController();
  final TextEditingController _lineController = TextEditingController();
  final TextEditingController _townController = TextEditingController();
  final TextEditingController _pincodeController = TextEditingController();
  String? country;
  String? state;
  String? city;
  String? addressType;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Address'),
        actions: [
          TextButton(
            onPressed: () {
              // Save address logic here
            },
            child: const Text('Done', style: TextStyle(color: Colors.blue)),
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildTextField('House No', _houseNoController),
            _buildTextField('Line', _lineController),
            _buildTextField('Town', _townController, hintText: '(OPTIONAL)'),
            _buildTextField('Pincode', _pincodeController),
            const SizedBox(height: 20),
            SelectState(
              onCountryChanged: (value) {
                setState(() {
                  country = value;
                });
              },
              onStateChanged: (value) {
                setState(() {
                  state = value;
                });
              },
              onCityChanged: (value) {
                setState(() {
                  city = value;
                });
              },
            ),
            const SizedBox(height: 20),
            const Text('Type of address',
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            _buildAddressTypeChips(),
            if (addressType == 'Other')
              const TextField(
                decoration: InputDecoration(
                  hintText: 'Other Label...',
                  border: OutlineInputBorder(),
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildTextField(String label, TextEditingController controller,
      {String? hintText}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(label,
            style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
        TextField(
          controller: controller,
          decoration: InputDecoration(
            hintText: hintText,
            border: const UnderlineInputBorder(),
          ),
        ),
        const SizedBox(height: 20),
      ],
    );
  }

  Widget _buildAddressTypeChips() {
    return Wrap(
      spacing: 8.0,
      children: ['Home', 'Office', 'Other'].map((String type) {
        return ChoiceChip(
          label: Text(type),
          selected: addressType == type,
          onSelected: (bool selected) {
            setState(() {
              addressType = selected ? type : addressType;
            });
          },
        );
      }).toList(),
    );
  }
}
