import 'package:chat/data/UserData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/models/group_info_model.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/image_util.dart';
import 'package:chat/utils/msgHandler.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:chat/models/ref_name_provider.dart';
import 'package:provider/provider.dart';

class JoinGroupConfirmPage extends StatefulWidget {
  final GroupInfoModel groupInfoModel;
  JoinGroupConfirmPage({Key key, @required this.groupInfoModel})
      : super(key: key);

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

class _JoinGroupConfirmPageState extends State<JoinGroupConfirmPage> {
  bool isInGroup = false;
  @override
  void initState() {
    super.initState();
    isInGroup =
        widget.groupInfoModel.isGroupMember(UserData().basicInfo.userId);
  }

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

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

  GlobalKey key = GlobalKey();
  Widget _buildBody() {
    Map refMap = Provider.of<RefNameProvider>(context).refMap;
    double width = 52;
    var headImg = Container(
        margin: EdgeInsets.only(top: 9),
        width: width,
        height: width,
        child: WidgetUtil.getAvatarNew(widget.groupInfoModel, key, (path) {
          widget.groupInfoModel.image = path;
        }));
    var name = Container(
      margin: EdgeInsets.only(top: 8.5),
      alignment: Alignment.center,
      width: Screen.width - 40,
      child: Text(
        widget.groupInfoModel.getGroupName(refMap),
        textScaleFactor: 1.0,
        maxLines: 2,
        overflow: TextOverflow.ellipsis,
        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).total_members.replaceFirst(
            '/s1', widget.groupInfoModel.getMembersInGroup().length.toString()),
        textScaleFactor: 1.0,
        style: TextStyle(fontSize: 15.5, color: const Color(0xFF6F6F6F)),
      ),
    );

    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: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            headImg,

            name,
            tips,
            divder,

            ///确认邀请
            Container(
                color: Colors.white,
                padding: EdgeInsets.only(top: 11, bottom: 11),
                child: GestureDetector(
                  onTap: isInGroup
                      ? null
                      : () {
                          MsgHandler.joinGroup(widget.groupInfoModel.sessionId);
                        },
                  child: Container(
                    height: 44.5,
                    margin: EdgeInsets.symmetric(horizontal: 40, vertical: 40),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: isInGroup ? Colors.grey : Color(0xff2685FA),
                        borderRadius: BorderRadius.all(Radius.circular(6))),
                    child: Text(
                      isInGroup
                          ? I18n.of(context).joined
                          : I18n.of(context).join_now,
                      textScaleFactor: 1.0,
                      style: TextStyle(color: Colors.white, fontSize: 16),
                    ),
                  ),
                )),
          ],
        ),
      ),
    );
  }

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