import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:formz/formz.dart';
import 'package:go_router/go_router.dart';
import '../../../../core/theme/design_tokens.dart';
import '../../../../core/di/injection.dart';
import '../../../../shared/widgets/app_button.dart';
import '../../../../shared/widgets/toast/toast.dart';
import '../../../../shared/widgets/toast/toast_type.dart';
import '../../presentation/cubits/reset_password_cubit.dart';
import '../../data/auth_repository.dart';

class ResetPasswordScreen extends StatelessWidget {
  const ResetPasswordScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (context) => ResetPasswordCubit(sl<AuthRepository>()),
      child: const ResetPasswordView(),
    );
  }
}

class ResetPasswordView extends StatefulWidget {
  const ResetPasswordView({super.key});

  @override
  State<ResetPasswordView> createState() => _ResetPasswordViewState();
}

class _ResetPasswordViewState extends State<ResetPasswordView> {
  final _emailController = TextEditingController();
  final _codeController = TextEditingController();
  final _passwordController = TextEditingController();
  final _confirmPasswordController = TextEditingController();
  bool _obscurePassword = true;
  bool _obscureConfirmPassword = true;

  @override
  void dispose() {
    _emailController.dispose();
    _codeController.dispose();
    _passwordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  Future<void> _handleSubmit() async {
    final cubit = context.read<ResetPasswordCubit>();
    cubit.emailChanged(_emailController.text);
    cubit.codeChanged(_codeController.text);
    cubit.newPasswordChanged(_passwordController.text);
    cubit.confirmPasswordChanged(_confirmPasswordController.text);

    await cubit.submit();
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<ResetPasswordCubit, ResetPasswordState>(
      listenWhen: (previous, current) =>
          previous.status != current.status ||
          previous.errorMessage != current.errorMessage ||
          previous.codeSent != current.codeSent,
      listener: (context, state) {
        if (state.status == FormzSubmissionStatus.success && state.isSuccess) {
          Toast.show(context, '密码重置成功，请使用新密码登录', type: ToastType.success);
          context.go('/');
        } else if (state.status == FormzSubmissionStatus.success &&
            state.codeSent &&
            state.errorMessage == 'CODE_SENT_SUCCESS') {
          Toast.show(context, '验证码已发送到您的邮箱', type: ToastType.success);
        } else if (state.status == FormzSubmissionStatus.failure &&
            state.errorMessage != null &&
            state.errorMessage != '' &&
            state.errorMessage != 'CODE_SENT_SUCCESS') {
          Toast.show(context, state.errorMessage!, type: ToastType.error);
        }
      },
      child: Scaffold(
        backgroundColor: AppColors.background,
        body: SafeArea(
          child: Padding(
            padding: const EdgeInsets.symmetric(horizontal: 24),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Expanded(
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        _buildTitle(),
                        const SizedBox(height: 32),
                        _buildFormContainer(),
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildTitle() {
    return const Text(
      '忘记密码',
      style: TextStyle(
        fontSize: 28,
        fontWeight: FontWeight.w700,
        color: AppColors.slate900,
      ),
    );
  }

  Widget _buildFormContainer() {
    return BlocBuilder<ResetPasswordCubit, ResetPasswordState>(
      builder: (context, state) {
        return SizedBox(
          width: 327,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.stretch,
            children: [
              _buildEmailInput(state.email.displayError != null),
              const SizedBox(height: 12),
              _buildCodeInput(state.code.displayError != null, state),
              const SizedBox(height: 12),
              _buildPasswordInput(state.newPassword.displayError != null),
              const SizedBox(height: 12),
              _buildConfirmPasswordInput(
                state.confirmPassword.displayError != null,
              ),
              const SizedBox(height: 24),
              _buildSubmitButton(state),
              const SizedBox(height: 16),
              _buildBackToLogin(),
            ],
          ),
        );
      },
    );
  }

  Widget _buildEmailInput(bool hasError) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '邮箱',
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w500,
            color: AppColors.slate600,
          ),
        ),
        const SizedBox(height: 6),
        TextField(
          controller: _emailController,
          keyboardType: TextInputType.emailAddress,
          onChanged: (value) {
            context.read<ResetPasswordCubit>().emailChanged(value);
          },
          decoration: InputDecoration(
            hintText: '请输入邮箱',
            errorText: hasError ? ' ' : null,
          ),
        ),
      ],
    );
  }

  Widget _buildCodeInput(bool hasError, ResetPasswordState state) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '验证码',
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w500,
            color: AppColors.slate600,
          ),
        ),
        const SizedBox(height: 6),
        Row(
          children: [
            Expanded(
              child: TextField(
                controller: _codeController,
                keyboardType: TextInputType.number,
                onChanged: (value) {
                  context.read<ResetPasswordCubit>().codeChanged(value);
                },
                decoration: InputDecoration(
                  hintText: '请输入 6 位验证码',
                  errorText: hasError ? ' ' : null,
                ),
              ),
            ),
            const SizedBox(width: 12),
            SizedBox(
              height: 40,
              child: TextButton(
                onPressed:
                    state.resendCountdown > 0 ||
                        state.status == FormzSubmissionStatus.inProgress
                    ? null
                    : () {
                        if (state.codeSent) {
                          context.read<ResetPasswordCubit>().resendCode();
                        } else {
                          context.read<ResetPasswordCubit>().sendCode();
                        }
                      },
                style: TextButton.styleFrom(
                  backgroundColor: state.codeSent
                      ? AppColors.background
                      : AppColors.primary,
                  foregroundColor: state.codeSent
                      ? AppColors.primary
                      : AppColors.primaryForeground,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(AppRadius.sm),
                  ),
                  padding: const EdgeInsets.symmetric(horizontal: 14),
                ),
                child: Text(
                  state.resendCountdown > 0
                      ? '${state.resendCountdown}秒'
                      : (state.codeSent ? '重新发送' : '发送验证码'),
                  style: const TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildPasswordInput(bool hasError) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '新密码',
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w500,
            color: AppColors.slate600,
          ),
        ),
        const SizedBox(height: 6),
        TextField(
          controller: _passwordController,
          obscureText: _obscurePassword,
          onChanged: (value) {
            context.read<ResetPasswordCubit>().newPasswordChanged(value);
          },
          decoration: InputDecoration(
            hintText: '请输入新密码（至少 6 位）',
            errorText: hasError ? ' ' : null,
            suffixIcon: IconButton(
              icon: Icon(
                _obscurePassword ? Icons.visibility_off : Icons.visibility,
                size: 20,
                color: AppColors.slate400,
              ),
              onPressed: () {
                setState(() {
                  _obscurePassword = !_obscurePassword;
                });
              },
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildConfirmPasswordInput(bool hasError) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Text(
          '确认密码',
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w500,
            color: AppColors.slate600,
          ),
        ),
        const SizedBox(height: 6),
        TextField(
          controller: _confirmPasswordController,
          obscureText: _obscureConfirmPassword,
          onChanged: (value) {
            context.read<ResetPasswordCubit>().confirmPasswordChanged(value);
          },
          decoration: InputDecoration(
            hintText: '请再次输入新密码',
            errorText: hasError ? ' ' : null,
            suffixIcon: IconButton(
              icon: Icon(
                _obscureConfirmPassword
                    ? Icons.visibility_off
                    : Icons.visibility,
                size: 20,
                color: AppColors.slate400,
              ),
              onPressed: () {
                setState(() {
                  _obscureConfirmPassword = !_obscureConfirmPassword;
                });
              },
            ),
          ),
        ),
      ],
    );
  }

  Widget _buildSubmitButton(ResetPasswordState state) {
    final isLoading = state.status == FormzSubmissionStatus.inProgress;
    final isDisabled = isLoading || !state.codeSent;

    return AppButton(
      text: '重置密码',
      onPressed: isDisabled ? null : _handleSubmit,
    );
  }

  Widget _buildBackToLogin() {
    return GestureDetector(
      onTap: () => context.go('/'),
      child: const Text(
        '返回登录',
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: AppColors.slate500,
        ),
        textAlign: TextAlign.center,
      ),
    );
  }
}
