import 'package:baseproject/core/common/index.dart';
import 'package:baseproject/core/components/index.dart';
import 'package:baseproject/features/presentation/account/bloc/login_bloc.dart';
import 'package:baseproject/features/repositories/hra_repository.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

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

  @override
  State<LoginScreen> createState() => _LoginScreenState();
}

class _LoginScreenState extends State<LoginScreen> {
  final GlobalKey<FormBuilderState> _formKey = GlobalKey<FormBuilderState>();
  bool _rememberMe = false;
  final LoginBloc _loginBloc = getItSuper<LoginBloc>();

  Future<void> _onSubmit() async {
    final formState = _formKey.currentState;
    if (formState == null) return;

    if (!formState.saveAndValidate()) {
      return;
    }
    final value = formState.value;

    final dto = LoginDto(
      userName: value['userName'] as String?,
      password: value['password'] as String?,
      rememberMe: _rememberMe,
    );
    final captcha = _loginBloc.state.model.captcha;
    dto.captchaText = "999999"; //captcha?.dntCaptchaTextValue;
    dto.captchaToken = captcha?.dntCaptchaTokenValue;
    dto.captchaInputText = "999999";

    _loginBloc.login(dto, context);
  }

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => _loginBloc,
      child: Scaffold(
        appBar: AppBar(
          title: const Text('Đăng nhập'),
        ),
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.all(16),
            child: FormBuilder(
              initialValue: kDebugMode
                  ? {
                      'userName': 'quylx',
                      'password': 'BearCMS0011002848238master',
                    }
                  : {},
              key: _formKey,
              autovalidateMode: AutovalidateMode.onUserInteraction,
              child: BlocBuilder<LoginBloc, BaseStateBloc<LoginViewModel>>(
                builder: (context, state) {
                  final vm = state.model;
                  final isLoading = state is LoadingState<LoginViewModel> || vm.isLoading;

                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: <Widget>[
                      CustomTextField(
                        name: 'userName',
                        labelText: 'Tên đăng nhập',
                        isLabelTop: true,
                        isShowTextRequire: true,
                        validator: FormBuilderValidators.required<String>(
                          context,
                        ),
                      ),
                      ConstantWidget.heightSpace16,
                      TextFieldPassword(
                        name: 'password',
                        labelText: 'Mật khẩu',
                        validator: FormBuilderValidators.required<String>(
                          context,
                        ),
                      ),
                      ConstantWidget.heightSpace24,
                      SizedBox(
                        height: 48,
                        child: ConstantWidget.buildPrimaryButton(
                          onPressed: isLoading ? null : _onSubmit,
                          text: 'Đăng nhập',
                        ),
                      ),
                    ],
                  );
                },
              ),
            ),
          ),
        ),
      ),
    );
  }
}
