// ignore_for_file: use_build_context_synchronously

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:hoshan/core/routes/route_generator.dart';
import 'package:hoshan/data/model/home_args.dart';
import 'package:hoshan/ui/screens/auth/code/bloc/check_code_bloc.dart';
import 'package:hoshan/ui/screens/splash/cubit/user_info_cubit.dart';
import 'package:hoshan/ui/theme/colors.dart';
import 'package:hoshan/ui/theme/text.dart';
import 'package:hoshan/ui/widgets/components/button/loading_button.dart';
import 'package:hoshan/ui/widgets/components/dropdown/hint_tooltip.dart';
import 'package:hoshan/ui/widgets/components/text/auth_text_field.dart';

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

  @override
  State<GiftCodeScreen> createState() => _GiftCodeScreenState();
}

class _GiftCodeScreenState extends State<GiftCodeScreen> {
  final TextEditingController textEditingController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return BlocConsumer<CheckCodeBloc, CheckCodeState>(
      listener: (context, state) async {
        if (state is CheckCodeSucceess) {
          await context.read<UserInfoCubit>().getUserInfo();
          context.go(Routes.home,
              extra: HomeArgs(message: state.message, freeCredit: 50));
        }
      },
      builder: (context, state) {
        return Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const HintTooltip(
                    hint:
                        'کد معرف هوشان یک کد یکتا است که در صورت به اشتراک‌گذاری این اپلیکیشن توسط دوستانتان با شما، از طریق پیامک برایتان ارسال می‌شود.'),
                const SizedBox(
                  width: 4,
                ),
                Text(
                  'آیا کد معرف دارید؟ اگر کد معرف دارید، وارد کنید.',
                  textDirection: TextDirection.rtl,
                  style: AppTextStyles.body5
                      .copyWith(color: Theme.of(context).colorScheme.onSurface),
                ),
              ],
            ),
            const SizedBox(
              height: 12,
            ),
            Padding(
              padding: const EdgeInsets.all(16.0),
              child: AuthTextField(
                controller: textEditingController,
                label: 'کد معرف',
                enabled: state is! CheckCodeLoading,
                error: state is CheckCodeFail
                    ? Text(
                        state.error,
                        style: AppTextStyles.body5
                            .copyWith(color: AppColors.red.defaultShade),
                      )
                    : null,
              ),
            ),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16.0),
              child: Column(
                children: [
                  LoadingButton(
                    width: MediaQuery.sizeOf(context).width,
                    height: 48,
                    radius: 360,
                    loading: state is CheckCodeLoading,
                    color: Theme.of(context).colorScheme.primary,
                    onPressed: () {
                      if (textEditingController.text.isEmpty ||
                          state is CheckCodeLoading) {
                        return;
                      }
                      context.read<CheckCodeBloc>().add(
                          VerifyGiftCode(code: textEditingController.text));
                    },
                    child: Text(
                      'تایید',
                      style: AppTextStyles.body4.copyWith(color: Colors.white),
                    ),
                  ),
                  const SizedBox(
                    height: 12,
                  ),
                  LoadingButton(
                    width: MediaQuery.sizeOf(context).width,
                    height: 48,
                    radius: 360,
                    isOutlined: true,
                    color: Theme.of(context).colorScheme.primary,
                    onPressed: () async {
                      if (state is CheckCodeLoading) return;
                      await context.read<UserInfoCubit>().getUserInfo();
                      context.go(Routes.home,
                          extra: HomeArgs(freeCredit: 50));
                    },
                    child: Text(
                      'ورود به اپلیکیشن',
                      style: AppTextStyles.body4.copyWith(
                          color: Theme.of(context).colorScheme.primary),
                    ),
                  ),
                ],
              ),
            )
          ],
        );
      },
    );
  }
}
