import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/const_text.dart';
import 'package:making_school_asignment_app/common/utils/anti_shake_throttling.dart';
import 'package:making_school_asignment_app/common/utils/app_upgrade/upgradeLogic.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

import 'children/sys_protocol.dart';
import 'login_logic.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final logic = Get.find<LoginLogic>();
  final upgradeLogic = Get.find<UpgradeLogic>();
  final state = Get.find<LoginLogic>().state;

  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
      statusBarColor: Colors.transparent, //状态栏背景颜色
    ));

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await sysProtocol(context);

      /// 为了发布各平台方便审核  不能再登陆页面直接弹出审核
      // await Future.delayed(Duration.zero, () => upgradeLogic.getAppUpgrade(context));
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        Utils.hideKeyboard();
        // FocusScope.of(context).requestFocus(state.theFocus);
      },
      child: AnnotatedRegion(
        value: const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          systemNavigationBarIconBrightness: Brightness.light,
          statusBarIconBrightness: Brightness.light,
          statusBarBrightness: Brightness.dark,
        ),
        child: Scaffold(
          backgroundColor: Colors.white,
          resizeToAvoidBottomInset: false,
          body: Stack(
            children: [
              Positioned(
                  top: 0,
                  left: 0,
                  child: Image.asset(
                    'assets/images/logo_banner.png',
                    width: Get.width,
                    fit: BoxFit.fill,
                  )),
              SingleChildScrollView(
                child: Column(
                  children: [
                    SizedBox(
                      height: 130.r,
                    ),
                    Container(
                      width: 77.w,
                      height: 77.w,
                      alignment: Alignment.center,
                      child: SizedBox(
                        height: 77.w,
                        width: 77.w,
                        child: Image.asset('assets/images/login_logo_icon.png', fit: BoxFit.cover),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 90.r),
                      padding: EdgeInsets.only(top: 50.h, bottom: 16.h, left: 40.w, right: 40.w),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius:
                            BorderRadius.only(topLeft: Radius.circular(30.r), topRight: Radius.circular(30.r)),
                        /*boxShadow: const [
                          BoxShadow(
                            color: Color.fromRGBO(46, 91, 255, 0.1),
                            offset: Offset.zero, //阴影y轴偏移量
                            blurRadius: 100, //阴影模糊程度
                            spreadRadius: 100, //阴影扩散程度
                          )
                        ],*/
                      ),
                      child: Column(children: [
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 15.w,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.transparent,
                            border: Border.all(width: 1.w, color: const Color(0xFF434343)),
                            borderRadius: BorderRadius.all(Radius.circular(17.w)),
                          ),
                          child: TextField(
                            controller: state.userNameController,
                            /* maxLines: 1,
                            maxLength: 20,*/
                            enableInteractiveSelection: false,
                            textInputAction: TextInputAction.next,
                            onEditingComplete: () {
                              Get.focusScope?.nextFocus();
                              // FocusScope.of(context).requestFocus(_pwdFocus);
                            },
                            style: TextStyle(
                              color: const Color(0xFF434343),
                              fontSize: 14.sp,
                            ),
                            decoration: InputDecoration(
                              hintText: "请输入账号",
                              hintStyle: TextStyle(
                                fontSize: 14.sp,
                                color: const Color(0xFF434343),
                              ),
                              // labelText: "账号",
                              labelStyle: TextStyle(
                                fontSize: 14.sp,
                                color: const Color(0xFF434343),
                              ),
                              border: InputBorder.none,
                              isDense: true,
                              prefixIconConstraints: BoxConstraints(
                                minHeight: 10.w,
                                minWidth: 10.h,
                              ),
                              prefixIcon: Padding(
                                padding: EdgeInsets.only(right: 5.r),
                                child: Image.asset(
                                  'assets/images/login_account.png',
                                  width: 15.r,
                                  height: 15.r,
                                ),
                              ),
                            ),
                          ),
                        ),
                        SizedBox(
                          height: 20.r,
                        ),
                        Obx(() {
                          return Container(
                            padding: EdgeInsets.symmetric(horizontal: 15.w),
                            decoration: BoxDecoration(
                              color: Colors.transparent,
                              border: Border.all(width: 1.w, color: const Color(0xFF434343)),
                              borderRadius: BorderRadius.all(Radius.circular(17.w)),
                            ),
                            child: TextField(
                              focusNode: state.pwdFocus,
                              controller: state.passwordController,
                              keyboardType: TextInputType.number,
                              maxLines: 1,
                              obscureText: state.isShowPwd.value,
                              //隐藏密码显示
                              // textInputAction: state.isShowPwd.value?TextInputAction.go:TextInputAction.next,
                              textInputAction: TextInputAction.send,
                              enableInteractiveSelection: false,
                              onSubmitted: (_) => easyThrottle('LOGIN_EASYTHROTTLE', () async {
                                Utils.hideKeyboard();
                                await Future.delayed(const Duration(milliseconds: 300));
                                WidgetsBinding.instance
                                    .addPostFrameCallback((_) => logic.toLogin(context, upgradeLogic));
                              }),
                              style: TextStyle(
                                color: const Color(0xFF434343),
                                fontSize: 14.sp,
                              ),
                              decoration: InputDecoration(
                                hintText: "请输入密码",
                                prefixIconConstraints: BoxConstraints(
                                  minHeight: 10.w,
                                  minWidth: 10.h,
                                ),
                                prefixIcon: Padding(
                                  padding: EdgeInsets.only(right: 5.r),
                                  child: Image.asset(
                                    'assets/images/login_pwd.png',
                                    width: 15.r,
                                    height: 15.r,
                                  ),
                                ),
                                suffixIconConstraints: BoxConstraints(
                                  minHeight: 10.w,
                                  minWidth: 10.h,
                                ),
                                suffixIcon: InkWell(
                                  onTap: () {
                                    state.isShowPwd.value = !state.isShowPwd.value;
                                  },
                                  child: Padding(
                                    padding: EdgeInsets.only(right: 5.r),
                                    child: Image.asset(
                                      state.isShowPwd.value
                                          ? 'assets/images/eye_default.png'
                                          : 'assets/images/eye_active.png',
                                      width: 15.r,
                                      height: 15.r,
                                    ),
                                  ),
                                ),
                                hintStyle: TextStyle(
                                  fontSize: 14.sp,
                                  color: const Color(0xFF434343),
                                ),
                                border: InputBorder.none,
                                // labelText: "密码",
                                isDense: true,
                                labelStyle: TextStyle(
                                  fontSize: 14.sp,
                                  color: Colors.white,
                                ),
                              ),
                            ),
                          );
                        }),
                        SizedBox(height: 10.h),
                        Row(
                          children: [
                            Expanded(
                              child: Row(
                                children: [
                                  Container(
                                    width: 25.w,
                                    padding: EdgeInsets.only(right: 0.w),
                                    child: Obx(() {
                                      return Transform.scale(
                                        scale: 1.0,
                                        child: Checkbox(
                                            // activeColor: Colors.transparent, //去掉勾选时背景颜色

                                            activeColor: Theme.of(context).primaryColor,
                                            // checkColor: Colors.white,
                                            value: state.keepPwd.value,
                                            onChanged: (value) {
                                              // Get.focusScope?.nextFocus();
                                              FocusScope.of(context).requestFocus(state.pwdFocus);
                                              FocusScope.of(context).requestFocus(state.theFocus);
                                              state.keepPwd.value = value ?? false;
                                            },
                                            side: WidgetStateBorderSide.resolveWith(
                                              (Set<WidgetState> states) {
                                                if (states.contains(WidgetState.selected)) {
                                                  //修改勾选时边框颜色为红色
                                                  return BorderSide(
                                                      width: 1.5.r, color: Theme.of(context).primaryColor);
                                                }
                                                //修改默认时边框颜色为绿色
                                                return BorderSide(width: 1.r, color: const Color(0xFF434343));
                                              },
                                            )),
                                      );
                                    }),
                                  ),
                                  InkWell(
                                    onTap: () {
                                      Utils.hideKeyboard();
                                      Get.focusScope?.nextFocus();
                                      Get.focusScope?.nextFocus();
                                      FocusScope.of(context).requestFocus(state.pwdFocus);
                                      FocusScope.of(context).requestFocus(state.theFocus);
                                      state.keepPwd.value = !state.keepPwd.value;
                                    },
                                    child: Text(
                                      '记住密码',
                                      style: TextStyle(
                                        fontSize: 12.sp,
                                        color: const Color(0xFF434343),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            InkWell(
                              onTap: () => Get.toNamed(Routes.register),
                              child: quickText('账号注册', color: const Color(0xFF434343)),
                            )
                          ],
                        ),
                        InkWell(
                          onTap: () => easyThrottle('LOGIN_EASYTHROTTLE', () async {
                            Utils.hideKeyboard();
                            await Future.delayed(Duration.zero);
                            WidgetsBinding.instance.addPostFrameCallback((_) => logic.toLogin(context, upgradeLogic));
                          }),
                          child: Obx(() {
                            return Container(
                                margin: EdgeInsets.symmetric(vertical: 10.h),
                                decoration: BoxDecoration(
                                  color: state.canLogin.value ? const Color(0xFF8C68FF) : const Color(0xFFdddddd),
                                  /*boxShadow: [
                                  BoxShadow(
                                    color:
                                        const Color.fromRGBO(76, 199, 147, 0.5),
                                    offset: Offset(6.w, 10.h), //阴影y轴偏移量
                                    blurRadius: 14, //阴影模糊程度
                                    spreadRadius: 0.5, //阴影扩散程度
                                  )
                                ],*/
                                  borderRadius: BorderRadius.all(
                                    Radius.circular(17.w),
                                  ),
                                ),
                                alignment: Alignment.center,
                                width: double.infinity,
                                height: 50.h,
                                child: quickText('登 录', size: 18.sp, color: Colors.white));
                          }),
                        ),
                        Row(
                          children: [
                            Container(
                              width: 25.w,
                              padding: EdgeInsets.only(right: 0.w),
                              child: Obx(() {
                                return Transform.scale(
                                  scale: 1.0,
                                  child: Checkbox(
                                    activeColor: Theme.of(context).primaryColor,
                                    checkColor: Colors.white,
                                    value: state.readAgreement.value,
                                    onChanged: (value) {
                                      Utils.hideKeyboard();
                                      /* FocusScope.of(context).requestFocus(
                                          state.pwdFocus);
                                      FocusScope.of(context).requestFocus(
                                          state.theFocus);*/
                                      state.readAgreement.value = value ?? false;
                                    },
                                    side: WidgetStateBorderSide.resolveWith(
                                      (Set<WidgetState> states) {
                                        if (states.contains(WidgetState.selected)) {
                                          //修改勾选时边框颜色为红色
                                          return BorderSide(width: 1.5.r, color: Theme.of(context).primaryColor);
                                        }
                                        //修改默认时边框颜色为绿色
                                        return BorderSide(width: 1.r, color: const Color(0xFF434343));
                                      },
                                    ),
                                  ),
                                );
                              }),
                            ),
                            InkWell(
                              onTap: () {
                                Get.toNamed(Routes.agreementPage,
                                    arguments: {"type": AGREEMENT_KEY.USER_AGREEMENT.name});
                              },
                              child: quickText(
                                '请仔细阅读',
                                size: 11.sp,
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                Get.toNamed(Routes.agreementPage,
                                    arguments: {"type": AGREEMENT_KEY.USER_AGREEMENT.name});
                              },
                              child: Text(
                                '《用户协议》',
                                style: TextStyle(fontSize: 12.r, color: Theme.of(context).primaryColor),
                              ),
                            ),
                            InkWell(
                              onTap: () {
                                Get.toNamed(Routes.agreementPage,
                                    arguments: {"type": AGREEMENT_KEY.PRIVACY_GREEMENT.name});
                              },
                              child: quickText(
                                '《隐私协议》',
                                size: 12.sp,
                                color: Theme.of(context).primaryColor,
                              ),
                            ),
                          ],
                        ),
                      ]),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
