import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/group_member_model.dart';
import 'package:chat/utils/msgHandler.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';

class InviteDetailPage extends StatefulWidget {
  final List<GroupMemberModel> originalList;
  final GroupMemberModel opt;
  final int groupId;
  InviteDetailPage(
      {Key key, @required this.originalList, @required this.opt, this.groupId})
      : super(key: key);

  @override
  _InviteDetailPageState createState() => new _InviteDetailPageState();
}

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

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      title: new Text(
        I18n.of(context).invite_detail,
        textScaleFactor: 1.0,
      ),
      leading: CustomUI.buildCustomLeading(context),
      centerTitle: true,
    );

    return Scaffold(
        appBar: appBar,
        body: SafeArea(
          child: _buildBody(),
        ));
  }

  Widget _buildBody() {
    var headImg = Container(
        margin: EdgeInsets.only(top: 9),
        child: ClipRRect(
            borderRadius: BorderRadius.circular(6),
            child: CachedNetworkImage(
              imageUrl: widget.opt.avtar,
              width: 61,
              height: 61,
            )));
    var name = Container(
      margin: EdgeInsets.only(top: 8.5),
      alignment: Alignment.center,
      width: Screen.width,
      child: Text(
        widget.opt.nickName,
        textScaleFactor: 1.0,
        style: TextStyle(fontSize: 15),
      ),
    );

    var tips = Container(
      margin: EdgeInsets.only(top: 16, bottom: 20.5),
      alignment: Alignment.center,
      width: Screen.width,
      child: Text(
        I18n.of(context)
            .invite_members
            .replaceFirst('/s1', widget.originalList.length.toString()),
        textScaleFactor: 1.0,
        style: TextStyle(fontSize: 15.5),
      ),
    );

    var divder = Container(
      width: Screen.width,
      margin: EdgeInsets.symmetric(horizontal: 16.5),
      decoration: BoxDecoration(border: Border(top: Constants.GreyBorderSide)),
    );

    return Container(
      color: Colors.white,
      width: Screen.width,
      margin: EdgeInsets.only(top: 11),
      child: ListView(
        children: <Widget>[
          headImg,
          name,
          tips,
          divder,
          _buildList(),

          ///确认邀请
          Container(
              color: Colors.white,
              padding: EdgeInsets.only(top: 11, bottom: 11),
              child: GestureDetector(
                onTap: () {
                  List<int> idList = [];
                  for (int k = 0; k < widget.originalList.length; k++) {
                    idList.add(widget.originalList[k].memberId);
                  }

                  MsgHandler.agreeMember(widget.groupId, idList);

                  Navigator.pop(context);
                },
                child: Container(
                  height: 44.5,
                  margin: EdgeInsets.symmetric(horizontal: 40, vertical: 40),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      color: Color(0xff2685FA),
                      borderRadius: BorderRadius.all(Radius.circular(6))),
                  child: Text(
                    I18n.of(context).determine,
                    textScaleFactor: 1.0,
                    style: TextStyle(color: Colors.white, fontSize: 16),
                  ),
                ),
              )),
        ],
      ),
    );
  }

  Widget _buildList() {
    List<Widget> list = [];
//    List userList = [];
//    for (int i = 0; i < 30; i++) {
//      userList.add({
//        'headImg': UserData().basicInfo.headimgurl,
//        'userName': UserData().basicInfo.nickName
//      });
//    }
    widget.originalList
        .forEach((f) => list.add(_buildUserInfo(f.avtar, f.nickName)));
    return Container(
      child: Wrap(crossAxisAlignment: WrapCrossAlignment.start, children: list),
    );
  }

  Widget _buildUserInfo(String headImg, String username) {
    return Container(
      width: Screen.width / 5,
      child: Column(
        children: <Widget>[
          Container(
              margin: EdgeInsets.only(top: 18),
              child: ClipRRect(
                  borderRadius: BorderRadius.circular(6),
                  child: CachedNetworkImage(
                    imageUrl: headImg,
                    width: 42.5,
                    height: 42.5,
                  ))),
          Container(
            margin: EdgeInsets.only(top: 8.5),
            child: Text(
              username,
              textScaleFactor: 1.0,
              style: TextStyle(fontSize: 10.3, color: Constants.GreyTextColor),
            ),
          )
        ],
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
