import 'package:diplomaticquarterapp/pages/MyAppointments/widgets/custom_radio.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:diplomaticquarterapp/uitl/date_uitl.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:manage_calendar_events/manage_calendar_events.dart';

class ReminderDialog extends StatefulWidget {
  static var selectedDuration;

  final String eventId;
  final String title;
  final String description;
  final String startDate;
  final String endDate;
  final String location;

  ReminderDialog(
      {@required this.eventId,
      @required this.title,
      @required this.description,
      @required this.startDate,
      @required this.endDate,
      @required this.location});

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

class _ReminderDialogState extends State<ReminderDialog> {
  final CalendarPlugin _myPlugin = CalendarPlugin();

  @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: RaisedButton(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0), side: BorderSide(color: Colors.blue)),
                color: Colors.blue,
                elevation: 0,
                onPressed: () {
                  print(ReminderDialog.selectedDuration);
                  createCalendarEvent();
                },
                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: 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)),
              ),
            ),
          ]),
        ),
      ),
    );
  }

  createCalendarEvent() {
    _myPlugin.hasPermissions().then((value) {
      if (!value) {
        _myPlugin.requestPermissions();
      } else {
        _myPlugin.getCalendars().then((value) => {print(value.length)});
      }
    });

    CalendarEvent calendarEvent = new CalendarEvent(
        eventId: widget.eventId,
        title: widget.title,
        description: widget.description,
        startDate: DateUtil.convertStringToDate(widget.startDate)
            .subtract(new Duration(microseconds: ReminderDialog.selectedDuration)),
        endDate: DateUtil.convertStringToDate(widget.endDate),
        location: widget.location,
        duration: new Duration(minutes: 15).inMinutes,
        isAllDay: false,
        hasAlarm: true);

    _myPlugin.createEvent(calendarId: "207749556", event: calendarEvent).then((value) {
      print("Cal event");
      print(value);
      if (int.parse(value) == int.parse(widget.eventId)) {
        AppToast.showSuccessToast(message: TranslationBase.of(context).reminderSuccess);
      }
      Navigator.of(context).pop();
    }).catchError((err) {
      print("Cal Error");
      print(err);
      Navigator.of(context).pop();
    });
  }
}
