import 'package:hmg_patient_app/pages/MyAppointments/models/AskDocRequestTypeModel.dart';
import 'package:hmg_patient_app/pages/MyAppointments/widgets/askDocDialog.dart';
import 'package:hmg_patient_app/pages/MyAppointments/widgets/reminder_dialog.dart';
import 'package:hmg_patient_app/pages/MyAppointments/widgets/reminder_dialog_prescription.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';

class CustomRadio extends StatefulWidget {
  List<AskDocRequestType>? requestData;

  CustomRadio({this.requestData});

  @override
  createState() {
    return new CustomRadioState();
  }
}

class CustomRadioState extends State<CustomRadio> {
  List<RadioModel>? sampleData = [];

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (widget.requestData != null) {
        widget.requestData!.forEach((element) {
          sampleData!.add(new RadioModel(false, element.description!, element.parameterCode!));
        });
      } else {
        sampleData!.add(new RadioModel(false, TranslationBase.of(context).appoReminder30, 30));
        sampleData!.add(new RadioModel(false, TranslationBase.of(context).appoReminder60, 60));
        sampleData!.add(new RadioModel(false, TranslationBase.of(context).appoReminder90, 90));
        sampleData!.add(new RadioModel(false, TranslationBase.of(context).appoReminder120, 120));
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ListView.builder(
          shrinkWrap: true,
          itemCount: sampleData!.length,
          itemBuilder: (BuildContext context, int index) {
            return new InkWell(
              //highlightColor: Colors.red,
              splashColor: Colors.transparent,
              onTap: () {
                setState(() {
                  sampleData!.forEach((element) => element.isSelected = false);
                  sampleData![index].isSelected = true;
                  if (widget.requestData != null) {
                    AskDocDialog.selectedParameterCode = sampleData![index].duration;
                  } else {
                    ReminderDialog.selectedDuration = sampleData![index].duration * 60000;
                    PrescriptionReminderDialog.selectedDuration = sampleData![index].duration * 60000;
                  }
                });
              },
              child: new RadioItem(sampleData![index]),
            );
          },
        ),
      ],
    );
  }
}

class RadioItem extends StatelessWidget {
  final RadioModel _item;

  RadioItem(this._item);

  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: new EdgeInsets.all(15.0),
      child: new Row(
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Container(
            height: 30.0,
            width: 30.0,
            child: new Center(
              child: Image.asset("assets/images/new-design/check_icon.png", width: 15.0, height: 15.0),
            ),
            decoration: new BoxDecoration(
              color: _item.isSelected ? Colors.blue : Colors.transparent,
              border: new Border.all(width: 1.0, color: _item.isSelected ? Colors.blue : Colors.grey),
              borderRadius: const BorderRadius.all(const Radius.circular(50.0)),
            ),
          ),
          new Container(
            margin: new EdgeInsets.only(left: 15.0, right: 15.0),
            child: new Text(_item.text, style: TextStyle(fontSize: 16.0)),
          ),
        ],
      ),
    );
  }
}

class RadioModel {
  bool isSelected;
  final String text;
  final int duration;

  RadioModel(this.isSelected, this.text, this.duration);
}
