import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.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 LoginMobilePage extends StatefulWidget {
  const LoginMobilePage({super.key});

  @override
  State<LoginMobilePage> createState() => _LoginMobilePageState();
}

class _LoginMobilePageState extends State<LoginMobilePage> {
  late final AuthViewModel viewModel;
  @override
  void initState() {
    viewModel = context.read<AuthViewModel>();
    viewModel.addListener(_viewModelListener);
    super.initState();
  }

  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: SafeArea(
        child: Center(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(horizontal: 24.0.w()),
            child: Consumer<AuthViewModel>(
              builder: (context, viewModel, _) {
                return Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // LoginLogo(width: 190.0.w(), height: 88.0.h()),
                    Padding(
                      padding: EdgeInsets.symmetric(horizontal: 22.0.w()),
                      child: SvgPicture.asset(
                        AssetsManager.logoBig,
                        width: double.infinity,
                        height: 50.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,
                    ),
                    SizedBox(height: 16.0.h()),

                    LoginTextField(
                      controller: viewModel.passwordController,
                      focusNode: viewModel.passwordFocus,
                      label: LoginStrings.passwordLabel,
                      iconPath: AssetsManager.passwordIcon,
                      isPassword: true,
                      obscureText: viewModel.obscurePassword,
                      onToggleVisibility: viewModel.togglePasswordVisibility,
                    ),
                    SizedBox(height: 32.0.h()),

                    if (viewModel.isLoading)
                      BaseButton(
                        isEnabled: false,
                        onPressed: null,
                        isLoading: true,
                        textColor: AppColors.white,
                      )
                    else
                      BaseButton(
                        isEnabled: viewModel.isLoginEnabled,
                        onPressed: () async {
                          if (viewModel.isLoginEnabled) {
                            await viewModel.login();
                          }
                        },
                      ),
                    SizedBox(height: 32.0.h()),
                    TextButton(
                      onPressed: () {
                        const ForgotPasswordRouteData().push(context);
                      },
                      child: Text(
                        LoginStrings.forgotPassword,
                        style: TextStyle(
                          fontSize: 14.0.sp(),
                          fontWeight: FontWeight.w500,
                          color: AppColors.mainBlue,
                        ),
                      ),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
