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:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:unit2/bloc/profile/learningDevelopment/learning_development_bloc.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/utils/url_launcher_file_downloader.dart';
import 'package:url_launcher/url_launcher.dart';

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

  @override
  State<LearningDevelopmentViewAttachment> createState() =>
      _LearningDevelopmentViewAttachmentState();
}

class _LearningDevelopmentViewAttachmentState
    extends State<LearningDevelopmentViewAttachment> {
  @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: context.watch<LearningDevelopmentBloc>().state is LearningAndDevelopmentAttachmentViewState ? [
            IconButton(onPressed: () {
              context.read<LearningDevelopmentBloc>().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<LearningDevelopmentBloc, LearningDevelopmentState>(
            builder: (context, state) {
              if (state is LearningAndDevelopmentAttachmentViewState) {
                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,
              
                            fit: BoxFit.fitHeight,
                          ),
                        ),
                );
              }
              return Container();
            },
            listener: (context, state) {
             
              if (state is LearningDevelopmentLoadingState) {
                final progress = ProgressHUD.of(context);
                progress!.showWithText("Please wait...");
              }
              if (state is LearningAndDevelopmentAttachmentViewState ||
                  state is LearningDevelopmentErrorState) {
                final progress = ProgressHUD.of(context);
                progress!.dismiss();
              }
            },
          ),
        ));
  }
}
