import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../controllers/localization/localization.dart';
import '../../controllers/providers/settings/setting_provider.dart';
import '../../controllers/providers/user_provider.dart';
import '../../controllers/validator/validator.dart';
import '../../models/subtitle.dart';
import '../../models/user.dart';
import '../app_style/sizing.dart';
import '../widgets/app_text_form_field.dart';
import '../widgets/buttons/app_button.dart';
import '../widgets/loaders/loading_manager.dart';

class Login extends StatefulWidget {
  static const String id = "/login";

  const Login({super.key});

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

class LoginState extends State<Login> {
  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  bool _obscurePassword = true;
  late double _height;
  final User _user = User();

  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _height = MediaQuery.of(context).size.height;
    Subtitle? subtitle = AppLocalization.of(context)?.subtitle;
    return Scaffold(
      key: _scaffoldKey,
      body: SafeArea(
        child: LoadingManager(
          isLoading: (_userProvider.loading) || !(_settingProvider.isLoaded),
          isFailedLoading: false,
          stateCode: 200,
          onRefresh: () async {},
          child: Form(
            key: _formKey,
            child: SingleChildScrollView(
              //padding: EdgeInsets.symmetric(horizontal: 32),
              child: Column(
                children: [
                  //AppNameBar(),
                  SizedBox(
                    height: MediaQuery.of(context).size.height / 7,
                  ),
                  Hero(
                    tag: "logo",
                    child: Image(
                      height: _height / 6,
                      fit: BoxFit.contain,
                      image: const AssetImage("assets/images/logo.png"),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 24 * AppStyle.getScaleFactor(context), vertical: 24 * AppStyle.getScaleFactor(context)),
                    child: Column(
                      children: [
                        SizedBox(
                          height: 24 * AppStyle.getScaleFactor(context),
                        ),
                        ATextFormField(
                          initialValue: _user.userName ?? "",
                          hintText: subtitle?.name ?? "",
                          textAlign: TextAlign.left,
                          style: Theme.of(context).textTheme.bodyLarge,
                          prefixIconData: Icons.account_circle,
                          validator: (value) => Validator.hasValue(value ?? '') ? null : subtitle?.nameValidateMessage,
                          textInputType: TextInputType.name,
                          onSaved: (value) {
                            if (value != null) {
                              _user.userName = value;
                            }
                          },
                        ),
                        const SizedBox(height: 12),
                        ATextFormField(
                          initialValue: _user.password,
                          hintText: subtitle?.password ?? "",
                          obscureText: _obscurePassword,
                          style: Theme.of(context).textTheme.bodyLarge,
                          prefixIconData: Icons.vpn_key_sharp,
                          textAlign: TextAlign.left,
                          validator: (value) => Validator.isValidPassword(value ?? '') ? null : subtitle?.passwordValidateMessage,
                          showPassword: () {
                            _obscurePassword = !_obscurePassword;
                            setState(() {});
                          },
                          onSaved: (value) {
                            _user.password = value!;
                          },
                        ),
                        SizedBox(
                          height: 32 * AppStyle.getScaleFactor(context),
                        ),
                        AButton(
                          text: subtitle?.signIn ?? "",
                          onPressed: () async {
                            if (_formKey.currentState?.validate() ?? false) {
                              _formKey.currentState?.save();
                              await _userProvider.login(context, user: _user);
                            }
                          },
                        ),
                        // SizedBox(
                        //   height: 140 * AppStyle.getScaleFactor(context),
                        // ),
                        // AOutLinedButton(
                        //   text: _subtitle.signUp,
                        //   //color: AColors.cyan,
                        //   onPressed: () {
                        //     Navigator.of(context).pushNamed(Register.id);
                        //   },
                        // ),
                        const SizedBox(height: 32),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}
