import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:flutter/material.dart';

class ListSelectDialog extends StatefulWidget {
  final List<dynamic> list;
  final String attributeName;
  final String attributeValueId;
  final okText;
  final Function(dynamic) okFunction;
  dynamic selectedValue;

  ListSelectDialog(
      {@required this.list,
      @required this.attributeName,
      @required this.attributeValueId,
      @required this.okText,
      @required this.okFunction});

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

class _ListSelectDialogState extends State<ListSelectDialog> {
  @override
  void initState() {
    super.initState();
    widget.selectedValue = widget.selectedValue ?? widget.list[0];
  }

  @override
  Widget build(BuildContext context) {
    return showAlertDialog(context);
  }

  showAlertDialog(BuildContext context) {
    // set up the buttons
    Widget cancelButton = FlatButton(
        child: Text(TranslationBase.of(context).cancel),
        onPressed: () {
          Navigator.of(context).pop();
        });
    Widget continueButton = FlatButton(
        child: Text(this.widget.okText),
        onPressed: () {
          this.widget.okFunction(widget.selectedValue);
          Navigator.of(context).pop();
        });
// set up the AlertDialog
    AlertDialog alert = AlertDialog(
      // title: Text(widget.title),
      content: createDialogList(),
      actions: [
        cancelButton,
        continueButton,
      ],
    );
    return alert;
  }

  Widget createDialogList() {
    return Container(
      height: MediaQuery.of(context).size.height * 0.5,
      child: SingleChildScrollView(
        child: Column(
          children: [
            ...widget.list
                .map((item) => RadioListTile(
                      title: Text("${item[widget.attributeName].toString()}"),
                      groupValue: widget.selectedValue[widget.attributeValueId]
                          .toString(),
                      value: item[widget.attributeValueId].toString(),
                      activeColor: Colors.blue.shade700,
                      selected: item[widget.attributeValueId].toString() ==
                          widget.selectedValue[widget.attributeValueId]
                              .toString(),
                      onChanged: (val) {
                        setState(() {
                          widget.selectedValue = item;
                        });
                      },
                    ))
                .toList()
          ],
        ),
      ),
    );
  }

  static closeAlertDialog(BuildContext context) {
    Navigator.of(context).pop();
  }
}
