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/LoveToggle.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart' as prefix0;
import 'package:oktoast/oktoast.dart';
import '../utils/HttpUtil.dart';
import 'package:dio/dio.dart';
import "../data/UserData.dart";
import 'TempUserChips.dart';
import 'TokenMgr.dart';

class TempUserCard 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;

  TempUserCard(
      {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 = Screen.width;
      return Stack(
        children: <Widget>[
          Container(
            height: height-40,
            width: height-40,
            child: ClipRRect(
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(15),
                    topRight: Radius.circular(15)),
                child: headUrl == '' || headUrl == null
                    ? Image.asset(
                  Constants.DefaultHeadImgUrl,
                  width: height-40,
                  height: height-40,
                )
                    : CachedNetworkImage(
                  imageUrl: headUrl,
                  placeholder: (context, url) => Image.asset(
                    Constants.DefaultHeadImgUrl,
                    width: height-40,
                    height: height-40,
                  ),
                  fit: BoxFit.cover,
                )),
          ),
          Positioned(
              left: 0,
              bottom: 0,
              child: Container(
                height: 27,
                width: 54,
                decoration: BoxDecoration(
                    color: Colors.black54,
                    borderRadius: BorderRadius.only(

                        topRight: Radius.circular(10))),
                child: Row(
                  //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Padding(
                        padding: EdgeInsets.only(left: 6),
                        child:
                        Icon(Icons.image, size: 15, color: Colors.white)),
                    Padding(
                      padding: EdgeInsets.only(left: 5, top: 2),
                      child: fixedText(imgNum.toString(),
                          color: Colors.white, fontSize: 14),
                    ),
                  ],
                ),
              ))
        ],
      );
    }

//名字以及认证状态
    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(),
        ],
      );
    }

    // void _selectValue(str) async {
    //   if (str == 'collection') {
    //     Map data = {
    //       "userid": UserData().basicInfo.userId,
    //       "type": 0,
    //       "followUserId": this.userId,
    //     };
    //     data['sign'] = TokenMgr().getSign(data);
    //     Response res = await HttpUtil()
    //         .post('userfollow/follow/insert', data: data, isShowLoading: true);
    //     Map resData = res.data;
    //     if (resData['code'] == 0) {
    //       MessageMgr().emit('refresh_love_list', {'UserId': userId, 'flag': 0});
    //       showToast(resData['msg']);
    //     }
    //   } else if (str == 'cancleCollection') {
    //     Map data = {
    //       "userid": UserData().basicInfo.userId,
    //       "type": 0,
    //       "followUserId": this.userId,
    //     };
    //     data['sign'] = TokenMgr().getSign(data);
    //     Response res = await HttpUtil()
    //         .post('userfollow/follow/cancel', data: data, isShowLoading: true);
    //     Map resData = res.data;
    //     if (resData['code'] == 0) {
    //       MessageMgr().emit('refresh_love_list', {'UserId': userId, 'flag': 2});
    //       showToast(resData['msg']);
    //     }
    //   } else if (str == 'black') {
    //     Map data = {
    //       "userid": UserData().basicInfo.userId,
    //       "type": 1,
    //       "followUserId": this.userId,
    //     };
    //     data['sign'] = TokenMgr().getSign(data);

    //     Response res =
    //         await HttpUtil().post('userfollow/follow/insert', data: data);
    //     Map resData = res.data;
    //     showToast(resData['msg']);
    //     if (resData['code'] == 0) {
    //       MessageMgr().emit('refresh_love_list', {'UserId': userId, 'flag': 1});
    //     }
    //   } else if (str == 'cancleBlack') {
    //     var data = {
    //       "userid": UserData().basicInfo.userId,
    //       "type": 1,
    //       "followUserId": this.userId,
    //     };
    //     data['sign'] = TokenMgr().getSign(data);

    //     Response res =
    //         await HttpUtil().post('userfollow/follow/cancel', data: data);
    //     Map resData = res.data;
    //     showToast(resData['msg']);
    //     if (resData['code'] == 0) {
    //       MessageMgr().emit('refresh_love_list', {'UserId': userId, 'flag': 2});
    //     }
    //   }
    // }

    //距离、在线以及相册
    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: Container(alignment: Alignment.center,color: AppColors.NewAppbarBgColor,width: Screen.width,child: Container(width: Screen.width-40,child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            SizedBox(height: 23,),
            headImg(),


            Container(decoration: BoxDecoration(color: Colors.white,borderRadius: BorderRadius.only(
                bottomLeft: Radius.circular(15),
                bottomRight: Radius.circular(15))) ,child: Column(children: <Widget>[
              Container(
                //背景
                  padding: EdgeInsets.only(left: 15,right: 10, bottom: 14.5,top: 5),
                  child: Column(children: <Widget>[
                    Stack(
                      children: <Widget>[
                        Row(
                          children: <Widget>[
                            //头像图片

                            //信息
                            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)),
                                    TempUserChips(
                                        city: city,
                                        age: age,
                                        constellation: constellation,
                                        professional: professional),
                                    Padding(padding: EdgeInsets.only(top: 4)),
                                    otherWidget(),
                                  ],
                                )),
                          ],
                        ),

                      ],
                    )
                  ],)),
              SizedBox(height: 5,),
              Container(

                child: //赞
                sex == UserData().basicInfo.sex
                    ? InkWell(
                    onTap: () {
                      showToast(UserData().isMan()
                          ? I18n.of(context).not_love
                          : I18n.of(context).not_love2);
                    },
                    child:  Container(
                        width: 100,
                        height: 30,
                        decoration: BoxDecoration(border:  Border.all(color: Color(0xFF878787), width: 1),borderRadius: BorderRadius.circular(16)) ,
                        padding: EdgeInsets.only(
                            left: 15, right: 15, bottom: 5,top: 5 ),
                        child:  Row( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
                          Icon(
                            IconData(
                              0xe625,
                              fontFamily: Constants.IconFontFamily,
                            ),
                            size: 15,
                            color: Color(0xffB2B2B2),
                          ),
                          SizedBox(width: 5,),
                          Text('LOVE',style: prefix0.TextStyle(color: Color(0xffB2B2B2)),)
                        ],)))
                    :   LoveToggle(isActive: isLove,onTap: (bool isActive) async {
                  Map data = {
                    "userid": UserData().basicInfo.userId,
                    "type": 0,
                    "followUserId": this.userId,
                  };
                  data['sign'] = TokenMgr().getSign(data);
                  if (isActive) {
                    Response res = await HttpUtil().post(
                        'userfollow/follow/insert',
                        data: data,
                        isShowLoading: true);
                    Map resData = res.data;
                    showToast(resData['msg']);
                    if (resData['code'] == 0) {
                      MessageMgr().emit('refresh_love_list',
                          {'UserId': userId, 'flag': 0});
                      showToast(resData['msg']);
                    }
                  } else {
                    Response res = await HttpUtil().post(
                        'userfollow/follow/cancel',
                        data: data,
                        isShowLoading: true);
                    Map resData = res.data;
                    if (resData['code'] == 0) {
                      MessageMgr().emit('refresh_love_list',
                          {'UserId': userId, 'flag': 2});
                      showToast(resData['msg']);
                    }
                  }
                }),
              ),
              SizedBox(height: 15,),



            ],),),





          ],
        ),),));
  }
}

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(
          constraints: BoxConstraints(maxWidth: 140),
          padding: EdgeInsets.only(right: 5),
          child: Text(title,
              textScaleFactor: 1.0,
              style: TextStyle(
                  fontSize: 11,
                  color: titleColor,
                  fontWeight: FontWeight.normal),
              textAlign: TextAlign.center)),
    );

    if (isDivider) {
      elements.add(Container(
          height: 12,
          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;
    }
  }
}
