
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';
import 'package:persistent_bottom_nav_bar/persistent_tab_view.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';

import '../../../api/webuntis/queries/getRooms/getRoomsResponse.dart';
import '../../../api/webuntis/queries/getSubjects/getSubjectsResponse.dart';
import '../../../api/webuntis/queries/getTimetable/getTimetableResponse.dart';
import '../../../model/timetable/timetableProps.dart';
import '../../../widget/debug/debugTile.dart';
import '../../../widget/unimplementedDialog.dart';
import '../more/roomplan/roomplan.dart';

class AppointmentDetails {
  static String _getEventPrefix(String? code) {
    if(code == "cancelled") return "Entfällt: ";
    if(code == "irregular") return "Änderung: ";
    return code ?? "";
  }
  static void show(BuildContext context, TimetableProps webuntisData, Appointment appointment) {
    GetTimetableResponseObject timetableData = appointment.id as GetTimetableResponseObject;

    //GetTimetableResponseObject timetableData = webuntisData.getTimetableResponse.result.firstWhere((element) => element.id == timetableObject.id);
    GetSubjectsResponseObject subject;
    GetRoomsResponseObject room;

    try {
      subject = webuntisData.getSubjectsResponse.result.firstWhere((subject) => subject.id == timetableData.su[0].id);
    } catch(e) {
      subject = GetSubjectsResponseObject(0, "?", "Unbekannt", "?", true);
    }

    try {
      room = webuntisData.getRoomsResponse.result.firstWhere((room) => room.id == timetableData.ro[0].id);
    } catch(e) {
      room = GetRoomsResponseObject(0, "?", "Unbekannt", true, "?");
    }

    showModalBottomSheet(context: context, builder: (context) => Column(
      children: [
        Padding(
          padding: const EdgeInsets.symmetric(vertical: 30),
          child: Center(
            child: Column(
              children: [
                Icon(Icons.info, color: appointment.color),
                const SizedBox(height: 10),
                Text("${_getEventPrefix(timetableData.code)}${subject.alternateName} - (${subject.longName})", style: const TextStyle(fontSize: 30)),
                Text("${Jiffy.parseFromDateTime(appointment.startTime).format(pattern: "HH:mm")} - ${Jiffy.parseFromDateTime(appointment.endTime).format(pattern: "HH:mm")}", style: const TextStyle(fontSize: 15)),
              ],
            ),
          ),
        ),

        Expanded(
          child: ListView(
            children: [
              ListTile(
                leading: const Icon(Icons.notifications_active),
                title: Text("Status: ${timetableData.code != null ? "Geändert" : "Regulär"}"),
              ),
              ListTile(
                leading: const Icon(Icons.room),
                title: Text("Raum: ${room.name}"),
                trailing: IconButton(
                  icon: const Icon(Icons.house_outlined),
                  onPressed: () {
                    PersistentNavBarNavigator.pushNewScreen(context, withNavBar: false, screen: const Roomplan());
                  },
                ),
              ),
              ListTile(
                leading: const Icon(Icons.person),
                title: Text("Lehrkraft: (${timetableData.te[0].name}) ${timetableData.te[0].longname}"),
                trailing: Visibility(
                  visible: !kReleaseMode,
                  child: IconButton(
                    icon: const Icon(Icons.textsms_outlined),
                    onPressed: () {
                      UnimplementedDialog.show(context);
                    },
                  ),
                ),
              ),
              ListTile(
                leading: const Icon(Icons.abc),
                title: Text("Typ: ${timetableData.activityType}"),
              ),
              ListTile(
                leading: const Icon(Icons.people),
                title: Text("Klasse(n): ${timetableData.kl.map((e) => e.name).join(", ")}"),
              ),
              DebugTile(timetableData.toJson()).asTile(context),
            ],
          ),
        )
      ],
    ));
  }
}