import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/ProfilePage.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/HttpUtil.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/TokenMgr.dart';
import 'package:chat/utils/friend_list_mgr.dart';
import 'package:chat/utils/screen.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';

class FriendsInfo extends StatelessWidget {
  FriendsInfo({
    @required this.avatar,
    @required this.title,
    @required this.userId,
    this.groupTitle,
    this.onPressed,
    this.gradient,
    this.iconCode,
    this.isShowDivder: true,
  });
  final int userId;
  final int iconCode;
  final String avatar;
  final String title;
  final String groupTitle;
  final VoidCallback onPressed;
  final Gradient gradient;
  final bool isShowDivder;

  static double height(bool hasGroupTitle) {
    final _buttonHeight = MARGIN_VERTICAL * 2 +
        Constants.ContactAvatarSize +
        Constants.DividerWidth;
    if (hasGroupTitle) {
      return _buttonHeight + GROUP_TITLE_HEIGHT;
    } else {
      return _buttonHeight;
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget _avatarIcon;
    if (iconCode == null) {
      _avatarIcon = ClipRRect(
          borderRadius: BorderRadius.circular(6),
          child: CachedNetworkImage(
            imageUrl: this.avatar,
            placeholder: (context, url) => Image.asset(
              Constants.DefaultHeadImgUrl,
              width: Constants.ContactAvatarSize,
              height: Constants.ContactAvatarSize,
            ),
            width: Constants.ContactAvatarSize,
            height: Constants.ContactAvatarSize,
          ));
    } else {
      _avatarIcon = Container(
        width: Constants.ContactAvatarSize,
        height: Constants.ContactAvatarSize,
        decoration: BoxDecoration(
            gradient: gradient, borderRadius: BorderRadius.circular(6)),
        child: Icon(
          IconData(this.iconCode, fontFamily: Constants.IconFontFamily),
          color: Colors.white,
        ),
      );
    }

    Widget _button = Container(
      padding: const EdgeInsets.symmetric(
          vertical: MARGIN_VERTICAL, horizontal: 16.0),
      decoration: BoxDecoration(color: Colors.white),
      child: Row(
        children: <Widget>[
          _avatarIcon,
          SizedBox(width: 10.0),
          Expanded(
              child: Container(
                  child: Text(
            title,
            textScaleFactor: 1.0,
            overflow: TextOverflow.ellipsis,
          )))
        ],
      ),
    );
    if (userId != 0) {
      _button = Dismissible(
          key: Key(userId.toString()),
          child: _button,
          direction: DismissDirection.endToStart,
          background: Container(
            color: Colors.red,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.end,
              children: <Widget>[
                fixedText(I18n.of(context).delete, color: Colors.white),
                Icon(
                  Icons.delete,
                  color: Colors.white,
                ),
                SizedBox(width: 20),
              ],
            ),
          ),
          confirmDismiss: (direction) async {
            var _confirmContent = I18n.of(context).confirm_delete;
            // CustomUI.buildOneConfirm(
            //     context, _confirmContent, I18n.of(context).determine, () async {
            //   Map data = {
            //     "userId": UserData().basicInfo.userId,
            //     "friendsUserId": userId,
            //   };
            //   data['sign'] = TokenMgr().getSign(data);

            //   Response res = await HttpUtil().post('friendship/delete/friends',
            //       data: data, isShowLoading: true);

            //   Map resData = res.data;
            //   showToast(resData['msg']);
            //   if (resData['code'] == 0) {
            //     FriendListMgr().delteFriend(userId);
            //     MessageMgr().emit('Delete friend');
            //   }
            //   Navigator.of(context).pop(true);
            // });
            //return false;
            bool isDismiss = await CustomUI.showIosDialog(
                context, _confirmContent, () async {
              Map data = {
                "userId": UserData().basicInfo.userId,
                "friendsUserId": userId,
              };
              data['sign'] = TokenMgr().getSign(data);

              Response res = await HttpUtil().post('friendship/delete/friends',
                  data: data, isShowLoading: true);

              Map resData = res.data;
              showToast(resData['msg']);
              if (resData['code'] == 0) {
                FriendListMgr().delteFriend(userId);
                MessageMgr().emit('Delete friend');
              }
              Navigator.of(context).pop(true);
            }, () {
              Navigator.of(context).pop(false);
            });

            return isDismiss;
          });
    }
    //分组标签
    Widget _itemBody;
    if (this.groupTitle != null) {
      _itemBody = Column(
        children: <Widget>[
          Container(
            height: GROUP_TITLE_HEIGHT,
            padding: EdgeInsets.only(left: 16.0, right: 16.0),
            color: const Color(AppColors.ContactGroupTitleBgColor),
            alignment: Alignment.centerLeft,
            child: Text(this.groupTitle,
                textScaleFactor: 1.0, style: AppStyles.GroupTitleItemTextStyle),
          ),
          _button,
        ],
      );
    } else {
      _itemBody = _button;
    }

    return InkWell(
        onTap: onPressed == null
            ? () {
                Navigator.of(context).push(
                  new MaterialPageRoute(
                    builder: (context) {
                      return ProfilePage(
                        userId: userId,
                      );
                    },
                  ),
                );
              }
            : onPressed,
        child: Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                _itemBody,
                isShowDivder
                    ? Container(
                        height: 1,
                        color: const Color(0xFFF3F3F3),
                        margin: EdgeInsets.only(
                            left: 26 + Constants.ContactAvatarSize),
                      )
                    : Container()
              ],
            )));
  }
}
