import 'package:diplomaticquarterapp/models/id-name-pair.dart';
import 'package:flutter/material.dart';

//  AppToast.showErrorToast(message: "Please select Time Slot to continue");

class RadioGroupDialog extends StatefulWidget {

  final BuildContext context;
  final title;
  final List<IdNamePair> list;
  final okText;
  final cancelText;
  final Function(IdNamePair) okFunction;
  final Function cancelFunction;
  IdNamePair selectedValue;

  RadioGroupDialog(
      {@required this.context,
        @required this.title,
        @required this.list,
        @required this.okText,
        @required this.cancelText,
        @required this.okFunction,
        @required this.cancelFunction});


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

class RadioGroupState extends State<RadioGroupDialog> {

  @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 = TextButton(
        child: Text(this.widget.cancelText),
        onPressed: () {
          Navigator.of(context).pop();
        });
    Widget continueButton =
    TextButton(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: createDialogGroup(),
      actions: [
        cancelButton,
        continueButton,
      ],
    );
    return alert;
  }

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

  Widget createDialogGroup(){
    return Container(
      height: MediaQuery.of(context).size.height * 0.3,
      child: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Container(
              child: Column(
                children:
                widget.list.map((data) => RadioListTile(
                  title: Text("${data.name}"),
                  groupValue: widget.selectedValue.id,
                  value: data.id,
                  onChanged: (val) {
                    setState(() {
                      widget.selectedValue = data ;
                    });
                  },
                )).toList(),
              ),
            ),

          ],
        ),
      ),
    );
  }

}
