import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import '../../core/app_colors.dart';
import '../components/code_dots.dart';
import '../components/custom_app_bar.dart';
import '../components/gradient_button.dart';
import '../event/scooter_code_event.dart';
import '../state/scooter_code_state.dart';
import '../viewmodel/pin_bloc.dart';
import '../event/pin_event.dart';
import '../state/pin_state.dart';
import '../viewmodel/scooter_code_bloc.dart';

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

  @override
  State<ScooterCodeInputScreen> createState() => _ScooterCodeInputScreenState();
}

class _ScooterCodeInputScreenState extends State<ScooterCodeInputScreen> {
  final TextEditingController controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    controller.addListener(_onTextChanged);
  }

  void _onTextChanged() {
    context.read<ScooterCodeBloc>().add(ScooterCodeChanged(controller.text));
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<ScooterCodeBloc, ScooterCodeState>(
      listener: (context, state) {
        if (state is ScooterCodeSuccess) {
          context.go("/home/scooter/${state.scooter.id}");
        }
        if (state is ScooterCodeFailure) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text(state.error ?? "Ошибка")),
          );
        }
      },
      child: Scaffold(
          resizeToAvoidBottomInset: false,
          body: Container(
              decoration: const BoxDecoration(gradient: AppColors.phoneScreenBg),
              child: SafeArea(
          child: Column(
          children: [
            const SizedBox(height: 16),
              Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
          child: CustomAppBar(title: "Ввод QR-кода"),
      ),
      Expanded(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 24),
          child: BlocBuilder<ScooterCodeBloc, ScooterCodeState>(
            builder: (context, state) {
              final bool isCodeValid = state.code.length >= 5 && state.code.length <= 7;

              return Column(
                children: [
                  const SizedBox(height: 60),
                  const Text(
                    "Введите номер, расположенный\nпод QR-кодом",
                    textAlign: TextAlign.center,
                    style: TextStyle(
                      color: AppColors.whiteText,
                      fontSize: 16,
                      height: 1.4,
                    ),
                  ),

                  const SizedBox(height: 80),

                  TextField(
                    controller: controller,
                    keyboardType: TextInputType.text,
                    maxLength: 7,
                    textAlign: TextAlign.left,
                    style: const TextStyle(color: Colors.white, fontSize: 18),
                    decoration: InputDecoration(
                      hintText: "Ввести номер",
                      hintStyle: const TextStyle(color: AppColors.hint),
                      counterText: "",
                      filled: true,
                      fillColor: Colors.white.withOpacity(0.1),
                      border: OutlineInputBorder(
                        borderRadius: BorderRadius.circular(24),
                        borderSide: BorderSide.none,
                      ),
                    ),
                  ),

                  if (state is ScooterCodeLoading)
                    const Padding(
                      padding: EdgeInsets.only(top: 20),
                      child: CircularProgressIndicator(color: Colors.white),
                    ),

                  const Spacer(),

                  GradientButton(
                    text: "Подтвердить",
                    enabled: isCodeValid && state is! ScooterCodeLoading,
                    onTap: () {
                      context.read<ScooterCodeBloc>().add(
                          ScooterCodeSubmitted(state.code),
                      );
                    },
                    showArrows: true,
                    height: 56,
                    width: double.infinity,
                  ),
                  const SizedBox(height: 40),
                ],
              );
            },
          ),
        ),
      ),
      ],
    ),
    ),
    ),
    ),
    );
  }
}