import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/data/WebData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/ProfilePage.dart';
import 'package:chat/utils/IconToggle.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/UserChips.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import '../utils/HttpUtil.dart';
import 'package:dio/dio.dart';
import "../data/UserData.dart";

class UserCard extends StatelessWidget {
  final String userName;
  final bool isReal;
  final String city; //城市
  final int age; //年龄
  final String constellation; //星座
  final String professional; //职业
  final String isOnline;
  final double distance;
  final int userId;
  final bool isLove;
  final bool isBalck;
  final String headUrl;
  final bool payImg;
  final int imgNum;
  final int sex;
  final bool isHidden;
  final int member;
  final bool hiddenDistince;
  final int nameSize;

  UserCard(
      {Key key,
      this.userName,
      this.isReal: false,
      this.city,
      this.age,
      this.constellation,
      this.professional,
      this.isOnline,
      this.distance,
      this.userId,
      this.isLove: false,
      this.sex,
      this.headUrl: '',
      this.payImg: false,
      this.imgNum: 0,
      this.isHidden: false,
      this.member: 0,
      this.hiddenDistince: false,
      this.nameSize = 8,
      this.isBalck: false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isVIP = member > 0;
    bool isSVIP = member == 2;

    //头像
    Widget headImg() {
      double height = 69.4;
      return Stack(
        children: <Widget>[
          Container(
            height: height,
            width: height,
            child: ClipRRect(
                borderRadius: BorderRadius.circular(10),
                child: headUrl == '' || headUrl == null
                    ? Image.asset(
                        Constants.DefaultHeadImgUrl,
                      )
                    : CachedNetworkImage(
                        imageUrl: headUrl,
                        placeholder: (context, url) => Image.asset(
                          Constants.DefaultHeadImgUrl,
                          width: height,
                          height: height,
                        ),
                        fit: BoxFit.cover,
                      )),
          ),
          Positioned(
              left: 0,
              bottom: 0,
              child: Container(
                height: 15,
                width: 40,
                decoration: BoxDecoration(
                    color: Colors.black54,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10),
                        topRight: Radius.circular(10))),
                child: Row(
                  //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Padding(
                        padding: EdgeInsets.only(left: 6),
                        child:
                            Icon(Icons.image, size: 12, color: Colors.white)),
                    Padding(
                      padding: EdgeInsets.only(left: 5, top: 2),
                      child: fixedText(imgNum.toString(),
                          color: Colors.white, fontSize: 12),
                    ),
                  ],
                ),
              ))
        ],
      );
    }

//名字以及认证状态
    Widget nameState() {
      return Row(
        children: <Widget>[
          fixedText(WebData().subUserName(userName, size: nameSize),
              fontSize: 15),
          isReal && sex == 2
              ? Container(
                  margin: EdgeInsets.only(left: 5),
                  padding:
                      EdgeInsets.only(top: 2, bottom: 2, left: 5, right: 5),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10),
                    color: Constants.PurpleBackgroundColor,
                  ),
                  child: fixedText('Real', fontSize: 8, color: Colors.white))
              : Container(),
          isVIP && sex == 1
              ? Container(
                  margin: EdgeInsets.only(left: 5),
                  child: isSVIP ? Constants.svipIcon : Constants.vipIcon)
              : Container(),
        ],
      );
    }

    //距离、在线以及相册
    Widget otherWidget() {
      var greyColor = const Color(0xFFB2B2B2);
      return Row(
        children: <Widget>[
          InfoTile(
              icon: IconData(0xe630, fontFamily: Constants.IconFontFamily),
              title: hiddenDistince
                  ? I18n.of(context).secrecy
                  : (UserData().hasLocationPermission
                      ? WebData().getDistanceString(distance)
                      : I18n.of(context).unknown),
              titleColor: greyColor,
              isDivider: true),
          InfoTile(
              title: isOnline,
              titleColor: greyColor,
              isDivider: payImg || isHidden),
          isHidden
              ? InfoTile(
                  icon: IconData(0xe645, fontFamily: 'iconfont'),
                  iconHeight: 22,
                  title: I18n.of(context).application_view,
                  titleColor: greyColor,
                  isDivider: false,
                  onTap: () {
                    print('click');
                  })
              : Container(),
          payImg
              ? InfoTile(
                  icon: IconData(0xe632, fontFamily: Constants.IconFontFamily),
                  title: I18n.of(context).paid_photo,
                  titleColor: greyColor,
                  isDivider: false,
                  onTap: () {
                    print('click');
                  })
              : Container(),
        ],
      );
    }

    return InkWell(
        onTap: () {
          // if (sex == UserData().basicInfo.sex) {
          //   showToast(UserData().basicInfo.sex == 1
          //       ? I18n.of(context).cant_see
          //       : I18n.of(context).cant_see2);
          //   return;
          // }

          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (context) {
                return ProfilePage(
                  userId: userId,
                );
              },
            ),
          );
        },
        child: Column(
          children: <Widget>[
            Container(
                //背景
                padding: EdgeInsets.only(left: 18, top: 14.5, bottom: 14.5),
                color: Colors.white,
                child: Stack(
                  children: <Widget>[
                    Row(
                      children: <Widget>[
                        //头像图片
                        Padding(
                            padding:
                                EdgeInsets.only(right: 16.5, top: 5, bottom: 5),
                            child: headImg()),
                        //信息
                        Container(
                            width: MediaQuery.of(context).size.width - 104,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: <Widget>[
                                nameState(), //名字以及状态
                                //用户的信息标记
                                Padding(padding: EdgeInsets.only(top: 9)),
                                UserChips(
                                    city: city,
                                    age: age,
                                    constellation: constellation,
                                    professional: professional),
                                Padding(padding: EdgeInsets.only(top: 4)),
                                otherWidget(),
                              ],
                            )),
                      ],
                    ),
                    Positioned(
                      child: //赞
                          sex == UserData().basicInfo.sex
                              ? InkWell(
                                  onTap: () {
                                    showToast(UserData().isMan()
                                        ? I18n.of(context).not_love
                                        : I18n.of(context).not_love2);
                                  },
                                  child: Container(
                                      padding: EdgeInsets.only(
                                          left: 15, right: 15, bottom: 15),
                                      child: Icon(
                                        IconData(
                                          0xe625,
                                          fontFamily: Constants.IconFontFamily,
                                        ),
                                        size: 15,
                                        color: Constants.GreyTextColor,
                                      )))
                              : IconToggle(
                                  key: key,
                                  icon: IconData(
                                    0xe625,
                                    fontFamily: Constants.IconFontFamily,
                                  ),
                                  activeIcon: IconData(
                                    0xe623,
                                    fontFamily: Constants.IconFontFamily,
                                  ),
                                  defaultColor: Constants.GreyTextColor,
                                  activeColor: Constants.RedTextColor,
                                  isActive: isLove,
                                  size: 16,
                                  onTap: (bool isActive) async {
                                    
                                    if (isActive) {
                                       HttpUtil().setLove(userId, () {
                                        MessageMgr().emit('refresh_love_list',
                                            {'UserId': userId, 'flag': 0});
                                      });
                                    } else {
                                     HttpUtil().cancleLove(userId, () {
                                        MessageMgr().emit('refresh_love_list',
                                            {'UserId': userId, 'flag': 2});
                                      });
                                    }
                                  }),
                      right: 0,
                      top: 1,
                    )
                  ],
                )),
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(
                  left: Screen.width * 0.1, right: Screen.width * 0.1),
              child: Divider(
                height: 1,
                color: Constants.DividerLineColor,
              ),
            )
          ],
        ));
  }
}

const double TileHeight = 14;

class InfoTile extends StatelessWidget {
  @required
  final String title;
  final Color titleColor;
  final IconData icon;
  final double iconHeight;
  final VoidCallback onTap;
  final bool isDivider;

  const InfoTile(
      {Key key,
      this.title,
      this.titleColor,
      this.icon,
      this.onTap,
      this.iconHeight: TileHeight,
      this.isDivider = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    List<Widget> elements = [];

    if (icon != null) {
      elements.add(Padding(
          padding: EdgeInsets.only(bottom: 2),
          child: Icon(icon, size: TileHeight, color: titleColor)));
    }

    elements.add(
      Container(
          alignment: Alignment.bottomCenter,
          constraints: BoxConstraints(minHeight: 17),
          padding: EdgeInsets.only(right: 5,bottom: 1.5),
          child: Text(title,
              textScaleFactor: 1.0,
              style: TextStyle(
                  fontSize: 11,
                  color: titleColor,
                  fontWeight: FontWeight.normal))),
    );

    if (isDivider) {
      elements.add(Container(
          height: 16,
          child: VerticalDivider(
            width: 1,
            indent: 0,
            endIndent: 0,
            color: Colors.black26,
          )));
    }

    var tileWidget = Container(
        padding: EdgeInsets.only(right: 5),
        child: Row(
          //crossAxisAlignment: CrossAxisAlignment.start,
          children: elements,
        ));

    if (onTap != null) {
      return InkWell(
        child: tileWidget,
        onTap: onTap,
      );
    } else {
      return tileWidget;
    }
  }
}
