/*
 * @Author: XianKaiQun
 * @Date: 2020-07-22 10:14:52
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-03-30 14:50:12
 * @Description: 
 */

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:universal_file_viewer/universal_file_viewer.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

///文件预览
class WisPreviewUtil extends StatefulWidget {
  WisPreviewUtil({
    Key? key,
    required this.url,
    required this.name,
    this.title = '文件预览',
    this.options,
  }) : super(key: key);
  final String url;

  ///文件名  务必带上拓展名
  final String name;

  ///标题
  final String title;

  // 请求头
  final Options? options;

  static Future show(
      {String? url,
      String? name,
      String title = '文件预览',
      Options? options}) async {
    return WNavUtil.instance!.push(
      CupertinoPageRoute(
        builder: (_) => WisPreviewUtil(
          url: url!,
          name: name!,
          title: title,
          options: options,
        ),
      ),
    );
  }

  @override
  _WisPreviewUtilState createState() => _WisPreviewUtilState();
}

class _WisPreviewUtilState extends State<WisPreviewUtil> {
  String? path;
  int count = 0;
  int total = 0;
  dl() async {
    path = await WisDownloadUtil.intent(widget.url, widget.name,
        onReceiveProgress: (c, t) {
      count = c;
      total = t;
      setState(() {});
    }, options: widget.options);
    print('------------------------------------');
    print(path);
    setState(() {});
    if (path == null) {
      Navigator.pop(context);
    }
  }

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

  @override
  Widget build(BuildContext context) {
    double value = 0;
    if (count != 0 && total != 0) value = count / total;
    final colorScheme = WTheme.of(context).colorScheme;
    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        iconTheme: IconThemeData(
          color: colorScheme.mainTitle,
          size: 19.pt,
        ),
        backgroundColor: Colors.white,
        title: WisText('文件预览',
            size: 19.pt, weight: FontWeight.w600, color: colorScheme.mainTitle),
      ),
      body: path == null
          ? Center(
              child: Wisdom.column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    WisText('资源正在加载中...'),
                    Wisdom.row(
                        // width: 250,
                        // alignment: Alignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Wisdom(
                            width: 250,
                            // alignment: Alignment.center,
                            child: ClipRRect(
                              // 边界半径（`borderRadius`）属性，圆角的边界半径。
                              borderRadius:
                                  BorderRadius.all(Radius.circular(4.0)),
                              child: LinearProgressIndicator(
                                  minHeight: 8,
                                  //0~1的浮点数，用来表示进度多少;如果 value 为 null 或空，则显示一个动画，否则显示一个定值
                                  value: value,
                                  //背景颜色
                                  backgroundColor: Colors.grey,
                                  //进度颜色
                                  valueColor: new AlwaysStoppedAnimation<Color>(
                                      Color(0XFF0F67F8))),
                            ),
                          ),
                          SizedBox(
                            width: 10.0,
                          ),
                          WisText('${(value * 100).toStringAsFixed(2)}%')
                        ]),
                  ]),
            )
          : UniversalFileViewer(
              filePath: path!,
            ),
    );
  }
}
