import 'package:flutter/material.dart';
import 'package:flutter_login/flutter_login.dart';

import '../basic/Method.dart';
import 'AppScreen.dart';
// import 'dashboard_screen.dart';

const users = {
  'dribbble@gmail.com': '12345',
  'hunter@gmail.com': 'hunter',
};

class LoginScreen extends StatelessWidget {
  const LoginScreen({Key? key}) : super(key: key);

  get loginTime => const Duration(milliseconds: 2250);

  _authUser(BuildContext context) {
    return (LoginData data) {
      debugPrint('Name: ${data.name}, Password: ${data.password}');
      method.name = data.name;
      method.password = data.password;
      method.login().then((value) {
        if (!value) return false;
        Navigator.pushReplacement(
          context,
          MaterialPageRoute(builder: (context) => const AppScreen()),
        );
        return true;
      });
      // if (!widget.mounted) return; // don't use context after async if you're not sure your widget is mounted.
    };
  }

  Future<String?> _signupUser(SignupData data) {
    debugPrint('Signup Name: ${data.name}, Password: ${data.password}');
    return Future.delayed(loginTime).then((_) {
      return null;
    });
  }

  Future<String?> _recoverPassword(String name) {
    debugPrint('Name: $name');
    return Future.delayed(loginTime).then((_) {
      if (!users.containsKey(name)) {
        return 'User not exists';
      }
      return null;
    });
  }

  @override
  Widget build(BuildContext context) {
    return FlutterLogin(
      title: 'PrimeChat',
      logo: const AssetImage('lib/assets/avatar.jpg'),
      userType: LoginUserType.name,
      userValidator: (_) => null,
      onLogin: _authUser(context),
      onSignup: _signupUser,
      onSubmitAnimationCompleted: () {
        // Navigator.of(context).pushReplacement(MaterialPageRoute(
        //   builder: (context) => DashboardScreen(),
        // ));
      },
      onRecoverPassword: _recoverPassword,
    );
  }
}