import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:frontend_eccp_mobile/app/core/constants/constants.dart';
import 'package:frontend_eccp_mobile/app/core/constants/enum/snackbar_type_enum.dart';
import 'package:frontend_eccp_mobile/app/core/di/injection.dart';
import 'package:frontend_eccp_mobile/app/core/navigation/app_navigator.dart';
import 'package:frontend_eccp_mobile/app/core/navigation/app_routes.dart';
import 'package:frontend_eccp_mobile/app/core/services/crashlytics_service.dart';
import 'package:frontend_eccp_mobile/app/core/utils/app_validators.dart';
import 'package:frontend_eccp_mobile/app/core/utils/show_message.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/app_button.dart';
import 'package:frontend_eccp_mobile/app/core/widgets/app_shad_textfield.dart';
import 'package:frontend_eccp_mobile/modules/auth/login/bloc/login_cubit.dart';
import 'package:frontend_eccp_mobile/modules/auth/login/bloc/login_state.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => getIt<LoginCubit>(),
      child: const LoginUI(),
    );
  }
}

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

  @override
  State<LoginUI> createState() => _LoginUIState();
}

class _LoginUIState extends State<LoginUI> {
  final formKey = GlobalKey<ShadFormState>();

  @override
  void dispose() {
    super.dispose();
  }

  Future<void> _onLogin() async {
    if (!formKey.currentState!.saveAndValidate()) return;

    await CrashlyticsService.log('Login attempt');

    if (!mounted) return;

    await context.read<LoginCubit>().login(
      nrp: formKey.currentState!.value['nrp']?.toString() ?? '',
      password: formKey.currentState!.value['password']?.toString() ?? '',
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.neutralWhite,
      body: BlocConsumer<LoginCubit, LoginState>(
        listener: (context, state) async {
          await AppNavigator.clearAndPush(
            context,
            AppRoutes.home,
            onOpened: () {
              showMessage(
                context,
                'Selamat, anda berhasil masuk',
                SnackBarType.success,
              );
            },
          );
          // if (state is LoginSuccess) {
          //   await AppNavigator.clearAndPush(
          //     context,
          //     AppRoutes.home,
          //     onOpened: () {
          //       showMessage(
          //         context,
          //         'Selamat, anda berhasil masuk',
          //         SnackBarType.success,
          //       );
          //     },
          //   );
          // } else if (state is LoginFailure && state.hasMessage) {
          //   showMessage(
          //     context,
          //     state.message ?? '',
          //     state.snackBarType,
          //     duration: state.duration,
          //   );
          // }
        },
        builder: (context, state) {
          return SafeArea(
            child: ShadForm(
              key: formKey,
              child: Align(
                alignment: Alignment.topCenter,
                child: ConstrainedBox(
                  constraints: const BoxConstraints(maxWidth: 520),
                  child: Padding(
                    padding: const EdgeInsets.all(8),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        AppSpacing.h48,

                        Padding(
                          padding: AppPadding.h16,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                'E-CPP DIVPROPAM POLRI',
                                style: AppTextStyles.h1,
                                textAlign: TextAlign.start,
                              ),

                              Text(
                                'Sign-in to continue',
                                style: AppTextStyles.h2Light,
                                textAlign: TextAlign.start,
                              ),
                            ],
                          ),
                        ),

                        Padding(
                          padding: AppPadding.h12,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              AppSpacing.h36,

                              AppShadInput(
                                title: 'NRP',
                                hint: 'Enter your NRP',
                                isForm: true,
                                id: 'nrp',
                                validator: (v) => AppValidators.required(
                                  v,
                                  message: 'NRP tidak boleh kosong',
                                ),
                              ),

                              AppSpacing.h16,

                              const AppShadInput(
                                title: 'Password',
                                hint: 'Enter your password',
                                isForm: true,
                                isPassword: true,
                                id: 'password',
                                validator: AppValidators.password,
                              ),
                              AppSpacing.h32,

                              AppButton(
                                label: 'Login',
                                isExpanded: true,
                                onPressed: _onLogin,
                                isLoading: state is LoginLoading,
                              ),
                            ],
                          ),
                        ),

                        const Spacer(),

                        Align(
                          alignment: Alignment.bottomCenter,
                          child: Text(
                            '© 2026 E-CPP MABES POLRI. All rights reserved.',
                            style: AppTextStyles.body.copyWith(
                              color: AppColors.neutral600,
                            ),
                          ),
                        ),

                        AppSpacing.h40,
                      ],
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      ),
    );
  }
}
