import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:zhiying_base_widget/widgets/team/fans_list/model/team_fans_list_model.dart';
import 'package:zhiying_comm/zhiying_comm.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'dart:ui' as ui show PlaceholderAlignment;

///
/// 我的团队 - 粉丝信息
///
class TeamFansItem extends StatefulWidget {
  TeamViewItemStyle styleModel;
  TeamFansListItemModel dataModel;

  TeamFansItem(this.styleModel, this.dataModel);

  @override
  _TeamFansItemState createState() => _TeamFansItemState();
}

class _TeamFansItemState extends State<TeamFansItem> {
  /// 跳去粉丝详情
  void _openFansItemDetailsPage() {
    // Navigator.push(context, CupertinoPageRoute(builder: (_) => TeamDetailsPage(null)));
    RouterUtil.route(widget?.styleModel, widget?.dataModel?.toJson(), context);
  }

  /// 复制文字
  void _copyText(text) {
    if(!EmptyUtil.isEmpty(text)) {
      Clipboard.setData(ClipboardData(text: text));
      Fluttertoast.showToast(msg: '复制成功~');
    }else{
      Fluttertoast.showToast(msg: '内容不能为空');
    }
  }

  @override
  Widget build(BuildContext context) {
    return _getMainWidget();
  }

  /// 主体视图
  Widget _getMainWidget() {
    return Container(
      decoration: BoxDecoration(color: HexColor.fromHex('#FFFFFF'), borderRadius: BorderRadius.circular(10)),
      margin: const EdgeInsets.only(left: 12.5, right: 12.5, top: 8),
      padding: const EdgeInsets.only(left: 20, right: 20, top: 17.5, bottom: 15),
      child: Column(
        children: <Widget>[
          // 粉丝头像信息等
          _getFansInfoWidget(),
          const SizedBox(height: 10),
          // 微信号码
          _getWXNumberInfoWidget(),
          const SizedBox(height: 10),
          // 数据信息
          _getDataWidget(),
        ],
      ),
    );
  }

  /// 粉丝头像信息等
  Widget _getFansInfoWidget() {
    return Row(
      children: <Widget>[
        /// 头像
        GestureDetector(
            onTap: () => _openFansItemDetailsPage(),
            child: Container(
              width: 50,
              child: CachedNetworkImage(
                imageUrl: widget?.dataModel?.avatar ?? '',
              ),
            )),
        const SizedBox(width: 10),

        /// 信息
        Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            /// 会员等级 关系 昵称
            RichText(
              textAlign: TextAlign.center,
              text: TextSpan(text: '', children: [
                /// 等级
                WidgetSpan(
                  alignment: ui.PlaceholderAlignment.middle,
                  child: Container(
                    padding: const EdgeInsets.only(left: 3, right: 2, top: 2, bottom: 2),
                    decoration: BoxDecoration(
                      color: HexColor.fromHex(widget?.dataModel?.levelBgColor),
                      borderRadius: BorderRadius.circular(2.5),
                    ),
                    alignment: Alignment.center,
                    child: Row(
                      children: <Widget>[
                        CachedNetworkImage(imageUrl: widget?.dataModel?.levelIcon ?? '', width: 11,),
                        const SizedBox(width: 2.5),
                        Text(
                          widget?.dataModel?.levelName ?? '黑钻会员',
                          style: TextStyle(color: HexColor.fromHex(widget?.styleModel?.lvTextColor ?? 'FFFFFF'), fontSize: 8),
                        ),
                      ],
                    ),
                  ),
                ),

                /// 会员关系
                WidgetSpan(
                  alignment: ui.PlaceholderAlignment.middle,
                  child: Container(
                    alignment: Alignment.center,
                    padding: const EdgeInsets.only(left: 2.5, right: 2.5, top: 2, bottom: 2),
                    margin: const EdgeInsets.only(left: 3, right: 3),
                    decoration: BoxDecoration(borderRadius: BorderRadius.circular(2.5), color: HexColor.fromHex(widget?.styleModel?.directTextBgColor ?? '#FF4242')),
                    child: Text(
                      widget?.dataModel?.levelType ?? '直',
                      style: TextStyle(color: HexColor.fromHex(widget?.styleModel?.directTextColor ?? '#FFFFFF'), fontSize: 8),
                    ),
                  ),
                ),

                /// 会员名称
                TextSpan(
                    text: widget?.dataModel?.blurUserName ?? '***',
                    style: TextStyle(color: HexColor.fromHex(widget?.styleModel?.usernameColor ?? '#333333'), fontSize: 12, fontWeight: FontWeight.bold))
              ]),
            ),
            const SizedBox(height: 2.5),

            /// 手机号码
            RichText(
              textAlign: TextAlign.center,
              text: TextSpan(text: '', children: [
                /// 手机号码
                TextSpan(
                    text: widget?.styleModel?.phooneText ?? '手机号：',
                    style: TextStyle(color: HexColor.fromHex(widget?.styleModel?.phoneColor ?? '#333333'), fontSize: 11)),
                TextSpan(
                    text: widget?.dataModel?.blurMobile ?? '',
                    style: TextStyle(
                        color: HexColor.fromHex(widget?.styleModel?.phoneColor ?? '#333333'), fontSize: 11, fontFamily: 'Din', package: 'zhiying_base_widget')),

                /// 复制按钮
                WidgetSpan(
                    alignment: ui.PlaceholderAlignment.middle,
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () => _copyText(widget?.dataModel?.phone),
                      child: Container(
                        width: 11,
                        margin: const EdgeInsets.only(left: 3),
                        child: CachedNetworkImage(
                          imageUrl: widget?.styleModel?.phoneCopyIcon ?? '',
                        ),
                      ),
                    ))
              ]),
            ),
          ],
        )
      ],
    );
  }

  /// 微信号码信息
  Widget _getWXNumberInfoWidget() {
    return Container(
      padding: const EdgeInsets.only(left: 9, right: 10, bottom: 6.5, top: 6.5),
      decoration: BoxDecoration(
        color: HexColor.fromHex(widget?.styleModel?.infoBarColor ?? '#F7F7F7'),
        borderRadius: BorderRadius.circular(5),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          /// 微信号码
          RichText(
            textAlign: TextAlign.center,
            text: TextSpan(text: '', children: [
              TextSpan(
                  text: widget?.styleModel?.infoBarWxText ?? '微信号:',
                  style: TextStyle(
                    color: HexColor.fromHex(widget?.styleModel?.infoBarWxTextColor ?? '#999999'),
                    fontWeight: FontWeight.bold,
                    fontSize: 11,
                  )),
              TextSpan(
                  text: widget?.dataModel?.blurWeChat ?? '',
                  style: TextStyle(
                      color: HexColor.fromHex(widget?.styleModel?.infoBarWxValueColor ?? '#333333'),
                      fontWeight: FontWeight.bold,
                      fontSize: 11,
                      fontFamily: 'Din',
                      package: 'zhiying_base_widget')),

              /// 复制安妮
              WidgetSpan(
                  alignment: ui.PlaceholderAlignment.middle,
                  child: GestureDetector(
                    onTap: () => _copyText(widget?.dataModel?.wechat),
                    behavior: HitTestBehavior.opaque,
                    child: Container(
                        padding: const EdgeInsets.only(left: 5.5),
                        width: 11 + 5.5,
                        child: CachedNetworkImage(
                          imageUrl: widget?.styleModel?.phoneCopyIcon ?? '',
                        )),
                  ))
            ]),
          ),

          /// 最近登陆时间
          RichText(
            textAlign: TextAlign.center,
            text: TextSpan(children: [
              TextSpan(
                  text: widget?.styleModel?.infoBarLastLoginText ?? '最近登陆 ',
                  style: TextStyle(
                    color: HexColor.fromHex(widget?.styleModel?.infoBarLastLoginTextColor ?? '#909090'),
                    fontSize: 11,
                  )),
              TextSpan(
                  text: widget?.dataModel?.lastLogin ?? '',
                  style: TextStyle(
                      color: HexColor.fromHex(widget?.styleModel?.infoBarLastLoginValueColor ?? '#909090'),
                      fontSize: 11,
                      fontFamily: 'Din',
                      package: 'zhiying_base_widget')),
            ]),
          )
        ],
      ),
    );
  }

  /// 数据信息
  Widget _getDataWidget() {
    return GestureDetector(
      onTap: () => _openFansItemDetailsPage(),
      behavior: HitTestBehavior.opaque,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: <Widget>[
          /// 左边数据
          Flexible(
            flex: 1,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                /// 邀请人数(人)
                _getCustomWidget(
                  text: widget?.styleModel?.totalInviteText ?? '邀请人数(人)',
                  textColor: widget?.styleModel?.totalInviteTextColor ?? '#333333',
                  textSize: 10,
                  number: widget?.dataModel?.inviteCount ?? '0',
                  numberColor: widget?.styleModel?.totalInviteValueColor ?? '#FF4242',
                  numberSize: 20,
                ),
                const SizedBox(height: 15),

                /// 今日邀请 & 本月邀请
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    /// 今日邀请
                    _getCustomWidget(
                      text: widget?.styleModel?.todayInviteText ?? '今日邀请',
                      textColor: widget?.styleModel?.todayInviteTextColor ?? '#909090',
                      textSize: 10,
                      number: widget?.dataModel?.todayInviteCount ?? '0',
                      numberColor: widget?.styleModel?.todayInviteValueColor ?? '#333333',
                      numberSize: 15,
                    ),

                    /// 本月邀请
                    _getCustomWidget(
                      text: widget?.styleModel?.monthInviteText ?? '本月邀请',
                      textColor: widget?.styleModel?.monthInviteTextColor ?? '#909090',
                      textSize: 10,
                      number: widget?.dataModel?.monthInviteCount ?? '0',
                      numberColor: widget?.styleModel?.monthInviteValueColor ?? '#333333',
                      numberSize: 15,
                    ),
                  ],
                )
              ],
            ),
          ),

          /// 分割线
          SizedBox(
              height: 65.5,
              width: 0.5,
              child: VerticalDivider(
                width: 0.5,
                thickness: 0.5,
                color: HexColor.fromHex(/*widget?.styleModel?.dashbordLineColor ??*/ '#F7F7F7'),
              )),

          /// 右边数据
          Flexible(
            flex: 1,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                /// 累计收益(¥)
                _getCustomWidget(
                  text: widget?.styleModel?.totalEarningText ?? '累计收益(¥)',
                  textColor: widget?.styleModel?.totalEarningTextColor ?? '#333333',
                  textSize: 10,
                  number: widget?.dataModel?.totalFin ?? '0.00',
                  numberColor: widget?.styleModel?.totalEarningValueColor ?? '#FF4242',
                  numberSize: 20,
                ),

                const SizedBox(height: 15),

                /// 本周收益 & 本月收益
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    /// 本周收益
                    _getCustomWidget(
                      text: widget?.styleModel?.weekEarningText ?? '本周收益',
                      textColor: widget?.styleModel?.weekEarningTextColor ?? '#909090',
                      textSize: 10,
                      number: widget?.dataModel?.weekFin ?? '0.00',
                      numberColor: widget?.styleModel?.weekEarningValueColor ?? '#333333',
                      numberSize: 15,
                    ),

                    /// 本月邀请
                    _getCustomWidget(
                      text: widget?.styleModel?.monthEarningText ?? '本月收益',
                      textColor: widget?.styleModel?.monthEarningTextColor ?? '#909090',
                      textSize: 10,
                      number: widget?.dataModel?.monthFin ?? '0.00',
                      numberColor: widget?.styleModel?.monthEarningValueColor ?? '#333333',
                      numberSize: 15,
                    ),
                  ],
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  /// 自定义Widget(数字加粗)
  Widget _getCustomWidget({String text, String textColor, double textSize, String number, String numberColor, double numberSize}) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        /// Number
        Text(number, style: TextStyle(color: HexColor.fromHex(numberColor), fontSize: numberSize, fontWeight: FontWeight.bold, fontFamily: 'Din', package: 'zhiying_base_widget')),

        /// Text
        Text(text, style: TextStyle(color: HexColor.fromHex(textColor), fontSize: textSize)),
      ],
    );
  }
}
