import 'dart:async';

import 'package:fcs/helpers/theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_pdfview/flutter_pdfview.dart';

class PaymentPDFScreen extends StatefulWidget {
  final String path;

  PaymentPDFScreen({Key key, this.path}) : super(key: key);

  _PaymentPDFScreenState createState() => _PaymentPDFScreenState();
}

class _PaymentPDFScreenState extends State<PaymentPDFScreen>
    with WidgetsBindingObserver {
  final Completer<PDFViewController> _controller =
      Completer<PDFViewController>();
  int pages = 0;
  int currentPage = 0;
  bool isReady = false;
  String errorMessage = '';

  @override
  Widget build(BuildContext context) {
    print(' widget.path => ${widget.path}');
    print(' pages => ${pages}');
    return Scaffold(
      appBar: AppBar(
        backgroundColor: primaryColor,
        title: Text("Invoice File"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () {},
          ),
        ],
      ),
      body: Stack(
        children: <Widget>[
          PDFView(
            filePath: widget.path,
            enableSwipe: true,
            swipeHorizontal: true,
            autoSpacing: false,
            pageFling: true,
            pageSnap: true,
            defaultPage: currentPage,
            fitPolicy: FitPolicy.BOTH,
            preventLinkNavigation:
                false, // if set to true the link is handled in flutter
            onRender: (_pages) {
              print(('pages => $pages'));
              setState(() {
                pages = _pages;
                isReady = true;
              });
            },
            // onError: (error) {
            //   setState(() {
            //     errorMessage = error.toString();
            //   });
            //   print(error.toString());
            // },
            // onPageError: (page, error) {
            //   setState(() {
            //     errorMessage = '$page: ${error.toString()}';
            //   });
            //   print('$page: ${error.toString()}');
            // },
            onViewCreated: (PDFViewController pdfViewController) {
              _controller.complete(pdfViewController);
            },
            onLinkHandler: (String uri) {
              print('goto uri: $uri');
            },
            onPageChanged: (int page, int total) {
              print('page change: $page/$total');
              setState(() {
                currentPage = page;
              });
            },
          ),
          //   errorMessage.isEmpty
          //       ? !isReady
          //           ? Center(
          //               child: CircularProgressIndicator(),
          //             )
          //           : Container()
          //       : Center(
          //           child: Text(errorMessage),
          //         )
        ],
      ),
      // floatingActionButton: FutureBuilder<PDFViewController>(
      //   future: _controller.future,
      //   builder: (context, AsyncSnapshot<PDFViewController> snapshot) {
      //     if (snapshot.hasData) {
      //       return FloatingActionButton.extended(
      //         label: Text("Go to ${pages ~/ 2}"),
      //         onPressed: () async {
      //           await snapshot.data.setPage(pages ~/ 2);
      //         },
      //       );
      //     }

      //     return Container();
      //   },
      // ),
    );
  }
}
