import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/screens/login/login.controller.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';

class LoginScreen extends StatelessWidget {
  LoginScreen({super.key});
  final _controller = Get.put<LoginController>(LoginController());
  final double formWidth = 700;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/bg.jpg'),
                fit: BoxFit.cover,
              ),
            ),
            child: buildBody()));
  }

  Widget buildBody() {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          // 点击空白处时收起键盘
          FocusScope.of(Get.context!).unfocus();
        },
        child: Center(
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Image(
                        height: ScreenAdaper.width(150),
                        image: const AssetImage(
                          'assets/images/company_logo.png',
                        )),
                    SizedBox(
                      width: ScreenAdaper.width(10),
                    ),
                    Stack(
                      alignment: Alignment.center,
                      children: [
                        Text(
                          '山矿通',
                          style: TextStyle(
                              shadows: [
                                Shadow(
                                  color: Colors.black,
                                  blurRadius: ScreenAdaper.sp(10),
                                  offset: Offset(ScreenAdaper.width(5),
                                      ScreenAdaper.height(5)),
                                ),
                              ],
                              letterSpacing: ScreenAdaper.width(5),
                              fontSize: ScreenAdaper.sp(70),
                              fontWeight: FontWeight.bold,
                              foreground: Paint()
                                ..style = PaintingStyle.stroke
                                ..strokeWidth = 2
                                ..color = Colors.black),
                        ),
                        Text(
                          '山矿通',
                          style: TextStyle(
                              fontWeight: FontWeight.w700,
                              color: AppTheme.nearlyWhite.withOpacity(0.9),
                              letterSpacing: ScreenAdaper.sp(5),
                              fontSize: ScreenAdaper.sp(70)),
                        ),
                      ],
                    )
                  ],
                ),
                SizedBox(height: ScreenAdaper.height(50)),
                buildForm(),
                SizedBox(height: ScreenAdaper.height(50)),
                buildSubmitButton(),
                SizedBox(height: ScreenAdaper.height(50)),
              ],
            ),
          ),
        ));
  }

  Widget buildForm() {
    final children = [
      buildUserNameInput(),
      Divider(
        thickness: ScreenAdaper.height(2),
      ),
      buildPasswordInput(),
    ];

    final child = Column(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: children,
    );

    return Form(
        key: _controller.formKey,
        child: Container(
          padding: EdgeInsets.symmetric(vertical: ScreenAdaper.height(10)),
          decoration: BoxDecoration(
            boxShadow: [
              BoxShadow(
                color: AppTheme.black.withOpacity(0.3),
                spreadRadius: ScreenAdaper.sp(10),
                blurRadius: ScreenAdaper.sp(10),
                offset: Offset(
                    0, ScreenAdaper.height(2)), // changes position of shadow
              ),
            ],
            border: Border.all(width: 0),
            borderRadius: BorderRadius.circular(ScreenAdaper.sp(30)),
            color: AppTheme.nearlyWhite.withOpacity(0.9),
          ),
          alignment: Alignment.center,
          width: ScreenAdaper.width(formWidth),
          child: child,
        ));
  }

  Widget buildSubmitButton() {
    final button = ElevatedButton(
      style: ElevatedButton.styleFrom(
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(ScreenAdaper.sp(30))),
          padding: EdgeInsets.symmetric(
              horizontal: ScreenAdaper.height(25),
              vertical: ScreenAdaper.height(20)),
          foregroundColor: AppTheme.nearlyWhite,
          textStyle: TextStyle(
              letterSpacing: 5,
              fontWeight: FontWeight.bold,
              fontSize: ScreenAdaper.sp(25)),
          backgroundColor: AppTheme.primaryColor),
      onPressed: _controller.doLogin,
      child: const Text('登录'),
    );
    final child = Expanded(
      child: button,
    );
    return Container(
      decoration: BoxDecoration(
        border: Border.all(width: 0),
        borderRadius: BorderRadius.circular(ScreenAdaper.sp(30)),
        boxShadow: [
          BoxShadow(
            color: AppTheme.black.withOpacity(0.3),
            spreadRadius: ScreenAdaper.sp(10),
            blurRadius: ScreenAdaper.sp(10),
            offset:
                Offset(0, ScreenAdaper.height(5)), // changes position of shadow
          ),
        ],
      ),
      alignment: Alignment.center,
      width: ScreenAdaper.width(formWidth),
      child: Row(
        children: [child],
      ),
    );
  }

  Widget buildUserNameInput() {
    return TextFormField(
      // 英文数字键盘
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
          prefixIcon: Icon(
            Icons.person_2_outlined,
            size: ScreenAdaper.sp(40),
          ),
          errorStyle: TextStyle(fontSize: ScreenAdaper.sp(20)),
          contentPadding: EdgeInsets.symmetric(
              horizontal: ScreenAdaper.width(30),
              vertical: ScreenAdaper.height(10)),
          hintText: '用户名',
          border: InputBorder.none,
          focusedBorder: InputBorder.none),
      onFieldSubmitted: (value) {
        _controller.passwordFocusNode.requestFocus();
      },
      autovalidateMode: AutovalidateMode.onUserInteraction,
      style: TextStyle(fontSize: ScreenAdaper.sp(25)),
      onChanged: (value) {
        _controller.username = value;
      },
      validator: (value) => value!.isEmpty ? '用户名不能为空' : null,
    );
  }

  Widget buildPasswordInput() {
    return TextFormField(
      decoration: InputDecoration(
          prefixIcon: Icon(
            Icons.lock_outlined,
            size: ScreenAdaper.sp(40),
          ),
          errorStyle: TextStyle(fontSize: ScreenAdaper.sp(20)),
          contentPadding: EdgeInsets.symmetric(
              horizontal: ScreenAdaper.width(30),
              vertical: ScreenAdaper.height(10)),
          hintText: '密码',
          border: InputBorder.none,
          focusedBorder: InputBorder.none),
      obscureText: true,
      focusNode: _controller.passwordFocusNode,
      onFieldSubmitted: (value) {
        _controller.doLogin();
      },
      style: TextStyle(fontSize: ScreenAdaper.sp(25)),
      onChanged: (value) {
        _controller.password = value;
      },
      autovalidateMode: AutovalidateMode.onUserInteraction,
      validator: (value) => value!.isEmpty ? '密码不能为空' : null,
    );
  }

  Widget buildForgotPassword() {
    return GestureDetector(
      onTap: () {},
      child: Container(
          alignment: Alignment.centerRight,
          child: Text(
            '忘记密码?',
            style: TextStyle(
              color: Colors.grey,
              fontSize: ScreenAdaper.sp(20),
              decoration: TextDecoration.underline,
            ),
          )),
    );
  }
}
