import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:tm_app/core/theme/colors.dart';

import '../../../core/constants/assets.dart';
import '../../../core/routes/app_routes.dart';
import '../../../core/utils/app_toast.dart';
import '../../../core/utils/size_config.dart';
import '../../../view_models/auth_view_model.dart';
import '../strings/login_strings.dart';
import '../widgets/widgets.dart';

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

  @override
  State<LoginTabletPage> createState() => _LoginTabletPageState();
}

class _LoginTabletPageState extends State<LoginTabletPage> {
  late final AuthViewModel viewModel;

  @override
  void initState() {
    super.initState();
    viewModel = context.read<AuthViewModel>();
    viewModel.addListener(_viewModelListener);
  }

  void _viewModelListener() {
    if (viewModel.loggedInUser != null) {
      Router.neglect(context, () => const HomeRouteData().go(context));
    }
    if (viewModel.errorMessage != null) {
      AppToast.error(context, viewModel.errorMessage!);
    }
  }

  @override
  void dispose() {
    viewModel.removeListener(_viewModelListener);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.backgroundColor,
      body: Center(
        child: SingleChildScrollView(
          padding: EdgeInsets.symmetric(horizontal: 24.0.w()),
          child: SizedBox(
            width: 526,
            child: Consumer<AuthViewModel>(
              builder: (context, viewModel, _) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    LoginLogo(width: 190.0.w(), height: 88.0.h()),
                    SizedBox(height: 32.0.h()),
                    const LoginTitle(),
                    SizedBox(height: 32.0.h()),
                    LoginTextField(
                      controller: viewModel.usernameController,
                      focusNode: viewModel.usernameFocus,
                      label: LoginStrings.usernameLabel,
                      iconPath: AssetsManager.userIcon,
                      fieldHeight: 60.0.h(),
                      borderRedus: 12,
                      prefixIconPadding: 4.0.w(),
                    ),
                    SizedBox(height: 16.0.h()),
                    LoginTextField(
                      controller: viewModel.passwordController,
                      focusNode: viewModel.passwordFocus,
                      label: LoginStrings.passwordLabel,
                      iconPath: AssetsManager.passwordIcon,
                      prefixIconPadding: 4.0.w(),
                      isPassword: true,
                      obscureText: viewModel.obscurePassword,
                      fieldHeight: 60.0.h(),
                      onToggleVisibility: viewModel.togglePasswordVisibility,
                      borderRedus: 12,
                    ),
                    SizedBox(height: 32.0.h()),
                    if (viewModel.isLoading)
                      const BaseButton(
                        isEnabled: false,
                        onPressed: null,
                        isLoading: true,
                      )
                    else
                      BaseButton(
                        isEnabled: viewModel.isLoginEnabled,
                        onPressed:
                            viewModel.isLoginEnabled
                                ? () async {
                                  await viewModel.login();
                                }
                                : null,
                      ),
                    const SizedBox(height: 32.0),
                    TextButton(
                      onPressed: () {
                        const ForgotPasswordRouteData().push(context);
                      },
                      child: Text(
                        LoginStrings.forgotPassword,
                        style: TextStyle(
                          fontSize: 14.0.sp(),
                          fontWeight: FontWeight.w500,
                          color: AppColors.mainBlue,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
