import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/TokenMgr.dart';
import 'package:chat/utils/controller_widget_builder.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
import 'package:video_player/video_player.dart';

class VideoPage extends StatefulWidget {
  final userId;
  VideoPage({Key key, @required this.userId}) : super(key: key);

  _VideoPageState createState() => _VideoPageState();
}

class _VideoPageState extends State<VideoPage> {
  VideoPlayerController _controller;
  bool isShowPlayButton = true;

  bool isLoadingFish = false;

  IjkMediaController controller = IjkMediaController();

  @override
  void initState() {
    super.initState();
    SystemChrome.setEnabledSystemUIOverlays([]);
    getInfo();
  }

  @override
  void dispose() {
    SystemChrome.setEnabledSystemUIOverlays(SystemUiOverlay.values);
    _controller.dispose();
    super.dispose();
  }

  getInfo() async {
    Map data = {
      "userId": widget.userId,
    };
    data['sign'] = TokenMgr().getSign(data);

    Response res = await HttpUtil().post('user/female/authInfo',
        data: data, failback: () => Navigator.of(context).pop());
    Map resData = res.data;
    print(resData);
    if (resData['code'] == 0) {
      var videoUrl = resData['data']['AttestationValue'];
      if (videoUrl != '' && videoUrl != null) {
//        _controller = VideoPlayerController.network(videoUrl)
//          ..initialize().then((_) {
//            isLoadingFish = true;
//            setState(() {});
//          });
//        _controller.addListener(() {
//          if (_controller.value.position >= _controller.value.duration) {
//            isShowPlayButton = true;
//            setState(() {});
//          }
//        });
        isLoadingFish = true;
        setState(() {});
        WidgetsBinding.instance.addPostFrameCallback((_) {
          controller.setNetworkDataSource(videoUrl, autoPlay: false);
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: new Text(
        I18n.of(context).certified_video,
        textScaleFactor: 1.0,
        style: TextStyle(color: AppColors.NewAppbarTextColor),
      ),
      centerTitle: true,
      leading: CustomUI.buildCustomLeading(context),
    );

    Widget content = Scaffold(
      backgroundColor: const Color(0xFFF4F4F4),
      body: Center(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: _buildVideo(),
        ),
      ),
      appBar: appBar,
      resizeToAvoidBottomPadding: false,
    );
    return CustomUI.buildPageLoading(context, content, !isLoadingFish);
  }

  Widget _buildVideo() {
    return IjkPlayer(
      controllerWidgetBuilder: (ctl) {
        return MyDefaultIJKControllerWidget(
          ///按钮修改
          controller: ctl,
          hideBackButton: true,
        );
      },
      mediaController: controller,
    );

//    Widget stack = Stack(
//      alignment: Alignment.center,
//      children: <Widget>[
//
//        Container(
//          width: Screen.width,
//          child: _controller != null && _controller.value.initialized
//              ? AspectRatio(
//                  aspectRatio: _controller.value.aspectRatio,
//                  child: VideoPlayer(_controller),
//                )
//              : Container(),
//        ),
//        InkWell(
//            highlightColor: Colors.transparent,
//            radius: 0,
//            onTap: () {
//              setState(() {
//                if (!_controller.value.isPlaying) {
//                  _controller.seekTo(Duration());
//                  _controller.play();
//                  isShowPlayButton = false;
//                  setState(() {});
//                }
//              });
//            },
//            child: Container(
//              height: MediaQuery.of(context).size.height,
//              color: Colors.black.withOpacity(
//                  _controller != null && _controller.value.isPlaying ? 0 : 0.5),
//              padding: EdgeInsets.only(bottom: 20),
//              alignment: Alignment.center,
//              child: Image.asset(
//                'assets/images/bofang.png',
//                width:
//                    _controller != null && _controller.value.isPlaying ? 0 : 70,
//              ),
//            ))
//      ],
//    );
//    return stack;
  }
}
