import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:liftmanager/internal/bbs/model/video_detail.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/time_format.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/utils/utils.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:orientation/orientation.dart';
import 'package:video_player/video_player.dart';

class VideoDetail extends StatefulWidget {
  VideoDetail(this.id);

  final String id;

  @override
  State<StatefulWidget> createState() {
    return VideoDetailState();
  }
}

class VideoDetailState extends State<VideoDetail> {
  @override
  void initState() {
    super.initState();
    getVideoDetail();
  }

  // final FijkPlayer player = FijkPlayer();
  bool _hasData = false;
  VideoDetailModel detailObj;
  VideoPlayerController _controller;

  Future getVideoDetail() async {
    await NewApiService().getVideoDetail(int.parse(widget.id), 1,
        onSuccess: (res) async {
      if (res != null) {
        _hasData = true;
        detailObj = res;
        _controller =
            VideoPlayerController.network(Utils.getImagePath(detailObj.url)
                // imgFontUrl + detailObj.url
                );
        // player.setDataSource(detailObj.url, autoPlay: false,showCover: true);
        // player.setOption(FijkOption.playerCategory, "cover-after-prepared", 1);
        // var imageData = await player.takeSnapShot();
        // Image.memory(imageData);
        // MemoryImage(imageData);
        // I.Image _img = I.decodeImage(imageData);
        // _img = I.encodeJpg(_img) as I.Image;
        // String s = new String.fromCharCodes(imageData);
        // var outputAsUint8List = new Uint8List.fromList(s.codeUnits);
        // print(123456);
        // print(outputAsUint8List);
        // print(1234561);
        setState(() {});
      }
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    if (_controller != null) {
      _controller.pause();
      _controller.dispose();
    }

    // SystemChrome.setPreferredOrientations([
    //   DeviceOrientation.portraitUp,
    // ]);
    // player.release();
    // OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;
    if (width > height) {
      // SystemChrome.setPreferredOrientations([
      //   DeviceOrientation.portraitUp,
      // ]);
      OrientationPlugin.forceOrientation(DeviceOrientation.portraitUp);
    }
    return Scaffold(
      resizeToAvoidBottomPadding: false, //不让键盘弹上去
      appBar: MyAppBar(
        centerTitle: "视频详情",
        actions: <Widget>[
          _hasData && detailObj.checkFlag == 0
              ? FlatButton(
                  child: Text(
                    "编辑",
                    style: TextStyle(
                        color: Colours.tip_text_black,
                        fontSize: ScreenUtil().setSp(15)),
                  ),
                  textColor: Colours.tip_text_black,
                  highlightColor: Colors.transparent,
                  onPressed: () {
                    // NavigatorUtils.push(context, WodeRouter.videoUpload);
                    if (_controller != null) {
                      _controller.pause();
                    }
                    NavigatorUtils.push(
                        context, "${WodeRouter.videoUpload}?id=${widget.id}");
                  },
                )
              : Container(
                  child: null,
                )
        ],
      ),
      body: _hasData
          ? Stack(
              children: <Widget>[
                Container(
                    child: ListView(children: <Widget>[
                  Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                          padding: EdgeInsets.only(
                              left: ScreenUtil().setWidth(15),
                              right: ScreenUtil().setWidth(15),
                              top: ScreenUtil().setWidth(15)),
                          child: ClipRRect(
                            borderRadius: BorderRadius.circular(5),
                            child: new Chewie(
                              controller: ChewieController(
                                videoPlayerController: _controller,
                                aspectRatio: 3 / 2,
                                autoPlay: false,
                                looping: true,
                                showControls: true,
                                deviceOrientationsAfterFullScreen: [
                                  DeviceOrientation.portraitUp
                                ],
                                // 占位图
                                // placeholder: Image.network(
                                //     imgFontUrl+detailObj.cover,
                                //     fit: BoxFit.contain,

                                //   ),

                                // 是否在 UI 构建的时候就加载视频
                                autoInitialize: true,

                                // 拖动条样式颜色
                                materialProgressColors:
                                    new ChewieProgressColors(
                                  playedColor: Colors.red,
                                  handleColor: Colors.blue,
                                  backgroundColor: Colors.grey,
                                  bufferedColor: Colors.lightGreen,
                                ),
                              ),
                            ),
                          )),
                      Container(
                        width: width,
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(15),
                            right: ScreenUtil().setWidth(15),
                            top: ScreenUtil().setWidth(10),
                            bottom: ScreenUtil().setWidth(10)),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(width: 0.5, color: Colours.line),
                          ),
                        ),
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text(
                                detailObj.title,
                                maxLines: 2,
                                overflow: TextOverflow.ellipsis,
                                style: TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: ScreenUtil().setSp(16)),
                                textAlign: TextAlign.start,
                              ),
                              SizedBox(
                                height: 15,
                              ),
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Container(
                                    padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                                    decoration: new BoxDecoration(
                                      //背景
                                      color: Color(0x21438DFA),
                                      //设置四周圆角 角度
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(4.0)),
                                    ),
                                    child: Text(
                                      detailObj.brandName,
                                      style: TextStyle(
                                          color: Color(0xff5887FF),
                                          fontSize: ScreenUtil().setSp(14)),
                                      textAlign: TextAlign.start,
                                    ),
                                  ),
                                  Expanded(child: Container()),
                                  Text(
                                    '${detailObj.browseNum}浏览·${detailObj.likeNum}赞',
                                    style: TextStyle(color: Color(0xffCCCCCC)),
                                  )
                                ],
                              )
                            ]),
                      ),
                      Container(
                        padding: EdgeInsets.only(
                          left: ScreenUtil().setWidth(15),
                          top: ScreenUtil().setWidth(30),
                        ),
                        decoration: BoxDecoration(
                          border: Border(
                            bottom: BorderSide(width: 0.5, color: Colours.line),
                          ),
                        ),
                        child: Row(
                          children: <Widget>[
                            Container(
                                child: ClipRRect(
                              borderRadius: BorderRadius.circular(
                                  ScreenUtil().setWidth(18)),
                              child: LoadNetworkImage(
                                detailObj.avatarUrl,
                                // fit: BoxFit.cover,
                                width: ScreenUtil().setWidth(36),
                                height: ScreenUtil().setWidth(36),
                              ),
                            )),
                            Container(
                              padding: EdgeInsets.only(left: 5),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Text(
                                    detailObj.nickname,
                                    style: TextStyle(
                                        fontSize: ScreenUtil().setSp(14),
                                        color: Color(0xff333333)),
                                  ),
                                ],
                              ),
                            ),
                            SizedBox(
                              width: 10,
                            ),
                            // Container(
                            //   decoration: new BoxDecoration(
                            //     //背景
                            //     color: Color(0xffFD7F5D),
                            //     //设置四周圆角 角度
                            //     borderRadius:
                            //         BorderRadius.all(Radius.circular(4.0)),
                            //     //设置四周边框
                            //   ),
                            //   padding: EdgeInsets.fromLTRB(5, 2, 5, 2),
                            //   child: Text(
                            //     'LV1',
                            //     style: TextStyle(color: Colors.white),
                            //   ),
                            // )
                          ],
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(
                            left: ScreenUtil().setWidth(15),
                            top: ScreenUtil().setWidth(20),
                            bottom: ScreenUtil().setWidth(30)),
                        child: Text(
                          '${DateUtils.instance.getDaysformat(DateTime.fromMillisecondsSinceEpoch(detailObj.createTime))} 发布',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(12),
                              color: Color(0xffaaaaaa)),
                        ),
                      ),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: <Widget>[
                          Row(
                            children: [
                              Container(
                                height: 13,
                                width: 2,
                                color: Color(0xff5589FF),
                              ),
                              Container(
                                padding: EdgeInsets.only(
                                    left: ScreenUtil().setWidth(15),
                                    top: ScreenUtil().setWidth(5),
                                    bottom: ScreenUtil().setWidth(5)),
                                child: Text(
                                  "视频简介",
                                  style: TextStyle(
                                    color: Color(0xff222222),
                                    fontSize: ScreenUtil().setSp(16),
                                  ),
                                  textAlign: TextAlign.left,
                                ),
                              ),
                            ],
                          ),
                          Container(
                            padding: EdgeInsets.only(
                                left: ScreenUtil().setWidth(15),
                                right: ScreenUtil().setWidth(15),
                                top: ScreenUtil().setWidth(10),
                                bottom: ScreenUtil().setWidth(90)),
                            child: Text(
                              detailObj.descr,
                              style: TextStyle(
                                color: Color(0xff222222),
                                fontSize: ScreenUtil().setSp(14),
                              ),
                              textAlign: TextAlign.left,
                            ),
                          ),
                        ],
                      ),
                      detailObj.checkFlag == 0
                          ? Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: <Widget>[
                                Container(
                                  padding: EdgeInsets.only(
                                      left: ScreenUtil().setWidth(15),
                                      top: ScreenUtil().setWidth(10),
                                      bottom: ScreenUtil().setWidth(5)),
                                  child: Text(
                                    "驳回原因",
                                    style: TextStyle(
                                      color: Color(0xff222222),
                                      fontSize: ScreenUtil().setSp(16),
                                    ),
                                    textAlign: TextAlign.left,
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.only(
                                      left: ScreenUtil().setWidth(15),
                                      right: ScreenUtil().setWidth(15),
                                      top: ScreenUtil().setWidth(10),
                                      bottom: ScreenUtil().setWidth(90)),
                                  child: Text(
                                    detailObj.checkComment,
                                    style: TextStyle(
                                      color: Color(0xff222222),
                                      fontSize: ScreenUtil().setSp(14),
                                    ),
                                    textAlign: TextAlign.left,
                                  ),
                                ),
                              ],
                            )
                          : Container(
                              child: null,
                            ),
                    ],
                  ),
                ])),
              ],
            )
          : Center(
              child: Text("正在加载..."),
            ),
    );
  }
}
