import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:agrilink_vocpro/core/route/app_route.dart';
import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/core/widgets/app_button.dart';
import 'package:agrilink_vocpro/core/widgets/app_textfield.dart';
import 'package:agrilink_vocpro/core/widgets/password_textfield.dart';
import 'package:agrilink_vocpro/features/auth/provider/auth_provider.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Consumer<AuthProvider>(
          builder: (context, authP, child) {
            return ListView(
              padding: const EdgeInsets.all(16),
              children: [
                const SizedBox(height: 40),
                Text(
                  'Hello, Welcome back 👋',
                  style: AppTheme.titleLarge,
                ),
                Text(
                  'Happy to have you back',
                  style: AppTheme.titleMedium,
                ),
                const SizedBox(height: 40),
                Text('Email address', style: AppTheme.labelLarge),
                const SizedBox(height: 4),
                AppTextfield(
                  controller: authP.emailController,
                  hintText: 'Masukkan email',
                ),
                const SizedBox(height: 24),
                Text('Password', style: AppTheme.labelLarge),
                const SizedBox(height: 4),
                PasswordTextfield(
                  controller: authP.passwordController,
                  hintText: 'Masukkan password',
                ),
                const SizedBox(height: 24),
                Row(
                  children: [
                    Checkbox(
                      value: authP.isRememberMe,
                      onChanged: (value) {
                        authP.setRememberMe(value!);
                      },
                    ),
                    Text('Remember me', style: AppTheme.labelLarge),
                  ],
                ),
                const SizedBox(height: 24),
                authP.loginState == ResultState.loading
                    ? const Center(child: CircularProgressIndicator())
                    : AppButton(
                        onPressed: () async {
                          await authP.login(
                            context,
                            email: authP.emailController.text,
                            password: authP.passwordController.text,
                          );
                          if (context.mounted) {
                            if (authP.loginState == ResultState.hasData) {
                              context.go(AppRoute.dashboard);
                              authP.controllerClear();
                              authP.loginState = ResultState.initial;
                            }
                          }
                        },
                        text: 'Login',
                      ),
                SizedBox(height: 24),
                // AppButton(
                //     onPressed: () {
                //       authP.getAccessToken();
                //     },
                //     text: 'Get Access Token'),
              ],
            );
          },
        ),
      ),
    );
  }
}
