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

import 'package:twong/router/index.dart';
import 'package:twong/utils/index.dart';
import 'package:twong/config/protocols.dart';
import 'package:provider/provider.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _LoginPageState();
  }
}

class _LoginPageState extends State<LoginPage> {
  bool read = false;
  FocusNode _accountNode = new FocusNode();
  FocusNode _passwordNode = new FocusNode();
  TextEditingController _accountCtl = TextEditingController();
  TextEditingController _passwordCtl = TextEditingController();

  _onLogin(String value) {
    var account = _accountCtl.text;
    var password = _passwordCtl.text;
    if(account.trim() == "" || password.trim() == "") {
      return;
    }

    Network.inst.login(account: account, password: password).then((value) {
      Cache.updateAccount(account, password);
      Navigator.pop(context);
      BotToast.showText(text: "登陆成功!");
    }).catchError((err) {
      Log.e(err);
      BotToast.showText(text: err.toString());
    });
  }

  _onNextInput(String value) {
    FocusScope.of(context).requestFocus(_passwordNode);
  }

  wechatLogin () {
    Utils.notOpen();
  }

  Widget _buildInput(String hit, FocusNode node, TextEditingController controller,
      TextInputAction action, Function(String) onSubmit, IconData icon,
      {bool password = false, TextInputType type}) {
    return Container(
        padding: EdgeInsets.only(left: 30, right: 30, top: 10.px),
        child: TextField(
          focusNode: node,
          obscureText: password,
          onSubmitted: onSubmit,
          controller: controller,
          keyboardType: type,
          style: TextStyle(color: Colors.white),
          textInputAction: action,
          decoration: InputDecoration(
            filled: true,
            hintText: hit,
            border: InputBorder.none,
            hintStyle: TextStyle(color: Colors.white70),
            enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Color(0x00FF00a0)),
              borderRadius: BorderRadius.all(
                Radius.circular(100),
              ),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Color(0x000000a0)),
              borderRadius: BorderRadius.all(
                Radius.circular(100),
              ),
            ),
            contentPadding: EdgeInsets.all(10.px),
            prefixIcon: Icon(icon, color: Colors.white),
          ),
        )
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
            image: DecorationImage(
              fit: BoxFit.cover,
              image: AssetImage('assets/images/launcher.png'),
            )
        ),
        child: ListView(
          physics: ClampingScrollPhysics(),
          children: <Widget>[
            Container(
              alignment: Alignment.topRight,
              child: FlatButton(
                highlightColor: Colors.transparent,
                child: Text('跳过，看好货 >',
                  style: TextStyle(color: Colors.white)
                ),
                onPressed: () { Navigator.pop(context); }
              ),
            ),
            Container(
              height: 100.px,
              margin: EdgeInsets.only(bottom: 50.px, top: 10.px),
              child: Center(
                child: Image.asset("assets/images/logo.png"),
              ),
            ),
            _buildInput("请输入您的账号", _accountNode, _accountCtl,
                TextInputAction.next,_onNextInput, Icons.account_circle,
                type: TextInputType.phone),
            _buildInput("请输入您的密码", _passwordNode, _passwordCtl,
                TextInputAction.done, _onLogin, Icons.lock, password: true,
                type: TextInputType.text),
            Container(
              height: 50.px,
              margin: EdgeInsets.only(left: 56.px, right: 56.px),
              padding: EdgeInsets.only(top: 12.px),
              child: FlatButton(
                color: Colors.white,
                shape: StadiumBorder(),
                onPressed: () { _onLogin(_accountCtl.text); },
                child: Text('登 陆', style: TextStyle(fontSize: 16.px)),
              ),
            ),
            Container(
              padding: EdgeInsets.only(right: 20.px, bottom: 20.px),
              alignment: Alignment.topRight,
              child: FlatButton(
                  highlightColor: Colors.transparent,
                  child: Text('遇到问题?', style: TextStyle(color: Colors.white)),
                  onPressed: () {}),
            ),
            Center(
              child: Text('其他登录方式', style: TextStyle(color: Colors.white)),
            ),
            Container(
              padding: EdgeInsets.only(top: 30),
              child: Flex(
                direction: Axis.horizontal,
                children: <Widget>[
                  Expanded(child: Container(
                      width: 46,
                      height: 46,
                      child: InkWell(
                        onTap: () => wechatLogin(),
                        child: Image.asset("assets/images/wechat.png"),
                      )
                  )),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}