/*
 * @author lsy
 * @date   2019-09-04
 **/

import 'package:flutter/material.dart';
import 'package:gmalpha_flutter/commonModel/net/Responce/SimpleResponce.dart';
import 'package:gmalpha_flutter/userModel/page/user/UserPageModel.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/TestUserEntity.dart';
import 'package:gmalpha_flutter/userModel/service/remote/entity/UserEntity.dart';

class UserWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => UserState();
}

class UserState extends State<UserWidget> {
  UserPageModel _model;

  TextEditingController _nameController = new TextEditingController();
  TextEditingController _wordController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    _model = new UserPageModel();
  }

  @override
  void dispose() {
    super.dispose();
    _model.dispose();
    _wordController.dispose();
    _nameController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "User",
          style: TextStyle(fontSize: 16),
        ),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          Expanded(
            child: StreamBuilder<SimpleResponce>(
              stream: _model.logoutLive.stream,
              initialData: _model.logoutLive.data,
              builder: (BuildContext context,
                  AsyncSnapshot<SimpleResponce> snapshot) {
                if (snapshot.data == null) {
                  return Center(child: Text("没有退出"));
                } else {
                  return Center(child: Text("退出接口回掉成功！！"));
                }
              },
            ),
          ),
          Container(
            margin: EdgeInsets.fromLTRB(16, 10, 16, 20),
            child: TextField(
              cursorColor: Colors.black,
              controller: _nameController,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10.0),
                  icon: Icon(Icons.text_fields, color: Colors.black),
                  labelText: 'name',
                  labelStyle: TextStyle(color: Colors.black)),
              autofocus: false,
            ),
          ),
          Container(
            margin: EdgeInsets.fromLTRB(16, 0, 16, 20),
            child: TextField(
              cursorColor: Colors.black,
              controller: _wordController,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10.0),
                  icon: Icon(
                    Icons.text_fields,
                    color: Colors.black,
                  ),
                  labelText: 'word',
                  labelStyle: TextStyle(color: Colors.black)),
              autofocus: false,
            ),
          ),
          Container(
              width: double.maxFinite,
              margin: EdgeInsets.fromLTRB(16, 0, 16, 0),
              child: OutlineButton(
                onPressed: () => _model.resignUser(
                    context, _nameController.text, _wordController.text),
                child: Text("注册"),
                shape: BeveledRectangleBorder(
                  borderRadius: BorderRadius.circular(3),
                ),
              )),
          Container(
              width: double.maxFinite,
              margin: EdgeInsets.fromLTRB(16, 0, 16, 0),
              child: OutlineButton(
                onPressed: () => _model.logout(context),
                child: Text("退出"),
                shape: BeveledRectangleBorder(
                  borderRadius: BorderRadius.circular(3),
                ),
              )),
          Expanded(
            child: StreamBuilder<TestUserEntity>(
              stream: _model.resignLive.stream,
              initialData: _model.resignLive.data,
              builder: (BuildContext context,
                  AsyncSnapshot<TestUserEntity> snapshot) {
                if (snapshot.data != null) {
                  return Center(
                    child: snapshot.data.data == null
                        ? Text("错误  ${snapshot.data.errorMsg}")
                        : Text("登入回掉 用户id: ${snapshot.data.data.id}"),
                  );
                } else {
                  return Center(
                    child: Text("初始化～"),
                  );
                }
              },
            ),
          )
        ],
      ),
    );
  }
}
