import 'dart:io';
import 'dart:math';

import 'package:better_open_file/better_open_file.dart';
import 'package:flutter/material.dart';
import 'package:marianum_mobile/storage/base/settingsProvider.dart';
import 'package:photo_view/photo_view.dart';
import 'package:provider/provider.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

import 'placeholderView.dart';

class FileViewer extends StatefulWidget {
  final String path;
  final bool openExternal;
  const FileViewer({super.key, required this.path, this.openExternal = false});

  @override
  State<FileViewer> createState() => _FileViewerState();
}

class _FileViewerState extends State<FileViewer> {
  PhotoViewController photoViewController = PhotoViewController();

  late SettingsProvider settings = Provider.of<SettingsProvider>(context, listen: false);
  late bool openExternal;

  @override
  void initState() {
    openExternal = settings.val().fileViewSettings.alwaysOpenExternally || widget.openExternal;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    AppBar appbar({List actions = const []}) {
      return AppBar(
        title: Text(widget.path.split("/").last),
        actions: [
          IconButton(
              onPressed: () => Navigator.of(context).push(
                  MaterialPageRoute(builder: (context) => FileViewer(path: widget.path, openExternal: true))
              ),
              icon: const Icon(Icons.open_in_new)
          ),
          ...actions
        ],
      );
    }

    switch(openExternal ? "" : widget.path.split(".").last.toLowerCase()) {
      case "png":
      case "jpg":
      case "jpeg":
      case "webp":
      case "gif":
        return Scaffold(
            appBar: appbar(
                actions: [
                  IconButton(onPressed: () {
                    setState(() {
                      photoViewController.rotation += pi/2;
                    });
                  }, icon: const Icon(Icons.rotate_right)),
                ]
            ),
            backgroundColor: Colors.white,
            body: PhotoView(
              controller: photoViewController,
              imageProvider: Image.file(File(widget.path)).image,
              backgroundDecoration: BoxDecoration(color: Theme.of(context).colorScheme.surface),
            )
        );


      case "pdf":
        return Scaffold(
          appBar: appbar(),
          body: SfPdfViewer.file(
            File(widget.path),
          ),
        );

      default:
        OpenFile.open(widget.path).then((result) {
          Navigator.of(context).pop();
          if(result.type != ResultType.done) {
            showDialog(context: context, builder: (context) {
              return AlertDialog(
                content: Text(result.message),
              );
            });
          }
        });

        return PlaceholderView(
          text: "Datei extern geöffnet",
          icon: Icons.open_in_new,
          button: TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text("Zurück"),
          ),
        );
    }
  }
}
