import 'dart:math';
import 'dart:ui';

import 'package:akuCommunity/pages/setting_page/agreement_page/agreement_page.dart';
import 'package:akuCommunity/pages/setting_page/agreement_page/privacy_page.dart';
import 'package:akuCommunity/pages/sign/user_authentication_page.dart';
import 'package:ani_route/ani_route.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flustars/flustars.dart' show TextUtil;
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:akuCommunity/utils/screenutil.dart';
import 'package:akuCommunity/base/base_style.dart';
import 'package:akuCommunity/base/assets_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class SignInPage extends StatefulWidget {
  SignInPage({Key key}) : super(key: key);

  @override
  _SignInPageState createState() => _SignInPageState();
}

class _SignInPageState extends State<SignInPage> {
  TextEditingController _phone = new TextEditingController();
  TextEditingController _code = new TextEditingController();
  String _verifyStr = '获取验证码';
  AppBar _appBar() {
    return AppBar(
      elevation: 0,
      backgroundColor: Colors.white,
      // leading: IconButton(
      //   icon: Icon(AntDesign.left, size: 40.sp),
      //   onPressed: () {
      //     Navigator.pop(context);
      //   },
      // ),
    );
  }

  Container _containerTextField(String imagePath,
      TextEditingController controller, String hintText, bool isCode) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 29.w),
      margin: EdgeInsets.symmetric(horizontal: 82.w),
      decoration: BoxDecoration(
        color: Color(0xfffff4d7),
        borderRadius: BorderRadius.all(Radius.circular(36)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Image.asset(
            imagePath,
            height: 50.w,
            width: 50.w,
          ),
          SizedBox(width: 24.w),
          Expanded(
            child: TextFormField(
              obscureText: false,
              obscuringCharacter: '*',
              cursorColor: Color(0xffffc40c),
              style: TextStyle(
                fontSize: BaseStyle.fontSize28,
                fontWeight: FontWeight.w600,
              ),
              controller: controller,
              onChanged: (String value) {},
              maxLines: 1,
              decoration: InputDecoration(
                isDense: true,
                contentPadding: EdgeInsets.symmetric(
                  vertical: 25.w,
                ),
                hintText: hintText,
                border: InputBorder.none, //去掉输入框的下滑线
                fillColor: Color(0xfffff4d7),
                filled: true,
                hintStyle: TextStyle(
                  color: BaseStyle.color999999,
                  fontSize: BaseStyle.fontSize28,
                  fontWeight: FontWeight.normal,
                ),
              ),
            ),
          ),
          isCode
              ? Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    SizedBox(
                      width: 2,
                      height: 29.w,
                      child: DecoratedBox(
                        decoration: BoxDecoration(color: Color(0xffd8d8d8)),
                      ),
                    ),
                    SizedBox(width: 16.w),
                    InkWell(
                      child: Text(
                        _verifyStr,
                        style: TextStyle(
                          color: BaseStyle.color999999,
                          fontSize: BaseStyle.fontSize28,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      onTap: null,
                    ),
                  ],
                )
              : SizedBox(),
        ],
      ),
    );
  }

  Container _containerImage() {
    return Container(
      alignment: Alignment.center,
      child: Image.asset(
        AssetsImage.LOGO,
        height: 184.w,
        width: 266.w,
      ),
    );
  }

  InkWell _inkWellLogin() {
    return InkWell(
      onTap: () {
        if (TextUtil.isEmpty(_phone.text))
          BotToast.showText(text: '手机号不能为空');
        else if (TextUtil.isEmpty(_code.text))
          BotToast.showText(text: '验证码不能为空');
        else {
          showCupertinoDialog(
            context: context,
            builder: (context) {
              return CupertinoAlertDialog(
                title: Text('点击登录即表示您已阅读并同意'),
                content: Text(
                    '''点击登录即表示您已阅读并同意《小蜜蜂用户协议》《小蜜蜂隐私政策》(特别是免除或限制责任、管辖等粗体下划线标注的条款)。如您不同意上述协议的任何条款,您应立即停止登录及使用本软件及服务。'''),
                actions: [
                  CupertinoDialogAction(
                    child: Text('同意'),
                    onPressed: () {
                      Future.delayed(
                        Duration(milliseconds: 1000 + Random().nextInt(500)),
                        () {
                          Navigator.pop(context);
                          (_phone.text == '18067170899') &&
                                  (_code.text == '123456')
                              ? ARoute.push(context, UserAuthenticationPage())
                              : BotToast.showText(text: '账号或密码错误!');
                        },
                      );
                    },
                  ),
                  CupertinoDialogAction(
                    child: Text('拒绝'),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ],
              );
            },
          );
        }
      },
      child: Container(
        alignment: Alignment.center,
        height: 89.w,
        width: 586.w,
        padding: EdgeInsets.only(top: 25.w, bottom: 24.w),
        margin: EdgeInsets.symmetric(horizontal: 82.w),
        decoration: BoxDecoration(
          color: Color(0xffffc40c),
          borderRadius: BorderRadius.all(Radius.circular(36)),
        ),
        child: Text(
          '登录',
          style: TextStyle(
            fontWeight: FontWeight.w600,
            fontSize: BaseStyle.fontSize28,
            color: ktextPrimary,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: _appBar(),
      body: SingleChildScrollView(
        child: Container(
          child: GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              FocusScope.of(context).requestFocus(FocusNode());
            },
            child: Container(
                color: Colors.white,
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    SizedBox(
                      height: 153.w,
                    ),
                    _containerImage(),
                    SizedBox(height: 16.w),
                    Container(
                      alignment: Alignment.center,
                      child: Text(
                        '欢迎登录小蜜蜂',
                        style: TextStyle(
                            fontWeight: FontWeight.w600,
                            fontSize: BaseStyle.fontSize38,
                            color: ktextPrimary),
                      ),
                    ),
                    SizedBox(height: 89.w),
                    _containerTextField(
                        AssetsImage.PHONELOGO, _phone, '请输入手机号码', false),
                    SizedBox(height: 27.w),
                    _containerTextField(
                        AssetsImage.CODELOGO, _code, '请输入验证码', true),
                    SizedBox(height: 59.w),
                    _inkWellLogin(),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        FlatButton(
                            padding: EdgeInsets.zero,
                            onPressed: () {
                              ARoute.push(context, AgreementPage());
                            },
                            child: Text(
                              '《小蜜蜂用户协议》',
                              style: TextStyle(
                                color: Colors.lightBlue,
                              ),
                            )),
                        SizedBox(width: 15.w),
                        FlatButton(
                            padding: EdgeInsets.zero,
                            onPressed: () {
                              ARoute.push(context, PrivacyPage());
                            },
                            child: Text(
                              '《小蜜蜂隐私政策》',
                              style: TextStyle(
                                color: Colors.lightBlue,
                              ),
                            ))
                      ],
                    ),
                  ],
                )),
          ),
        ),
      ),
    );
  }
}