import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:share_plus/share_plus.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:unit2/bloc/profile/education/education_bloc.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class EudcationViewAttachment extends StatefulWidget {
  const EudcationViewAttachment({super.key});

  @override
  State<EudcationViewAttachment> createState() =>
      _EudcationViewAttachmentState();
}

class _EudcationViewAttachmentState extends State<EudcationViewAttachment> {
  @override
  Widget build(BuildContext context) {
    String? fileUrl;
    String? filename;
    return Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () async {
            await _launchInBrowser(fileUrl!);
          },
          child: const Icon(Icons.file_download),
        ),
        appBar: AppBar(
          title: const Text("Attachment"),
          centerTitle: true,
          actions: [
            IconButton(
                onPressed: () {
                  context.read<EducationBloc>().add(
                      ShareAttachment(fileName: filename!, source: fileUrl!));
                },
                icon: const Icon(Icons.share)),
          ],
        ),
        body: ProgressHUD(
          padding: const EdgeInsets.all(24),
          backgroundColor: Colors.black87,
          indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
          child: BlocConsumer<EducationBloc, EducationState>(
            builder: (context, state) {
              if (state is EducationAttachmentViewState) {
                fileUrl = state.fileUrl;
                filename = state.fileName;

                bool isPDF = state.fileUrl[state.fileUrl.length - 1] == 'f'
                    ? true
                    : false;
                return SizedBox(
                  child: isPDF
                      ? SfPdfViewer.network(
                          state.fileUrl,
                          onDocumentLoadFailed: (details) {
                            Center(
                              child: Text(details.description),
                            );
                          },
                        )
                      : Center(
                          child: CachedNetworkImage(
                            progressIndicatorBuilder: (context, url, progress) {
                              return const SizedBox(
                                  height: 100,
                                  width: 100,
                                  child: CircularProgressIndicator(
                                    color: primary,
                                  ));
                            },
                            imageBuilder: (context, imageProvider) => Container(
                              decoration: BoxDecoration(
                                  image: DecorationImage(
                                      image: imageProvider, fit: BoxFit.fill)),
                            ),
                            imageUrl: state.fileUrl,
                            width: double.infinity,
                            height: 220,
                            fit: BoxFit.cover,
                          ),
                        ),
                );
              }
              return Container();
            },
            listener: (context, state) {
              if (state is EducationalBackgroundLoadingState) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is EducationAttachmentViewState) {
                final progress = ProgressHUD.of(context);
                progress!.dismiss();
              }
            },
          ),
        ));
  }

  Future<void> _launchInBrowser(String url) async {
    final UrlLauncherPlatform launcher = UrlLauncherPlatform.instance;
    if (!await launcher.launch(
      url,
      useSafariVC: false,
      useWebView: false,
      enableJavaScript: false,
      enableDomStorage: false,
      universalLinksOnly: false,
      headers: <String, String>{},
    )) {
      throw Exception('Could not launch $url');
    }
  }
}
