import 'package:chat/data/group_data_mgr.dart';
import 'package:chat/home/global_search.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/utils/MessageMgr.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/CustomUI.dart';
import 'create_group_view.dart';
import 'group_item_widget.dart';

class GroupChatListPage extends StatefulWidget {
  @override
  _GroupChatListPageState createState() => _GroupChatListPageState();
}

class _GroupChatListPageState extends State<GroupChatListPage> {
  ScrollController _scrollController = ScrollController();
  List<GroupInfoModel> searchList = [];

  @override
  void initState() {
    super.initState();

    print('GroupChatListPage initState');
    MessageMgr().on('Quit Group', quitGroup);

    MessageMgr().on('Update Group List', updateGroupLastMsg);
  }

  updateGroupLastMsg(args) async {
    await GroupInfoMgr().sortGroupList();
    if (mounted) {
      setState(() {});
    }
  }

  @override
  void dispose() {
    _scrollController.dispose();

    MessageMgr().off('Update Group List', updateGroupLastMsg);

    MessageMgr().off('Quit Group', quitGroup);
    super.dispose();
  }

  quitGroup(args) {
    if (mounted) {
      setState(() {
        print('更新群列表');
      });
    }
  }

  Widget _buildCreateButton() {
    List<GroupInfoModel> groupList = GroupInfoMgr().groupInfoList;
    Widget _avatarIcon = ClipRRect(
        borderRadius: BorderRadius.circular(GroupRadius),
        child: Container(
          color: const Color(0xFFF2F2F2),
          height: AvatarSize,
          width: AvatarSize,
          child: Icon(
            IconData(0xe66f, fontFamily: Constants.IconFontFamily),
            color: Constants.BlueTextColor,
            size: 35,
          ),
        ));

    Widget _button = InkWell(
        onTap: () {
          Navigator.of(context).push(
            new MaterialPageRoute(
              builder: (context) {
                return CreateGroupPage(
                    GroupOperatingPageType.CreateGroup, [], null);
              },
            ),
          );
        },
        child: Container(
          height: ItemHeight,
          margin: EdgeInsets.only(top: 6.5, bottom: 16),
          decoration: BoxDecoration(color: Colors.white),
          child: Row(
            children: <Widget>[
              SizedBox(width: LeftPadding),
              _avatarIcon,
              SizedBox(width: 14.0),
              Text(
                I18n.of(context).create_group_chat,
                textScaleFactor: 1.0,
                style: TextStyle(fontSize: 15.5),
              ),
            ],
          ),
        ));

    Widget tips = Container(
      alignment: Alignment.centerLeft,
      width: Screen.width,
      height: 33.5,
      padding: EdgeInsets.only(left: LeftPadding),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: Constants.GreyBorderSide)),
      child: Text(I18n.of(context).group_chat + '(${groupList.length})', textScaleFactor: 1.0,),
    );

    return Column(
      children: <Widget>[
        _button,
        tips,
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    List<GroupInfoModel> groupList = GroupInfoMgr().groupInfoList;
    print('build group chat Page 群数量${groupList.length}');
    final List<Widget> _body = [];

    _body.add(
      ListView.builder(
        controller: _scrollController,
        itemBuilder: (BuildContext context, int index) {
          if (index == 0) {
            return _buildCreateButton();
          } else {
            var info = groupList[index - 1];
            return GroupItem(ValueKey(info), groupInfoModel: info);
          }
        },
        itemCount: groupList.length + 1,
      ),
    );

    return Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: AppBar(
            backgroundColor: AppColors.NewAppbarBgColor,
            title: Text(I18n.of(context).group_chat,
                textScaleFactor: 1.0, style: Constants.MainTitleStyle),
            centerTitle: false,
            elevation: 1,
            bottom: CustomUI.buildSearchButton(context, () {
              Navigator.of(context).push(
                new MaterialPageRoute(
                  builder: (context) {
                    return GlobalSearchPage(
                      type: GlobalSearchPageType.SearchGroup,
                    );
                  },
                ),
              );
            })),
        body: Stack(
          children: _body,
        ));
  }
}
