
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:twong/api/index.dart';
import 'package:twong/config/style.dart';
import 'package:twong/providers/user.dart';

import 'package:twong/utils/index.dart';
import 'package:twong/widgets/app_bar.dart';
import 'package:twong/widgets/counter_botton.dart';

class AccountEditPage extends StatefulWidget {
  final int type;
  AccountEditPage(this.type);

  @override
  State<StatefulWidget> createState() {
    return _AccountEditState();
  }
}

class _AccountEditState extends State<AccountEditPage> {
  List _widgets;
  FocusNode _firstNode = FocusNode();
  List _titles = ["修改昵称", "修改密码"];
  TextEditingController _firstCtrl = TextEditingController();

  @override
  initState() {
    super.initState();
    _widgets = [_nickEdit, _passEdit];
  }

  Widget get _nickEdit {
    _firstCtrl.text = Cache.user.nickname;
    // FocusScope.of(context).requestFocus(_firstNode);
    return Column(
      children: [
        Container(
          padding: EdgeInsets.all(6.px),
          child: Row(
            children: [
              Text("昵称"),
              Expanded(child: Container(
                margin: EdgeInsets.only(left: 24.px),
                child: TextField(
                  focusNode: _firstNode,
                  controller: _firstCtrl,
                  cursorColor: DColors.Main,
                  decoration: InputDecoration(
                    border: InputBorder.none
                  ),
                ),
              ))
            ],
          ),
        ),
        Divider()
      ],
    );
  }

  Widget get _passEdit {
    return Column(
      children: [
        _buildItem("手机号", value: Consumer<UserModel>(
          builder: (context, model, child) =>
              Text("${Utils.encodeString(model.info.phone)}"),
        )),
        _buildItem("验证码", hit: "请输入手机验证码", child: CounterButton(
          onClick: _onGetCode,
        color: DColors.Main, disableColor: Colors.grey)),
        _buildItem("新密码", password: true, hit: "请输入新的密码"),
        _buildItem("密码确认", password: true, hit: "请再次输入新的密码"),
      ],
    );
  }

  bool _onGetCode() {
    return true;
  }

  Widget _buildItem(String key, { Widget child, bool password = false,
    String hit, Widget value }) {
    return Column(
      children: [
        Container(
          padding: EdgeInsets.all(6.px),
          child: Row(
            children: [
              Container(
                width: 68.px,
                child: Text(key),
              ),
              value == null ? Expanded(child: Container(
                child: TextField(
                  obscureText: password,
                  // focusNode: _firstNode,
                  // controller: _firstCtrl,
                  cursorColor: DColors.Main,
                  decoration: InputDecoration(
                      hintText: hit,
                      border: InputBorder.none
                  ),
                ),
              )) : value,
              child == null ? Container() : child
            ],
          ),
        ),
        Divider()
      ],
    );
  }

  _saveClick() {
    if(widget.type == 0) {
      Network.inst.editUser(nickname: _firstCtrl.text).then((res) {
        Provider.of<UserModel>(context, listen: false)
            .updateNickName(_firstCtrl.text);
        Navigator.pop(context);
        BotToast.showText(text: "保存成功");
      }).catchError((err) {
        Log.d(err);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: DAppBar(_titles[widget.type]),
      body: Container(
        margin: EdgeInsets.only(left: 12.px, right: 12.px, top: 6.px),
        child: Column(
          children: [
            _widgets[widget.type],
            Container(
              margin: EdgeInsets.only(left: 32.px, right: 32.px),
              width: double.infinity,
              child: FlatButton(
                color: DColors.Main,
                shape: StadiumBorder(),
                onPressed: _saveClick,
                child: Text("保 存", style: TextStyle(color: Colors.white,
                  fontSize: 14.px)),
              ),
            )
          ],
        ),
      ),
    );
  }
}