import 'package:chat/data/WebData.dart';
import 'package:chat/data/constants.dart';
import 'package:chat/generated/i18n.dart';
import 'package:chat/home/Registerpage.dart';
import 'package:chat/utils/CustomUI.dart';
import 'package:chat/utils/screen.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:oktoast/oktoast.dart';
import '../utils/OtherLogin.dart';
import '../utils/HttpUtil.dart';
import "../data/UserData.dart";

const RATE_NUM = 0.82;

//const BlueColor = const Color(0xFF93C3FF);
const BlueColor = Constants.BlueTextColor;

class LoginPage extends StatefulWidget {
  LoginPage({Key key, bool flag}) : super(key: key);

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String _phoneNumber = '';
  String _passWorld = '';

  String _selectType = UserData().language == LanguageType.Vietnamese
      ? phone.keys.toList()[1]
      : phone.keys.toList()[0];

  @override
  Widget build(BuildContext context) {
    var keyHeight = MediaQuery.of(context).viewInsets.bottom;
    if (keyHeight > 0) {
      UserData().setKeyboardHeight(keyHeight);
    }

    Widget appBar = AppBar(
      backgroundColor: AppColors.NewAppbarBgColor,
      title: Text(
        I18n.of(context).login,
        textScaleFactor: 1.0,
        style: TextStyle(color: AppColors.NewAppbarTextColor),
      ),
      leading: CustomUI.buildCustomLeading(context),
      centerTitle: true,
    );
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: Container(
          height: MediaQuery.of(context).size.height,
          width: MediaQuery.of(context).size.width,
          child: _buildBody(),
        ),
      )),
      appBar: appBar,
      resizeToAvoidBottomPadding: false,
    );
  }

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

    print('LoginPage initState');
  }

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

  Widget _buildBody() {
    return new Column(
      children: <Widget>[
        _buildAcountInput(),
        _buildPasswordInput(),
        _buildForgetPassWord(),
        SizedBox(
          height: 40,
        ),
        _buildLoginButton(),
        _buildOtherLogin(),
      ],
    );
  }

  //构建底部第三方登陆
  Widget _buildOtherLogin() {
    return new Expanded(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: <Widget>[
          new Container(
            alignment: Alignment.bottomCenter,
            child: OtherLogin().builder(context),
          )
        ],
      ),
    );
  }

  //账号输入框
  Widget _buildAcountInput() {
    double height = 54.5;
    return new Container(
        alignment: Alignment.center,
        margin: EdgeInsets.only(top: 8.5),
        height: height,
        padding: EdgeInsets.only(left: 17),
        decoration: BoxDecoration(
            color: Colors.white,
            border: Border(
                top: Constants.GreyBorderSide,
                bottom: Constants.GreyBorderSide)),
        child: Stack(
          children: <Widget>[
            Container(
              width: Screen.width,
              height: height,
              alignment: Alignment.centerLeft,
              child: Icon(
                Icons.phone_android,
                color: BlueColor,
                size: Constants.TextFieldIconSize,
              ),
            ),
            Positioned(
              left: 46,
              child: Container(
                padding: EdgeInsets.only(top: 5),
                margin: EdgeInsets.all(0),
                alignment: Alignment.centerLeft,
                child: new DropdownButtonHideUnderline(
                    child: new DropdownButton(
                  items: phone.keys.map((key) {
                    return DropdownMenuItem(
                      child: new Text(key, textScaleFactor: 1.0),
                      value: key,
                    );
                  }).toList(),
                  onChanged: (value) {
                    setState(() {
                      _selectType = value;
                    });
                  },
                  value: _selectType,
                  elevation: 24, //设置阴影的高度
                  style: new TextStyle(
                    //设置文本框里面文字的样式
                    color: Constants.BlackTextColor,
                    fontSize: 12,
                  ),
                  iconSize: 25.0,
                )),
              ),
            ),
            Positioned(
              left: 90,
              child: Container(
                alignment: Alignment.center,
                width: Screen.width - 90,
                height: height,
                child: TextField(
                  keyboardAppearance: Brightness.light,
                  decoration: new InputDecoration(
                    hintText: I18n.of(context).enter_number,
                    hintStyle: TextStyle(fontSize: 14),
                    border: InputBorder.none,
                  ),
                  maxLines: 1,
                  style: TextStyle(textBaseline: TextBaseline.alphabetic),
                  inputFormatters: [
                    WhitelistingTextInputFormatter.digitsOnly,
                  ],
                  onChanged: (str) {
                    _phoneNumber = str;
                    //_phoneNumber = str;
                    setState(() {});
                  },
                  keyboardType: TextInputType.phone,
                ),
              ),
            )
          ],
        ));
  }

  //构建密码按钮
  Widget _buildPasswordInput() {
    return new Container(
      alignment: Alignment.center,
      child: new TextField(
        keyboardAppearance: Brightness.light,
        decoration: new InputDecoration(
          hintText: I18n.of(context).enter_password,
          hintStyle: TextStyle(fontSize: 14),
          icon: new Icon(
            Icons.lock,
            color: BlueColor,
            size: Constants.TextFieldIconSize,
          ),
          border: InputBorder.none,
        ),
        style: TextStyle(textBaseline: TextBaseline.alphabetic),
        maxLines: 1,
        obscureText: true,
        onChanged: (str) {
          _passWorld = str;
          setState(() {});
        },
      ),
      height: 54.5,
      padding: EdgeInsets.only(left: 17),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(bottom: Constants.GreyBorderSide)),
    );
  }

  //构建忘记密码
  Widget _buildForgetPassWord() {
    return new Container(
      child: new Align(
        alignment: FractionalOffset.centerRight,
        child: new FlatButton(
          child: new Text(
            I18n.of(context).forget_password,
            textScaleFactor: 1.0,
            style: TextStyle(color: BlueColor),
          ),
          onPressed: () {
            Navigator.of(context).push(
              new MaterialPageRoute(
                builder: (context) {
                  return RegisterPage(
                    type: PageType.changePWD.index,
                  );
                },
              ),
            );
          },
        ),
      ),
    );
  }

  //构建登陆按钮
  Widget _buildLoginButton() {
    Text text = new Text(I18n.of(context).login,
        textScaleFactor: 1.0,
        style: TextStyle(
            fontSize: Constants.ShaderButtonFontSize, color: Colors.white));
    callback() async {
      if (_phoneNumber == null || _phoneNumber == "") {
        showToast(I18n.of(context).enter_number);
        return;
      }
      if (_passWorld == null || _passWorld == "") {
        showToast(I18n.of(context).enter_password);
        return;
      }
      HttpUtil().login(phone[_selectType] + _phoneNumber, _passWorld, context);
    }

    return InkWell(
      onTap: callback,
      child: Container(
//        margin: EdgeInsets.only(top: 40),
        alignment: Alignment.center,
        height: Constants.ShaderButtonHeight,
        width: Screen.width * RATE_NUM,
        decoration: BoxDecoration(
            color: Constants.ConfrimButtonColor,
            border: Border.all(color: const Color(0x803875E9)),
            borderRadius:
                BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
        child: text,
      ),
    );
  }
}
