import 'dart:ui';

import 'package:chat/data/chat_data_mgr.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/send_msg_state.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/models/last_msg_description.dart';
import 'package:chat/utils/app_navigator.dart';
import 'package:chat/utils/image_util.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:chat/models/ref_name_provider.dart';
import 'package:provider/provider.dart';
import 'last_unread_dot.dart';

const double AvatarSize = 60;
const double GroupRadius = 6.5;
const double LeftPadding = 15;
const double ItemHeight = 83.5;

class GroupItem extends StatefulWidget {
  GroupItem(Key key,
      {@required this.groupInfoModel,
      this.onPressed,
      this.isShowDivder: true,
      this.enterType = 0,
      this.enterContent})
      : super(key: key);

  final GroupInfoModel groupInfoModel;
  final VoidCallback onPressed;
  final bool isShowDivder;
  final int enterType; // 0默认  1图片
  final dynamic enterContent;
  @override
  _GroupItemState createState() => _GroupItemState();
}

class _GroupItemState extends State<GroupItem> {
  @override
  void initState() {
    super.initState();
  }

  GlobalKey key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    Widget _avatarIcon =
        WidgetUtil.getAvatarNew(widget.groupInfoModel, key, (String path) {
      widget.groupInfoModel.image = path;
    });

    List<Widget> _rightArea = [
      SizedBox(height: 4),
      Align(
        alignment: Alignment.topCenter,
        child: widget.groupInfoModel.lastMsg == null
            ? Container()
            : Text(
                Constants.getShowTime(
                    widget.groupInfoModel.lastMsg.updateAt, context),
                textScaleFactor: 1.0,
                style: AppStyles.DescStyle),
      ),
      Expanded(
          child: Align(
        alignment: Alignment.bottomCenter,
        child: widget.groupInfoModel.lastMsg == null
            ? Container()
            : (widget.groupInfoModel.messageFree == 1
                ? Icon(
                    IconData(0xe673, fontFamily: Constants.IconFontFamily),
                    color: Colors.grey[400],
                    size: 20,
                  )
                : UnreadCountDot(
                    sessionId: widget.groupInfoModel.lastMsg.sessionId,
                    unreadCount: ChatDataMgr()
                        .groupUnreadProvider
                        .getUnreadCount(
                            widget.groupInfoModel.lastMsg.sessionId))),
      ))
    ];

    Map refMap = Provider.of<RefNameProvider>(context).refMap;
    return InkWell(
      child: Container(
        padding: const EdgeInsets.only(
            left: LeftPadding, right: LeftPadding, top: 13.5, bottom: 13.5),
        height: ItemHeight,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Color(widget.groupInfoModel.topTag == 1
                ? 0xfff5f6f9
                : AppColors.ConversationItemBgColor),
            border: Border(bottom: Constants.GreyBorderSide)),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Stack(
              children: <Widget>[
                _avatarIcon,
                Positioned(
                    right: 0,
                    top: 0,
                    child: Offstage(
                        offstage: !(widget.groupInfoModel.lastMsg != null &&
                            widget.groupInfoModel.messageFree == 1 &&
                            ChatDataMgr().groupUnreadProvider.getUnreadCount(
                                    widget.groupInfoModel.lastMsg.sessionId) >
                                0),
                        child: CircleAvatar(
                          radius: 4,
                          backgroundColor: Colors.red,
                        )))
              ],
            ),
            Container(width: 10.0),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  SizedBox(height: 3),
                  Text(widget.groupInfoModel.getGroupName(refMap),
                      textScaleFactor: 1.0,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 15,
                        color: Colors.black,
                      )),
                  Expanded(child: SizedBox()),
                  Row(
                    children: <Widget>[
                      widget.groupInfoModel.lastMsg == null
                          ? Container()
                          : SendMsgState(
                              sessionId:
                                  widget.groupInfoModel.lastMsg.sessionId,
                              time: widget.groupInfoModel.lastMsg.updateAt),
                      widget.groupInfoModel.lastMsg == null
                          ? Container()
                          : Row(
                              children: <Widget>[
                                //有人@你就优先展示
                                ChatDataMgr()
                                        .groupUnreadProvider
                                        .getHavaAltertime(widget
                                            .groupInfoModel.lastMsg.sessionId) != null
                                    ? Text(
                                        '[有人@我]',
                                        textScaleFactor: 1.0,
                                        style: TextStyle(
                                            color: Colors.red, fontSize: 12),
                                      )
                                    : Text(
                                        widget.groupInfoModel.lastMsg != null &&
                                                widget.groupInfoModel
                                                        .messageFree ==
                                                    1 &&
                                                ChatDataMgr()
                                                        .groupUnreadProvider
                                                        .getUnreadCount(widget
                                                            .groupInfoModel
                                                            .lastMsg
                                                            .sessionId) >
                                                    0
                                            ? '[${ChatDataMgr().groupUnreadProvider.getUnreadCount(widget.groupInfoModel.lastMsg.sessionId)}${I18n.of(context).article}] '
                                            : "",
                                        textScaleFactor: 1.0,
                                        style: AppStyles.DescStyle),
                                Container(
                                    constraints: BoxConstraints(
                                        maxWidth: Screen.width - 160),
                                    child: LastMsgDescription(
                                        Key(widget.groupInfoModel.sessionId
                                            .toString()),
                                        widget.groupInfoModel.lastMsg))
                              ],
                            )
                    ],
                  ),
                  SizedBox(height: 7)
                ],
              ),
            ),
            SizedBox(width: 10),
            SizedBox(
              height: Constants.ConversationAvatarSize,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: _rightArea,
              ),
            )
          ],
        ),
      ),
      onTap: () {
        AppNavigator.pushGroupChatPage(context, widget.groupInfoModel,
            enterType: widget.enterType, enterContent: widget.enterContent);
      },
    );
  }
}
