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

  @override
  State<LoginDesktopPage> createState() => _LoginDesktopPageState();
}

class _LoginDesktopPageState extends State<LoginDesktopPage> {
  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));
    } else 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: Row(
          mainAxisAlignment:
              kIsWeb ? MainAxisAlignment.spaceAround : MainAxisAlignment.center,
          children: [
            SizedBox(
              width: 536,
              child: Consumer<AuthViewModel>(
                builder: (context, viewModel, _) {
                  return Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SvgPicture.asset(
                        AssetsManager.logoBig,
                        width: 320,
                        height: 50,
                        fit: BoxFit.cover,
                      ),
                      SizedBox(height: 72.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: 7.0.w(),
                      ),
                      SizedBox(height: 16.0.h()),
                      LoginTextField(
                        controller: viewModel.passwordController,
                        focusNode: viewModel.passwordFocus,
                        label: LoginStrings.passwordLabel,
                        iconPath: AssetsManager.passwordIcon,
                        prefixIconPadding: 7.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: () async {
                            if (viewModel.isLoginEnabled) {
                              await viewModel.login();
                            }
                          },
                        ),
                      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,
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
            ),
            kIsWeb
                ? Padding(
                  padding: EdgeInsets.symmetric(vertical: 20.0.h()),
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(32),
                    child: Image.asset(
                      AssetsManager.webLoginImage,
                      width: 796.0,
                      height: 944.0,
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                )
                : Container(),
          ],
        ),
      ),
    );
  }
}
