import 'package:flutter_full_pdf_viewer/flutter_full_pdf_viewer.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_plugin.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:liftmanager/res/resources.dart';

import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:path_provider/path_provider.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/net/api_service.dart';

// class MeansPDF extends StatefulWidget {
//   @override
//   MeansPDFState createState() => new MeansPDFState();
// }

// class MeansPDFState extends State<MeansPDF> {
//   String pathPDF = "";

//   @override
//   void initState() {
//     super.initState();
//     createFileOfPdfUrl().then((f) {
//       setState(() {
//         pathPDF = f.path;
//         print(pathPDF);
//       });
//     });
//   }

//   Future<File> createFileOfPdfUrl() async {
//     final url = "http://africau.edu/images/default/sample.pdf";
//     final filename = url.substring(url.lastIndexOf("/") + 1);
//     var request = await HttpClient().getUrl(Uri.parse(url));
//     var response = await request.close();
//     var bytes = await consolidateHttpClientResponseBytes(response);
//     String dir = (await getApplicationDocumentsDirectory()).path;
//     File file = new File('$dir/$filename');
//     await file.writeAsBytes(bytes);
//     return file;
//   }

//   @override
//   Widget build(BuildContext context) {
//     return Container(
//       child: PDFScreen(pathPDF)
//     )
//     ;
//   }
//   // Widget build(BuildContext context) {
//   //   return Scaffold(
//   //     appBar: AppBar(title: const Text('Plugin example app')),
//   //     body: Center(
//   //       child: RaisedButton(
//   //         child: Text("Open PDF"),
//   //         onPressed: () => Navigator.push(
//   //           context,
//   //           MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),
//   //         ),
//   //       ),
//   //     ),
//   //   );
//   // }
// }

class PDFScreen extends StatelessWidget {
  String pathPDF = "";
  String fileUrl = "";
  String id = "";
  PDFScreen(this.pathPDF, this.fileUrl,this.id);

  getUpdateDownLoadNum() {
    // showLoading(context);
    NewApiService().getUpdateDownLoadNum(id, onSuccess: (res) {
      
    }, onError: (code, msg) {
      // dismissLoading(context);
      toasts(msg);
    });
  }

  getViewNum() {
    // showLoading(context);
    NewApiService().getLiftFilesDetail(id, onSuccess: (res) {
      
    }, onError: (code, msg) {
      // dismissLoading(context);
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    print("123456789");
    print(pathPDF);
    print(fileUrl);
    print(id);
    print("123456789");
    if(id!=null&&id!="null"){
      getViewNum();
    }
    Future<String> _findLocalPath() async {
      //这里根据平台获取当前安装目录
      final directory = Theme.of(context).platform == TargetPlatform.android
          ? await getExternalStorageDirectory()
          : await getApplicationDocumentsDirectory();
      return directory.path;
    }

    return PDFViewerScaffold(
        appBar: MyAppBar(
          centerTitle: fileUrl!=null&&fileUrl!="null"?'电梯资料库':"详情",
          actions: <Widget>[
            fileUrl!=null&&fileUrl!="null"? FlatButton(
              child: Text("下载"),
              textColor: Colours.dark_text,
              highlightColor: Colors.transparent,
              onPressed: () async {
                String _localPath = (await _findLocalPath()) + '/Download/';
                var pos = fileUrl.lastIndexOf("/");
                if (pos == -1) {
                  pos = fileUrl.lastIndexOf("\\");
                }
                var filename = fileUrl.substring(pos + 1);
                Response responce =
                    await Dio().download(fileUrl, _localPath + filename);
                if (responce.statusCode == 200) {
                  // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载成功")));
                  getUpdateDownLoadNum();
                  toasts("下载成功");
                } else {
                  // Scaffold.of(context).showSnackBar(SnackBar(content: Text("下载失败")));
                  toasts("下载失败");
                }
              },
            ):Container(child:null,)
          ],
        ),
        path: pathPDF);
  }
}
