import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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 '../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 {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _sharePopupState();
  }
}

class _sharePopupState extends State<sharePopup> {
  List<int> data = [0, 1, 2, 3];
  bool showCloseButton = true;
  Widget content(int e) {
    return Container(
//      width: MediaQuery.of(context).size.width * 1,
      child: 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(20.0)),
                color: Colors.white,
                border: Border.all(color: Color(0xf2f2f2f2))),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Space(
                  height: 36.0,
                ),
                Text(
                  "恭喜您达到新成就",
                  style: Theme.of(context).textTheme.headline3,
                ),
                Space(
                  height: 5.0,
                ),
                Text(
                  "您是第$e位获得者",
                  style: Theme.of(context).textTheme.bodyText1,
                ),
                Space(
                  height: 16.0,
                ),
                CachedNetworkImage(
                  imageUrl: avatarList[Random().nextInt(10) % 11],
                  width: 120.0,
                  height: 120.0,
                ),
                Space(
                  height: 8.0,
                ),
                Text(
                  "燃脂达人Ⅱ",
                  style: Theme.of(context).textTheme.headline1,
                ),
                Space(
                  height: 16.0,
                ),
                Text(
                  "使用智能鞋运动消耗1000kal",
                  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,
                  child: Text(
                    "经验值+5  积分+1",
                    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.topCenter,
                        end: Alignment.bottomCenter,
                        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 (data.length != 1) {
                        setState(() {
                          data = data.sublist(0, data.length - 1);
                        });
                      } else {
                        Navigator.pop(context);
                      }
                      NavigatorUtil.goPage(
                          context, (context) => ShareAchievementPage());
                    },
                  ),
                ),
                Space(
                  height: 8.0,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Expanded(
                      child: Divider(
                        indent: 50.0,
                        endIndent: 10.0,
                      ),
                    ),
                    Text(
                      "2020.07.14",
                      style: Theme.of(context).textTheme.bodyText1,
                    ),
                    Expanded(
                      child: Divider(
                        indent: 10.0,
                        endIndent: 50.0,
                      ),
                    ),
                  ],
                ),
              ],
            ),
//            padding: EdgeInsets.only(bottom: 20.0),
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.0),
            child: data.length > 0 && 0 == data.indexOf(e)
                ? InkWell(
                    child: Image.asset("lib/assets/img/pop_share_chose.png"),
                    onTap: () {
                      if (data.length != 1) {
                        data.removeAt(0);
                        setState(() {
                          data = data;
                        });
                      } else {
                        Navigator.of(context).pop();
                      }
                    },
                  )
                : Container(
                    width: 35.0,
                    height: 35.0,
                  ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    print(data);
    // TODO: implement build
    return Material(
      color: Colors.black.withOpacity(0.5),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          Expanded(
            child: Center(
              // stack 需要一个高度进行搞一搞
              child: Stack(
                children: data.reversed.map((e) {
                  return Transform.translate(
                      offset: Offset((data.indexOf(e) + 1) * 5.0,
                          -(data.indexOf(e)) * 5.0),
//                      padding: EdgeInsets.only(
//                          right: (data.indexOf(e)) * 10.0,
//                          top: (data.indexOf(e)) * 10.0),
                      child: content(e));
                }).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();
//                }
//              },
//            )
        ],
      ),
    );
  }
}
