import 'package:flutter/material.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked/stacked_annotations.dart';
import 'package:yimaru_app/ui/views/forget_password/forget_password_view.form.dart';
import 'package:yimaru_app/ui/views/forget_password/screens/request_reset_code_screen.dart';
import 'package:yimaru_app/ui/views/forget_password/screens/reset_password_screen.dart';

import '../../common/validators/form_validator.dart';
import 'forget_password_viewmodel.dart';

@FormView(fields: [
  FormTextField(name: 'password', validator: FormValidator.validateForm),
  FormTextField(name: 'resetCode', validator: FormValidator.validateForm),
  FormTextField(name: 'email', validator: FormValidator.validateEmailForm),
  FormTextField(name: 'confirmPassword', validator: FormValidator.validateForm)
])
class ForgetPasswordView extends StackedView<ForgetPasswordViewModel>
    with $ForgetPasswordView {
  const ForgetPasswordView({Key? key}) : super(key: key);

  void _initClearData() {
    emailController.clear();
    passwordController.clear();
    resetCodeController.clear();
    confirmPasswordController.clear();
  }

  void _clearDataOnNavigation(ForgetPasswordViewModel viewModel) {
    if (viewModel.currentPage == 0) {
      emailController.clear();
      viewModel.resetRequestResetCodeScreen();
    } else {
      passwordController.clear();
      resetCodeController.clear();
      confirmPasswordController.clear();
      viewModel.resetResetPasswordScreen();
    }
  }

  void _pop({required bool value, required ForgetPasswordViewModel viewModel}) {
    {
      // if (!value) return;
      _clearDataOnNavigation(viewModel);
      WidgetsBinding.instance.addPostFrameCallback((_) => viewModel.goBack());
    }
  }

  @override
  void onViewModelReady(ForgetPasswordViewModel viewModel) {
    _initClearData();
    syncFormWithViewModel(viewModel);
    super.onViewModelReady(viewModel);
  }

  @override
  ForgetPasswordViewModel viewModelBuilder(BuildContext context) =>
      ForgetPasswordViewModel();

  @override
  Widget builder(
    BuildContext context,
    ForgetPasswordViewModel viewModel,
    Widget? child,
  ) =>
      _buildLoginScreensWrapper(viewModel);

  Widget _buildLoginScreensWrapper(ForgetPasswordViewModel viewModel) =>
      PopScope(
          canPop: viewModel.currentPage == 0 ? true : false,
          onPopInvokedWithResult: (value, data) =>
              _pop(value: value, viewModel: viewModel),
          child: _buildBody(viewModel));

  Widget _buildBody(ForgetPasswordViewModel viewModel) =>
      IndexedStack(index: viewModel.currentPage, children: _buildScreens());

  List<Widget> _buildScreens() => [
        _buildRequestCodeScreen(),
        _buildResetPasswordScreen(),
      ];

  Widget _buildRequestCodeScreen() =>
      RequestCodeScreen(emailController: emailController);

  Widget _buildResetPasswordScreen() => ResetPasswordScreen(
      passwordController: passwordController,
      resetCodeController: resetCodeController,
      confirmPasswordController: confirmPasswordController);
}
