import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/bean/rank_game_info.dart';
import 'package:sport/bean/user.dart';
import 'package:sport/bean/user_info.dart';
import 'package:sport/pages/social/chat_page.dart';
import 'package:sport/pages/social/private_message_page.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/services/api/inject_api.dart';
import 'package:sport/services/userid.dart';
import 'package:sport/utils/toast.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/button_primary.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';
import 'package:sport/widgets/image.dart';
import 'package:sport/widgets/misc.dart';
import 'package:sport/widgets/space.dart';

class RankPeopleDetailsPage extends StatefulWidget {
  final User _data;

  RankPeopleDetailsPage(this._data);

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

class _RankPeopleDetailsPageState extends State<RankPeopleDetailsPage> with InjectApi, UserId {
  ValueNotifier<UserInfo> _valueNotifierUserInfo;

  @override
  void initState() {
    super.initState();
    _valueNotifierUserInfo = ValueNotifier(UserInfo.fromJson(widget._data.toJson()));
    api.getUserInfo("${widget._data.userId}").then((value) {
      _valueNotifierUserInfo.value = value.data;
    });
  }

  @override
  Widget build(BuildContext context) {
    final data = widget._data;
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          elevation: 0,
          leading: buildBackButton(context),
        ),
        body: SingleChildScrollView(
//        physics: ClampingScrollPhysics(),
          child: Container(
            color: Colors.white,
            width: double.infinity,
            child: Column(
              children: <Widget>[
                ValueListenableBuilder(
                  valueListenable: _valueNotifierUserInfo,
                  builder: (BuildContext context, UserInfo user, Widget child) {
                    if (user == null) {
                      return Container();
                    }
                    return Container(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: <Widget>[
                          CircleAvatar(
                            backgroundImage: userAvatarProvider(user.avatar),
                            radius: 45,
                          ),
                          Space(
                            height: 12,
                          ),
                          Text(user.name, style: Theme.of(context).textTheme.headline1),
                          Space(
                            height: 6,
                          ),
                          Row(
                            mainAxisSize: MainAxisSize.min,
                            children: <Widget>[
                              Image.asset("lib/assets/img/mine_icon_${user.gender == 1 ? "man" : "girl"}_gray.png"),
                              Space(
                                width: 4,
                              ),
                              Text(user.gender == 1 ? "男" : "女", style: Theme.of(context).textTheme.bodyText1),
                              Space(
                                width: 10,
                              ),
                              Text(user.age != null ? "${user.age}岁" : "", style: Theme.of(context).textTheme.bodyText1),
                              if ((user.districtId ?? 0) != 0)
                                Padding(
                                  padding: const EdgeInsets.only(left: 10.0),
                                  child: Text("${user.province}${user.province == user.city ? '' : user.city}", style: Theme.of(context).textTheme.bodyText1),
                                )
                            ],
                          ),
                          if (selfId != '${widget._data.userId}')
                            Padding(
                              padding: const EdgeInsets.only(top: 20.0),
                              child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
                                PrimaryButton(
                                  width: 120,
                                  height: 35,
                                  callback: () {
                                    // 先尝试换一下
                                    NavigatorUtil.goPage(context, (context) => ChatPage(user));
                                    // NavigatorUtil.goPage(context, (context) => PrivateMessagePage(user));
                                  },
                                  content: '',
                                  child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
                                    Image.asset("lib/assets/img/mine_icon_message.png"),
                                    Space(
                                      width: 6,
                                    ),
                                    Text(
                                      "私信",
                                      strutStyle: fixedLine,
                                      style: Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.white),
                                    )
                                  ]),
                                ),
                                Space(
                                  width: 12,
                                ),
                                user.followStatus == "friends" || user.followStatus == "followed"
                                    ? GestureDetector(
                                        child: Container(
                                          width: 120,
                                          height: 35,
                                          alignment: Alignment.center,
                                          child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
                                            Image.asset("lib/assets/img/mine_icon_followed.png"),
                                            Space(
                                              width: 6,
                                            ),
                                            Text(
                                              "已关注",
                                              strutStyle: fixedLine,
                                              style: Theme.of(context).textTheme.subtitle1.copyWith(color: Theme.of(context).accentColor),
                                            )
                                          ]),
                                          decoration: BoxDecoration(
                                            borderRadius: BorderRadius.circular(20),
                                            border: Border.all(
                                              color: Theme.of(context).accentColor,
                                              width: .5,
                                            ),
                                          ),
                                        ),
                                        onTap: () async {
                                          await request(context, () async {
                                            var resp = await api.userUnFollow(uid: user.id).catchError((onError) {});
                                            if (resp?.code == 0) {
                                              ToastUtil.show("取关成功");
                                              user.followStatus = "none";
                                              setState(() {});
                                            }
                                          });
                                        },
                                      )
                                    : PrimaryButton(
                                        width: 120,
                                        height: 35,
                                        callback: () async {
                                          await request(context, () async {
                                            var resp = await api.userFollow(uid: user.id).catchError((onError) {});
                                            if (resp?.code == 0) {
                                              ToastUtil.show("关注成功");
                                              user.followStatus = "followed";
                                              setState(() {});
                                            }
                                          });
                                        },
                                        content: '',
                                        child: Row(mainAxisSize: MainAxisSize.min, children: <Widget>[
                                          Image.asset("lib/assets/img/mine_icon_follow.png"),
                                          Space(
                                            width: 6,
                                          ),
                                          Text(
                                            "关注",
                                            strutStyle: fixedLine,
                                            style: Theme.of(context).textTheme.subtitle1.copyWith(color: Colors.white),
                                          )
                                        ]),
                                      ),
                              ]),
                            ),
                          Space(
                            height: 32,
                          ),
                        ],
                      ),
                    );
                  },
                ),
                Divider(
                  endIndent: 12,
                  indent: 12,
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
                  child: Column(
                    children: <Widget>[
                      Padding(
                        padding: EdgeInsets.only(
                          bottom: 12.0,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              "榜单排名",
                              style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                            ),
                            Text(
                              "第 ${data.position} 名",
                              style: TextStyle(color: Color.fromRGBO(255, 196, 0, 1)),
                            )
                          ],
                        ),
                      ),
                      if (data.score != null)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "通关评分",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.score.toStringAsFixed(1)}分",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      if (data.sportDay == 0)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "通关日期",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.finishAt}",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      if (data.sportDay == 0)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "通关时长",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.duration}分钟",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      if (data.sportDay > 0)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "运动时长",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.duration}分钟",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      if (data.sportDay > 0)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "运动天数",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.sportDay}天",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      if (data.sportDay > 0)
                        Padding(
                          padding: EdgeInsets.only(
                            bottom: 12.0,
                          ),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: <Widget>[
                              Text(
                                "每天平均时长",
                                style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                              ),
                              Text(
                                "${data.duration ~/ data.sportDay}分钟/天",
                                style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                              )
                            ],
                          ),
                        ),
                      Padding(
                        padding: EdgeInsets.only(
                          bottom: 12.0,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              "消耗能量",
                              style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                            ),
                            Text(
                              "${data.consume} kal",
                              style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                            )
                          ],
                        ),
                      ),
                      Padding(
                        padding: EdgeInsets.only(
                          bottom: 5.0,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: <Widget>[
                            Text(
                              "运动效率",
                              style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),
                            ),
                            Text(
                              "${(data.consume / data.duration).toStringAsFixed(2)} kal/min",
                              style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1)),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
                Divider(
                  endIndent: 12,
                  indent: 12,
                ),
                Column(
                  children: <Widget>[
                    Space(
                      height: 24,
                    ),
                    // head
                    Stack(
                      alignment: Alignment.center,
                      children: <Widget>[
                        Image.asset(
                          "lib/assets/img/mine_image_achievement.png",
                          fit: BoxFit.fitWidth,
                          width: 240,
                        ),
                        Text(
                          "${(widget?._data?.userId?.toString() == selfId) ? "我" : "他"}的成就",
                          style: Theme.of(context).textTheme.subtitle1,
                        )
                      ],
                    ),
                    // bottom
                    Space(
                      height: 12,
                    ),
                    ValueListenableBuilder(
                      valueListenable: _valueNotifierUserInfo,
                      builder: (BuildContext context, UserInfo user, Widget child) {
                        if (user == null) {
                          return Container();
                        }
                        return Padding(padding: EdgeInsets.all(12.0), child: achievementGroupWidget(user.achievements));
                      },
                    ),
                  ],
                )
              ],
            ),
          ),
        ));
  }
}
