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/FullWithButton.dart';
import 'package:chat/utils/msgHandler.dart';
import 'package:flutter/material.dart';
import 'create_group_view.dart';

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

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

class _GroupManagePageState extends State<GroupManagePage> {
  bool invite = false; //消息提示

  @override
  void initState() {
    super.initState();
    invite = widget.groupInfoModel.askSwitch == 1;
  }

  Widget _buildSettingWithSwitch(
      bool switchValue, String title, Function onchang) {
    Widget left = new Text(
      title,
      textScaleFactor: 1.0,
      style: TextStyle(fontSize: 14, fontWeight: FontWeight.normal),
    );
    Widget right = new Expanded(
        child: new Align(
            alignment: Alignment.centerRight,
            child: new Switch(
              value: switchValue,
              activeTrackColor: Constants.ConfrimButtonColor.withOpacity(0.3),
              onChanged: onchang,
            )));
    return new Container(
      decoration: BoxDecoration(
        color: Colors.white,
      ),
      padding: EdgeInsets.only(left: 20),
      height: 53,
      child: new Row(
        children: <Widget>[left, right],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    Widget appBar = new AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: new Text(
        I18n.of(context).group_setting,
        style: TextStyle(color: AppColors.NewAppbarTextColor),
        textScaleFactor: 1.0,
      ),
      leading: CustomUI.buildCustomLeading(context),
      centerTitle: true,
    );

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

  Widget _buildBody() {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 7,
          ),

          ///群聊邀请确认
          _buildSettingWithSwitch(invite, I18n.of(context).group_invite,
              (bool val) {
            setState(() {
              invite = val;
              MsgHandler.setGroupSwitch(
                  widget.groupInfoModel.sessionId, invite);
            });
          }),
          _buildTips(),
          Container(
              color: Colors.white,
              padding: EdgeInsets.only(left: 10),
              height: 53,
              child: FullWidthButton(
                title: I18n.of(context).change_group_owner,
                showDivider: false,
                showRightIcon: true,
                onPressed: () async {
                  String str = await Navigator.of(context).push(
                    new MaterialPageRoute(
                      builder: (context) {
                        return CreateGroupPage(
                            GroupOperatingPageType.SelectGroupOwner,
                            widget.groupInfoModel.members,
                            widget.groupInfoModel.sessionId);
                      },
                    ),
                  );
                  if (str == 'close') {
                    Navigator.pop(context);
                  }
                },
              ))
        ],
      ),
    );
  }

  Widget _buildTips() {
    return Container(
      margin: EdgeInsets.only(top: 10, bottom: 10, left: 15, right: 15),
      child: Text(
        I18n.of(context).group_setting_tips,
         textScaleFactor: 1.0,
        style: TextStyle(fontSize: 10.5, color: Constants.GreyTextColor),
      ),
    );
  }

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