// Flutter imports:
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

// Project imports:
import 'package:remever/common/resources.dart';
import 'package:remever/common/widgets/loose_focus.dart';
import 'package:remever/common/widgets/typography.dart';
import 'package:remever/common/widgets/wspace.dart';
import 'package:remever/components/extensions/context.dart';
import 'package:remever/gen/assets.gen.dart';
import 'package:remever/screens/auth/cubit/auth_cubit.dart';
import 'package:remever/screens/auth/widgets/pin_code.dart';
import 'package:remever/screens/auth/widgets/timer.dart';
import 'package:remever/widgets/primary_button.dart';

///
/// Отрисовка экрана ввода кода подтверждения
///
class CodeAuth extends StatefulWidget {
  const CodeAuth({super.key, required this.email, required this.uuid});
  final String email;
  final String uuid;

  @override
  CodeAuthState createState() => CodeAuthState();
}

class CodeAuthState extends State<CodeAuth> {
  final TextEditingController _pinController = TextEditingController();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  // Константы для стилей и отступов
  static final EdgeInsetsGeometry _padding = EdgeInsets.all(16).r;
  static final _regular14 = Regular14px();
  static final _medium14 = Medium14px();

  void _onEnterTap() async {
    if (!_formKey.currentState!.validate()) return;
    await context.read<AuthCubit>().sendCode(_pinController.text, widget.uuid);
  }

  @override
  Widget build(BuildContext context) {
    return LooseFocus(
      child: Scaffold(
        backgroundColor: AppColors.bg,
        body: Padding(
          padding: _padding,
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                HSpace(148),
                _buildLogo(),
                const HSpace(60),
                _buildEmailField(),
                const HSpace(24),
                _buildInstructions(),
                HSpace(16),
                _buildPinCode(),
                HSpace(12),
                _buildResendTimer(),
                HSpace(50),
                _buildLoginButton(),
                const HSpace(104),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildLogo() {
    return Assets.images.logo.image(width: 308.w, height: 100.h);
  }

  Widget _buildEmailField() {
    return Container(
      height: 48.h,
      decoration: BoxDecoration(
        color: AppColors.white,
        borderRadius: BorderRadius.all(Radius.circular(8)).r,
      ),
      child: Padding(
        padding: const EdgeInsets.all(12).r,
        child: Row(
          children: <Widget>[
            AppTypography(widget.email, type: _medium14),
            Spacer(),
            _buildCloseButton(),
          ],
        ),
      ),
    );
  }

  Widget _buildCloseButton() {
    return InkWell(
      onTap: () {
        // Добавьте логику для смены e-mail или закрытия экрана
      },
      child: SizedBox.square(
        dimension: 24.r,
        child: DecoratedBox(
          decoration: BoxDecoration(
            color: AppColors.bg,
            shape: BoxShape.circle,
          ),
          child: Icon(Icons.close, color: AppColors.disabled, size: 16.r),
        ),
      ),
    );
  }

  Widget _buildInstructions() {
    return AppTypography(
      'Ниже введите код, который получили на e-mail',
      type: _regular14,
    );
  }

  Widget _buildPinCode() {
    return Form(key: _formKey, child: PinCode(pinController: _pinController));
  }

  Widget _buildResendTimer() {
    return ResendTimer(onTap: () async {});
  }

  Widget _buildLoginButton() {
    return PrimaryButton(
      onTap: _onEnterTap,
      child: AppTypography('Войти', type: _medium14, color: AppColors.white),
    );
  }
}
