import 'package:hmg_patient_app/pages/MyAppointments/widgets/custom_radio.dart';
import 'package:hmg_patient_app/uitl/CalendarUtils.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/buttons/custom_text_button.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:flutter/material.dart';

class PrescriptionReminderDialog extends StatefulWidget {
  static var selectedDuration;

  final String eventId;
  final String title;
  final String description;
  final DateTime startDate;
  final DateTime endDate;
  final String location;
  final int days;

  List<DateTime> _scheduleList =[];

  PrescriptionReminderDialog({
    required this.eventId,
    required this.title,
    required this.description,
    required this.startDate,
    required this.endDate,
    required this.location,
    required this.days,
  });

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

class _ReminderDialogState extends State<PrescriptionReminderDialog> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Dialog(
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
        child: Container(
          // height: MediaQuery.of(context).size.height * 0.57,
          width: 450.0,
          child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.min, children: <Widget>[
            Container(
              margin: EdgeInsets.all(20.0),
              child: Text(TranslationBase.of(context).setReminder, style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold)),
            ),
            Container(
              transform: Matrix4.translationValues(0.0, -30.0, 0.0),
              child: CustomRadio(),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              height: 40.0,
              margin: EdgeInsets.only(left: 30.0, top: 0.0, right: 30.0),
              child: CustomTextButton(
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0), side: BorderSide(color: Colors.blue)),
                  backgroundColor: Colors.blue,
                  elevation: 0,
                onPressed: () {
                  createOrUpdateEvents();
                },
                child: Text(TranslationBase.of(context).confirm, style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width,
              margin: EdgeInsets.only(left: 100.0, top: 20.0, right: 100.0, bottom: 20.0),
              child: DefaultButton(
                TranslationBase.of(context).cancel_nocaps,
                () {
                  Navigator.of(context).pop();
                },
              ),
              // OutlineButton(
              //   shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
              //   color: Colors.red,
              //   borderSide: BorderSide(color: Colors.red),
              //   highlightColor: Colors.red,
              //   highlightedBorderColor: Colors.red,
              //   onPressed: () {
              //     Navigator.of(context).pop();
              //   },
              //   child: Text(TranslationBase.of(context).cancel_nocaps, style: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
              // ),
            ),
          ]),
        ),
      ),
    );
  }

  createOrUpdateEvents() async {
    Navigator.pop(context);

    for (int count = 0; count < widget.days; count++) {
      widget._scheduleList.add(DateTime(DateTime.now().year, DateTime.now().month, DateTime.now().day));
    }

    List<DateTime> scheduleDateTime = calculateDaysInterval(widget.startDate.subtract(new Duration(microseconds: PrescriptionReminderDialog.selectedDuration)), widget.endDate);

    CalendarUtils calendarUtils = await CalendarUtils.getInstance();

    calendarUtils.createOrUpdateEvents(scheduleList: widget._scheduleList, description: widget.description, title: widget.title, scheduleDateTime: scheduleDateTime);

    AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess);
  }

  List<DateTime> calculateDaysInterval(DateTime startDate, DateTime endDate) {
    List<DateTime> days = [];
    for (int i = 0; i <= endDate.difference(startDate).inDays; i++) {
      widget._scheduleList.forEach((element) {
        days.add(startDate.add(Duration(days: i, hours: element.hour, minutes: element.minute)));
      });
    }
    return days;
  }
}
