import 'package:doctor_app_flutter/config/size_config.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:flutter/material.dart';
import 'package:provider/provider.dart';

import '../app_texts_widget.dart';

// ignore: must_be_immutable
class MasterKeyDailog extends StatefulWidget {
  final List<MasterKeyModel> list;
  final okText;
  final Function(MasterKeyModel) okFunction;
  MasterKeyModel? selectedValue;
  final bool isICD;

  MasterKeyDailog({required this.list, required this.okText, required this.okFunction, this.selectedValue, this.isICD = false});

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

class _MasterKeyDailogState extends State<MasterKeyDailog> {
  @override
  void initState() {
    super.initState();
    widget.selectedValue = widget.selectedValue ?? widget.list.first;
  }

  @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(widget.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: Column(
          children: [
            ...widget.list
                .map((item) => RadioListTile(
                      title: AppText(
                        '${projectViewModel.isArabic ? item.nameAr : item.nameEn}' + (widget.isICD ? '/${item.code}' : ''),
                      ),
                      groupValue: widget.isICD ? widget.selectedValue!.code.toString() : widget.selectedValue!.id.toString(),
                      value: widget.isICD ? widget.selectedValue!.code.toString() : item.id.toString(),
                      activeColor: Colors.blue.shade700,
                      selected: widget.isICD ? item.code.toString() == widget.selectedValue!.code.toString() : item.id.toString() == widget.selectedValue!.id.toString(),
                      onChanged: (val) {
                        setState(() {
                          widget.selectedValue = item;
                        });
                      },
                    ))
                .toList()
          ],
        ),
      ),
    );
  }

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