import 'package:chat/data/chat_data_mgr.dart';
import 'package:chat/home/ConversActionPage.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:flutter/material.dart';

import 'friend_page.dart';

class UnreadDot extends StatefulWidget {
  final Widget child;
  final int type;
  UnreadDot({this.child, this.type = 1});
  @override
  _UnreadDotState createState() => _UnreadDotState();
}

class _UnreadDotState extends State<UnreadDot> {
  @override
  void initState() {
    super.initState();

    MessageMgr().on('Update UnreadCount', updateDotState);
  }

  @override
  void dispose() {
    MessageMgr().off('Update UnreadCount', updateDotState);
    super.dispose();
  }

  updateDotState(args) {
//    print('更新未读状态');
    Future.delayed(Duration(milliseconds: 100), () {
      if (mounted) {
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    bool isHasUnread = false;
    switch (widget.type) {
      case 1:
        isHasUnread = ChatDataMgr().unreadCountProvider.checkUnreadMsg();
        break;
      case 2:
        isHasUnread = isShowMsg();
        break;
      case 3:
        isHasUnread =
            ChatDataMgr().unreadCountProvider.checkUnreadMsg() || isShowMsg();
        break;
      case 4:
        isHasUnread = ChatDataMgr().groupUnreadProvider.checkUnreadMsg();
        break;
      case 5:
        isHasUnread = isHaveNewFriends();
        break;
      case 6:
        isHasUnread = ChatDataMgr().unreadCountProvider.checkUnreadMsg() ||
            isShowMsg() ||
            ChatDataMgr().groupUnreadProvider.checkUnreadMsg();
        break;
    }

    return Stack(
      children: <Widget>[
        widget.child,
        Positioned(
            right: 0,
            top: 0,
            child: Offstage(
                offstage: !isHasUnread,
                child: CircleAvatar(
                  radius: 3.5,
                  backgroundColor: Colors.red,
                )))
      ],
    );
  }
}
