import 'package:hmg_patient_app/core/model/packages_offers/responses/tamara_payment_option.dart';
import 'package:hmg_patient_app/theme/colors.dart';
import 'package:hmg_patient_app/uitl/CalendarUtils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:manage_calendar_events/manage_calendar_events.dart';
import 'package:permission_handler/permission_handler.dart';


class SingleSelectionDialog<T> extends StatefulWidget {
  final List<T> items;
  final String title;
  final Widget icon;

  SingleSelectionDialog(this.items, {required this.icon, required this.title});

  Future<Future<T?>> show(BuildContext context) async {
    return showDialog<T>(
      context: context,
      barrierDismissible: true, // user must tap button!
      builder: (BuildContext context) {
        return Dialog(
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0.0)),
          insetPadding: EdgeInsets.all(21),
          child: this,
        );
      },
    );
  }

  @override
  _SingleSelectionDialogState createState() => _SingleSelectionDialogState();

}

class _SingleSelectionDialogState extends State<SingleSelectionDialog> {
  int i = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
      //width: MediaQuery.of(context).size.width / 0.8,
      padding: const EdgeInsets.all(20.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [

              SizedBox(
                height: 40,
                child: widget.icon,
              ),

              IconButton(
                icon: Icon(Icons.clear),
                color: Color(0xff2B353E),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
          mHeight(20),
          Text(
            widget.title,
            style: TextStyle(
              fontSize: 16,
              color: Color(0xff2B353E),
              fontWeight: FontWeight.w600,
              letterSpacing: -0.48,
            ),
          ),
          
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 20),
            child: optionList(widget.items),
          ),
          
          mHeight(12),
          DefaultButton(
            TranslationBase.of(context).done,
            () {
              Navigator.pop(context, widget.items[i]);
            },
            color: CustomColors.green,
          ),
        ],
      ),
    );
  }
  
  Widget optionList(List<dynamic> options){
    return ListView.builder(
        shrinkWrap: true,
        itemCount: options.length,
        itemBuilder: (ctx,idx){
          final opt = options[idx];
          return Row(
            children: [
              Radio<int>(
                value: idx,
                groupValue: i,
                onChanged: (int? value) {
                  setState(() {
                    i = value!;
                  });
                },
              ),
              Text(
                opt.toString(),
                style: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w600,
                  letterSpacing: -0.48,
                ),
              ),
            ],
          );
        }
    );
  }
}
