import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/SOAP/home_medication_vp/GetSearchCurrentMedicationDetails.dart';
import 'package:doctor_app_flutter/core/model/SOAP/master_key_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


class DropdownPopup extends StatefulWidget {
  final GetSearchCurrentMedicationDetails? medicationDetails;
  bool isStrength;
  bool isRoute;
  bool isFrequency;
  int? selectedID;
  String? selectedText;
  final okText;
  final Function(int selectedID, String selectedText)? okFunction;
  DropdownPopup({this.medicationDetails, this.isStrength = false, this.okFunction, this.okText, this.selectedID, this.isRoute =false, this.isFrequency =false, this.selectedText });

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

class _DropdownPopupState extends State<DropdownPopup> {
  @override
  void initState() {
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    ProjectViewModel projectViewModel = Provider.of(context);
    return showAlertDialog(context, projectViewModel);
  }

  showAlertDialog(BuildContext context, ProjectViewModel projectViewModel) {
    // set up the buttons
    Widget cancelButton = ElevatedButton(
        child: AppText(
          TranslationBase.of(context).cancel,
          color: Colors.white,
          fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * (SizeConfig.isWidthLarge ? 3.5 : 5),
        ),
        onPressed: () {
          Navigator.of(context).pop();
        });
    Widget continueButton = ElevatedButton(
        child: AppText(
          this.widget.okText,
          color: Colors.white,
          fontSize: SizeConfig.getTextMultiplierBasedOnWidth() * (SizeConfig.isWidthLarge ? 3.5 : 5),
        ),
        onPressed: () {

           // this.widget.okFunction(selectedValue);
          Navigator.of(context).pop();
        });
// set up the AlertDialog
    AlertDialog alert = AlertDialog(
      // title: Text(widget.title),
      content: createDialogList(projectViewModel),
      actions: [
        cancelButton,
        continueButton,
      ],
    );
    return alert;
  }

  Widget createDialogList(ProjectViewModel projectViewModel) {
    return Container(
      height: MediaQuery.of(context).size.height * 0.5,
      child: SingleChildScrollView(
        child:  widget.isStrength ? Column(
          children: [

           ...widget.medicationDetails!.itemStrengthDetailsDto!
                .map((item) => RadioListTile(
              title: AppText(
                '${item.strength}',
              ),
              groupValue: widget.selectedID!.toString(),
              value:  item.strengthId.toString(),
              activeColor: Colors.blue.shade700,
              selected:  item.strengthId.toString() == widget.selectedID!.toString(),
              onChanged: (val) {
                    widget.selectedID = item.strengthId;
                    widget.selectedText = item.strength;
                 widget.okFunction!( item.strengthId!,   item.strength!);

              },
            ))
                .toList()
          ],
        )  :

      widget.isRoute ?  Column( children: [

      ...widget.medicationDetails!.genericItemRouteDetailsEntity!
          .map((item) => RadioListTile(
    title: AppText(
    '${item.route}',
    ),
    groupValue: widget.selectedID!.toString(),
    value:  item.routeId.toString(),
    activeColor: Colors.blue.shade700,
    selected:  item.routeId.toString() == widget.selectedID!.toString(),
    onChanged: (val) {
    widget.selectedID = item.routeId;
    widget.selectedText = item.route;
    widget.okFunction!( item.routeId!,   item.route!);

    },
    ))
        .toList()
    ],
    ) : Column( children: [

        ...widget.medicationDetails!.genericItemFrequencyDetailsEntity!
            .map((item) => RadioListTile(
          title: AppText(
            '${item.frequency}',
          ),
          groupValue: widget.selectedID!.toString(),
          value:  item.frequencyId.toString(),
          activeColor: Colors.blue.shade700,
          selected:  item.frequencyId.toString() == widget.selectedID!.toString() ,
          onChanged: (val) {
            widget.selectedID = item.frequencyId;
            widget.selectedText = item.frequency;
            widget.okFunction!( item.frequencyId!,   item.frequency!);
          setState(() {

          });
          },
        ))
            .toList()
      ],
      ),
      ),
    );
  }

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