import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:unit2/bloc/profile/education/education_bloc.dart';
import 'package:unit2/bloc/profile/eligibility/eligibility_bloc.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../../utils/urls.dart';
class EligibilityViewAttachment extends StatefulWidget {

  const EligibilityViewAttachment({super.key});

  @override
  State<EligibilityViewAttachment> createState() => _EligibilityViewAttachmentState();
}

class _EligibilityViewAttachmentState extends State<EligibilityViewAttachment> {
  @override
  Widget build(BuildContext context) {
    String? fileUrl;
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: ()async {
          await launchUrl(Uri.parse(fileUrl!));
        },
        child: const Icon(Icons.file_download),
      ),
      appBar: AppBar(
        title: const Text("Attachment"),
        centerTitle: true,
        actions: [
          IconButton(onPressed: () {}, icon: const Icon(Icons.share)),
        ],
      ),
      body: BlocConsumer<EligibilityBloc, EligibilityState>(builder: (context,state){
        if(state is EligibilityAttachmentViewState){
          fileUrl = state.fileUrl;
          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) {
        
      },)
    );
  }
}
