import 'package:cached_network_image/cached_network_image.dart';
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/models/ref_name_provider.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';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

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

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

class _GroupAnnouncementPageState extends State<GroupAnnouncementPage> {
  TextEditingController _txtCtrl = new TextEditingController();
  bool isGroupOwner = false;

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

    print('GroupAnnouncementPage initState');
    _txtCtrl.text = widget.groupInfoModel.describe;
    isGroupOwner =
        widget.groupInfoModel.hosterId == UserData().basicInfo.userId;
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: new Text(
        I18n.of(context).group_announcement,
        style: TextStyle(color: AppColors.NewAppbarTextColor),
        textScaleFactor: 1.0,
      ),
      centerTitle: true,
      leading: CustomUI.buildCustomLeading(context),
      actions: <Widget>[
        isGroupOwner
            ? InkWell(
                onTap: () {
                  var content = _txtCtrl.text;

                  MsgHandler.updateGroupNotice(
                      widget.groupInfoModel.sessionId, content);
                  Navigator.of(context).pop();
                },
                child: Container(
                  alignment: Alignment.center,
                  margin: EdgeInsets.only(top: 15, bottom: 15, right: 20),
                  height: 20,
                  width: 60,
                  padding: EdgeInsets.symmetric(horizontal: 5),
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(4.5)),
                  child: Text(
                    I18n.of(context).determine,
                    textScaleFactor: 1.0,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        color: Constants.ConfrimButtonColor, fontSize: 12.6),
                  ),
                ),
              )
            : Container()
      ],
    );

    return Scaffold(
        appBar: appBar,
        backgroundColor: Colors.white,
        body: SafeArea(
          child: _buildBody(),
        ));
  }

  Widget _buildBody() {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          _buildGroupOwner(),
          _buildLine(),
          _buildInput(),
          Expanded(
            child: SizedBox(),
          ),
          _buildTips(),
        ],
      ),
    );
  }

  Widget _buildLine() {
    return Container(
      width: Screen.width,
      decoration:
          BoxDecoration(border: Border(bottom: Constants.GreyBorderSide)),
      margin: EdgeInsets.only(left: 20, right: 20),
    );
  }

  Widget _buildTips() {
    return isGroupOwner
        ? Container()
        : Container(
            width: Screen.width,
            margin: EdgeInsets.only(bottom: 60),
            child: Row(
              children: <Widget>[
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 30, right: 10),
                    decoration: BoxDecoration(
                        border: Border(top: Constants.GreyBorderSide)),
                  ),
                ),
                Text(
                  I18n.of(context).only_host,
                  textScaleFactor: 1.0,
                  style: TextStyle(color: Colors.grey[350]),
                ),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(right: 30, left: 10),
                    decoration: BoxDecoration(
                        border: Border(top: Constants.GreyBorderSide)),
                  ),
                ),
              ],
            ),
          );
  }

  Widget _buildGroupOwner() {
    GroupMemberModel memberModel = widget.groupInfoModel.getHoster();

    var refName = Provider.of<RefNameProvider>(context)
        .getGroupRefName(widget.groupInfoModel.sessionId, memberModel.memberId);
    return Container(
      height: 62.5,
      child: Row(
        children: <Widget>[
          SizedBox(width: 20),
          ClipRRect(
              borderRadius: BorderRadius.circular(6),
              child: CachedNetworkImage(
                imageUrl: memberModel.avtar,
                width: 45,
                height: 45,
              )),
          SizedBox(width: 8),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                refName,
                textScaleFactor: 1.0,
                style: TextStyle(fontSize: 16),
              ),
              SizedBox(height: 6.5),
            ],
          )
        ],
      ),
    );
  }

  Widget _buildInput() {
    return isGroupOwner
        ? Container(
            child: TextField(
              keyboardAppearance: Brightness.light,
              controller: _txtCtrl,
              style: TextStyle(
                  fontSize: 17,
                  color: Constants.BlackTextColor,
                  textBaseline: TextBaseline.alphabetic),
              decoration: InputDecoration(
                contentPadding:
                    EdgeInsets.only(right: 20, left: 20, top: 10, bottom: 10),
                hintText: I18n.of(context).fill_out,
                hintStyle: TextStyle(fontSize: 16, color: Colors.grey),
                border: InputBorder.none,
              ),
              autofocus: true,
              maxLines: 5,
              inputFormatters: [LengthLimitingTextInputFormatter(100)],
              onChanged: (str) async {},
            ),
          )
        : Container(
            //color: Colors.white,
            padding: EdgeInsets.only(right: 20, left: 20, top: 10, bottom: 10),
            child: Text(
              widget.groupInfoModel.describe,
              textScaleFactor: 1.0,
            ),
          );
  }

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