import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:unit2/theme-data.dart/colors.dart';
import 'package:unit2/widgets/error_state.dart';

import '../../../utils/urls.dart';

class ImageAttachment extends StatefulWidget {
  final String imgUrl;
  const ImageAttachment({super.key, required this.imgUrl});

  @override
  State<ImageAttachment> createState() => _ImageAttachmentState();
}

class _ImageAttachmentState extends State<ImageAttachment> {
  @override
  Widget build(BuildContext context) {
    bool isPDF = widget.imgUrl[widget.imgUrl.length - 1] == 'f' ? true : false;

    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {},
        child: const Icon(Icons.file_download),
      ),
      appBar: AppBar(
        title: const Text("Attachment"),
        centerTitle: true,
        actions: [
          IconButton(onPressed: () {}, icon: const Icon(Icons.share)),
        ],
      ),
      body: isPDF
          ? SfPdfViewer.network(
              '${Url.instance.prefixHost()}://${Url.instance.host()}${widget.imgUrl}',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:
                    '${Url.instance.prefixHost()}://${Url.instance.host()}${widget.imgUrl}',
                width: double.infinity,
                height: 220,
                fit: BoxFit.cover,
              ),
            ),
    );
  }
}
