import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/widgets/shared/dialogs/dailog-list-select.dart';
import 'package:doctor_app_flutter/widgets/shared/text_fields/app-textfield-custom.dart';
import 'package:flutter/material.dart';

class CountryTextField extends StatefulWidget {
  final dynamic element;
  final String elementError;
  final List<dynamic>? elementList;
  final String? keyName;
  final String? keyId;
  final String? hintText;
  final double? width;
  final Function(dynamic)? okFunction;

  CountryTextField({Key? key, required this.element, required this.elementError, this.width, this.elementList, this.keyName, this.keyId, this.hintText, this.okFunction}) : super(key: key);

  @override
  _CountryTextfieldState createState() => _CountryTextfieldState();
}

class _CountryTextfieldState extends State<CountryTextField> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: widget.width ?? null,
      child: InkWell(
        onTap: widget.elementList != null
            ? () {
                Utils.hideKeyboard(context);
                ListSelectDialog dialog = ListSelectDialog(
                  list: widget.elementList!,
                  attributeName: '${widget.keyName}',
                  attributeValueId: widget.elementList!.length == 1 ? widget.elementList![0]['${widget.keyId}'] : '${widget.keyId}',
                  okText: TranslationBase.of(context).ok,
                  okFunction: (selectedValue) => widget.okFunction!(selectedValue),
                );
                showDialog(
                  barrierDismissible: false,
                  context: context,
                  builder: (BuildContext context) {
                    return dialog;
                  },
                );
              }
            : null,
        child: AppTextFieldCustom(
          hintText: widget.hintText,
          dropDownText: widget.elementList!.length == 1
              ? widget.elementList![0]['${widget.keyName}']
              : widget.element != null
                  ? widget.element['${widget.keyName}']
                  : null,
          isTextFieldHasSuffix: true,
          validationError: widget.elementList!.length != 1 ? widget.elementError : null,
          enabled: false,
          onClick: () {},
          onChanged: (String? value) {},
          onFieldSubmitted: () {},
        ),
      ),
    );
  }
}
