import 'dart:math';
import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/user.dart';
import 'package:sport/pages/social/chat_page.dart';
import 'package:sport/pages/social/share_achievement.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/widgets/button_primary.dart';

import '../space.dart';

//Future<bool> showSharePopup(BuildContext context) {
//
//  return showGeneralDialog<bool>(
//      context: context,
//      barrierDismissible: true,
//      barrierLabel: '',
//      barrierColor: Colors.black.withOpacity(0.5),
//      transitionDuration: Duration(milliseconds: 200),
////                              useRootNavigator: false,
//      pageBuilder: (BuildContext context, Animation<double> animation,
//          Animation<double> secondaryAnimation) {
//        return Center(
//          child: Material(
//            color: Colors.transparent,
//            child: Center(
//              child: Stack(
//                children: data.map((e) {
//                  if (e != 0) {
//                    print(
//                        "--------------------------------------------------------------");
//                    print(e);
//                    print(e * 10.0);
//                    return Positioned.fill(
//                        left: e * 10.0, top: e * 10.0, child: content(e));
//                  } else {
//                    return Positioned.fill(child: content(e));
//                  }
//                }).toList(),
//              ),
//            ),
//          ),
//        );
//      });
//}

// 换成这种 自定义PopRoute
class sharePopup extends StatefulWidget {
  List<Achievement> list;
  sharePopup(this.list);
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _sharePopupState();
  }
}

class _sharePopupState extends State<sharePopup> {
  bool showCloseButton = true;
  Widget content(Achievement e, int index, int length) {
    print("${index}-------------------------");
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(bottom: 20.0),
//            height: MediaQuery.of(context).size.height * 0.7,
          width: MediaQuery.of(context).size.width * 0.7,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(10.0)),
            boxShadow: [
              BoxShadow(
                  offset: Offset(0.0, 0),
                  blurRadius: 5,
                  spreadRadius: 0,
                  color: Color.fromRGBO(0, 0, 0, 0.1))
            ],
            color: Colors.white.withOpacity(1 - index * 0.12),
//                border: Border.all(color: Color(0xf2f2f2f2))
//              image: DecorationImage(
//                  image:
//                      AssetImage("lib/assets/img/bg_pop_achievement.png"))
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Space(
                height: 30.0,
              ),
              Text(
                "恭喜您达到新成就",
                style: Theme.of(context).textTheme.headline3,
              ),
              Space(
                height: 5.0,
              ),
              Text(
                "您是第${e.userCount}位获得者",
                style: Theme.of(context).textTheme.bodyText1,
              ),
              Space(
                height: 16.0,
              ),
              Container(
                width: double.infinity,
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(
                            "lib/assets/img/bg_pop_achievement.png"))),
                child: CachedNetworkImage(
                  imageUrl: e.logo,
                  width: 120.0,
                  height: 120.0,
                ),
              ),
              Space(
                height: 8.0,
              ),
              Text(
                "${e.name}",
                style: Theme.of(context).textTheme.headline1,
              ),
              Space(
                height: 16.0,
              ),
              Text(
                "${e.conditionDetail}",
                style: Theme.of(context).textTheme.bodyText1,
              ),
              Space(
                height: 5.0,
              ),
              Container(
                height: 25.0,
                width: MediaQuery.of(context).size.width * 0.6,
//                  color: Color(0xffFFC400).withOpacity(0.3),
                alignment: Alignment.center,
                decoration: BoxDecoration(
//                  gradient: new RadialGradient(radius: 4, colors: [
//                    Color.fromRGBO(255, 196, 0, 1).withAlpha(80),
//                    Color(0x00ffffff),
//                  ]),
                    image: DecorationImage(
                        image: AssetImage(
                            "lib/assets/img/bg1_pop_achievement.png"))),
                child: Text(
                  "经验值+${e.rewardExp}  积分+${e.rewardScore}",
                  style: Theme.of(context)
                      .textTheme
                      .subtitle2
                      .copyWith(color: Theme.of(context).accentColor),
                ),
              ),
              Space(
                height: 16.0,
              ),
              Container(
                width: 132.0,
                height: 35.0,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20.0)),
                  border: new Border.all(
                      width: 1, color: Theme.of(context).accentColor),
                  gradient: new LinearGradient(
                      begin: Alignment.centerLeft,
                      end: Alignment.centerRight,
                      colors: [
                        Color.fromRGBO(255, 196, 0, 1),
                        Color.fromRGBO(255, 170, 0, 1),
                      ]),
                ),
//                  child: InkWell(
//                    child: Text(
//                      "成就分享",
//                      style: Theme.of(context).textTheme.headline6,
//                    ),
//                    onTap: () {
//                      if (widget.list.length != 1) {
//                        setState(() {
//                          widget.list =
//                              widget.list.sublist(0, widget.list.length - 1);
//                        });
//                      } else {
//                        Navigator.pop(context);
//                      }
//                      NavigatorUtil.goPage(
//                          context, (context) => ShareAchievementPage(e));
//                    },
//                  ),
                child: PrimaryButton(
                  content: "成就分享",
                  callback: () {
                    if (widget.list.length != 1) {
                      setState(() {
                        widget.list =
                            widget.list.sublist(0, widget.list.length - 1);
                      });
                    } else {
                      Navigator.pop(context);
                    }
                    NavigatorUtil.goPage(
                        context, (context) => ShareAchievementPage(e));
                  },
                  height: 35,
//                    fontSize: 12.0,
                ),
              ),
              Space(
                height: 8.0,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    child: Divider(
                      indent: 50.0,
                      endIndent: 10.0,
                    ),
                  ),
                  Text(
                    "${e.createdAt.split(" ")[0]}",
                    style: Theme.of(context)
                        .textTheme
                        .bodyText1
                        .copyWith(fontSize: 11.0),
                  ),
                  Expanded(
                    child: Divider(
                      indent: 10.0,
                      endIndent: 50.0,
                    ),
                  ),
                ],
              ),
            ],
          ),
//            padding: EdgeInsets.only(bottom: 20.0),
        ),
        Padding(
          padding: EdgeInsets.only(top: 20.0),
          child: widget.list.length > 0 && 0 == widget.list.indexOf(e)
              ? InkWell(
                  child: Image.asset("lib/assets/img/pop_share_chose.png"),
                  onTap: () {
                    if (widget.list.length != 1) {
                      widget.list.removeAt(0);
                      setState(() {
                        widget.list = widget.list;
                      });
                    } else {
                      Navigator.of(context).pop();
                    }
                  },
                )
              : Container(
                  width: 35.0,
                  height: 35.0,
                ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    print(widget.list);
    // TODO: implement build
    return BackdropFilter(
      //背景滤镜
      filter: ImageFilter.blur(sigmaX: 5, sigmaY: 5),
      child: Scaffold(
          backgroundColor: Colors.black.withOpacity(0.8),
          body: GestureDetector(
              onTap: () {
                Navigator.maybePop(context);
              },
              behavior: HitTestBehavior.opaque,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Expanded(
                    child: Center(
                      // stack 需要一个高度进行搞一搞
                      child: Stack(
                        children: widget.list.reversed.map((e) {
                          return Transform.translate(
                              offset: Offset((widget.list.indexOf(e) + 1) * 5.0,
                                  -(widget.list.indexOf(e)) * 5.0),
//                      padding: EdgeInsets.only(
//                          right: (data.indexOf(e)) * 10.0,
//                          top: (data.indexOf(e)) * 10.0),
                              child: content(e, widget.list.indexOf(e),
                                  widget.list.length));
                        }).toList(),
                      ),
                    ),
                  ),
//          if (data.length > 0)
//            InkWell(
//              child: Image.asset("lib/assets/img/pop_share_chose.png"),
//              onTap: () {
//                if (data.length != 1) {
//                  setState(() {
//                    data = data.sublist(0, data.length - 1);
//                  });
//                } else {
//                  Navigator.of(context).pop();
//                }
//              },
//            )
                ],
              ))),
    );
  }
}
