// Copyright (C) 2023 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

part of 'sign_up_cubit.dart';

/// {@template base_sign_up_cubit}
/// Abstract sign up cubit useful for implementing a cubit with fine
/// granularity by adding only the required mixins.
/// {@endtemplate}
abstract class BaseSignUpCubit<Data> extends FormDataCubit<SignUpState> {
  /// {@macro base_sign_up_cubit}
  BaseSignUpCubit({
    required this.authenticationRepository,
  }) : super(
          SignUpState(
            form: authenticationRepository.formRepository
                .accessForm(AuthFormName.signUpForm),
          ),
        );

  /// The authentication repository.
  final AuthenticationRepository<Data> authenticationRepository;
  FormRepository get formRepository => authenticationRepository.formRepository;

  @override
  String get formName => AuthFormName.signUpForm;

  @override
  FutureOr<void> dataChanged<Value>(
    String key,
    FormInputValidator<Value, ValidationError> dirtyValue,
  ) {
    final form = formRepository.accessForm(formName).clone();

    try {
      form.updateValidator(key, dirtyValue);
      formRepository.updateForm(form);
    } catch (e) {
      rethrow;
    }

    emit(
      SignUpState(form: form, status: form.validate()),
    );
  }

  @override
  FutureOr<void> reset() {
    final form = state.form.reset();
    formRepository.updateForm(form);
    emit(
      SignUpState(form: form, status: form.validate()),
    );
  }

  @override
  FutureOr<void> update(
    WyattForm form, {
    SetOperation operation = SetOperation.replace,
  }) {
    final WyattForm current = formRepository.accessForm(formName).clone();
    final WyattForm newForm = operation.operation.call(current, form);
    formRepository.updateForm(newForm);

    emit(
      SignUpState(
        form: newForm,
        status: newForm.validate(),
      ),
    );
  }

  @override
  FutureOr<void> validate() {
    final WyattForm form = formRepository.accessForm(formName);
    emit(
      SignUpState(form: form, status: form.validate()),
    );
  }

  @override
  FutureOr<void> submit() async {
    final WyattForm form = formRepository.accessForm(formName);
    const error = '`submit()` is not implemented for BaseSignUpCubit, '
        'please use `signUpWithEmailAndPassword()`.';
    emit(
      SignUpState(
        form: form,
        errorMessage: error,
        status: FormStatus.submissionFailure,
      ),
    );
    throw UnimplementedError(error);
  }
}
