import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../../api/mhsl/message/getMessages/getMessagesResponse.dart';
import '../../../../widget/confirmDialog.dart';

class MessageView extends StatefulWidget {
  final String basePath;
  final GetMessagesResponseObject message;
  const MessageView({super.key, required this.basePath, required this.message});

  @override
  State<MessageView> createState() => _MessageViewState();
}

class _MessageViewState extends State<MessageView> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.message.name),
      ),
      body: SfPdfViewer.network(
        widget.basePath + widget.message.url,
        enableHyperlinkNavigation: true,
        onDocumentLoadFailed: (PdfDocumentLoadFailedDetails e) {
          Navigator.of(context).pop();
          showDialog(context: context, builder: (context) {
            return AlertDialog(
              title: const Text("Fehler beim öffnen"),
              content: Text("Dokument '${widget.message.name}' konnte nicht geladen werden:\n${e.description}"),
              actions: [
                TextButton(onPressed: () {
                  Navigator.of(context).pop();
                }, child: const Text("Ok"))
              ],
            );
          });
        },
        onHyperlinkClicked: (PdfHyperlinkClickedDetails e) {
          showDialog(
            context: context,
            builder: (context) => ConfirmDialog(
              title: "Link öffnen",
              content: "Möchtest du den folgenden Link öffnen?\n${e.uri}",
              confirmButton: "Öffnen",
              onConfirm: () => launchUrl(Uri.parse(e.uri), mode: LaunchMode.externalApplication),
            ),
          );
        },
      ),
    );
  }
}
