import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:tangheem/api/tangheem_user_api_client.dart';
import 'package:tangheem/classes/colors.dart';
import 'package:tangheem/classes/consts.dart';
import 'package:tangheem/classes/utils.dart';
import 'package:tangheem/models/content_info_model.dart';
import 'package:tangheem/ui/misc/no_data_ui.dart';

class PdfListScreen extends StatefulWidget {
  static const String routeName = "/tangheem_pdf";

  PdfListScreen({Key key}) : super(key: key);

  @override
  _PdfListScreenState createState() {
    return _PdfListScreenState();
  }
}

class _PdfListScreenState extends State<PdfListScreen> {
  List<ContentInfoDataModel> contentList;

  @override
  void initState() {
    super.initState();
    getPdfs();
  }

  void getPdfs() async {
    Utils.showLoading(context);
    try {
      var membersData = await TangheemUserApiClient().getContentInfo(8);
      contentList = membersData?.data ?? [];
    } catch (ex) {
      contentList = [];
      if (mounted) Utils.handleException(ex, null);
    } finally {
      Utils.hideLoading(context);
    }
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return contentList == null
        ? SizedBox()
        : contentList.isEmpty
            ? NoDataUI()
            : ListView.separated(
                physics: BouncingScrollPhysics(),
                padding: EdgeInsets.all(16),
                itemCount: contentList.length,
                separatorBuilder: (context, index) {
                  return SizedBox(height: 8);
                },
                itemBuilder: (context, index) {
                  return ListTile(
                    tileColor: Colors.white,
                    onTap: () {
                      Navigator.pushNamed(context, PdfViewerScreen.routeName, arguments: contentList[index]);
                    },
                    title: Text(
                      contentList[index].fileName?.trim() ?? "",
                      style: TextStyle(fontSize: 14, color: ColorConsts.primaryBlue),
                    ),
                    subtitle: Text(
                      contentList[index].contentTypeNameAr?.trim() ?? "",
                      style: TextStyle(fontSize: 12, color: ColorConsts.primaryBlue),
                    ),
                  );
                },
              );
  }
}

class PdfViewerScreen extends StatefulWidget {
  static const String routeName = "/tangheem_pdf_view";
  final ContentInfoDataModel pdfDetail;

  PdfViewerScreen(this.pdfDetail, {Key key}) : super(key: key);

  @override
  _PdfViewerScreenState createState() {
    return _PdfViewerScreenState();
  }
}

class _PdfViewerScreenState extends State<PdfViewerScreen> {
  final GlobalKey<SfPdfViewerState> _pdfViewerKey = GlobalKey();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SfPdfViewer.network(
      ApiConsts.baseUrl + widget.pdfDetail.exposeFilePath,
      key: _pdfViewerKey,
      canShowScrollHead: false,
      enableTextSelection: false,
      enableDocumentLinkAnnotation: false,
      canShowPaginationDialog: false,
      canShowScrollStatus: false,
    );
  }
}
