import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tanami_app/core/styles/app_color.dart';
import 'package:tanami_app/core/styles/app_text.dart';
import 'package:tanami_app/core/utils/secure/secure_storage_service.dart';
import 'package:tanami_app/shared/components/text_widget.dart';
import 'package:tanami_app/shared/components/toast_message.dart';

import '../../../../Globalconst.dart';
import '../../../../core/routes/routes.dart';
import '../../../../core/utils/language/localizations_delegate.dart';
import '../../../../shared/components/loader.dart';
import '../../../register/presentation/bloc/register_bloc.dart';
import '../../../register/presentation/bloc/register_event.dart';
import '../../../register/presentation/bloc/register_state.dart';
import '../bloc/timer/timer_bloc.dart';
import '../bloc/timer/timer_event.dart';
import '../bloc/timer/timer_state.dart';

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

  @override
  Widget build(BuildContext context) {
    String token = "";
    getToken() async {
      final SharedPreferenceLocalData secureStorageService =
          SharedPreferenceLocalData();
      token = await secureStorageService.read('temp_token') ?? "";
    }

    getToken();
    var loginBloc = context.read<RegisterBloc>();
    loginBloc.isdcode = Globalconst.isdcode;
    loginBloc.phoneNumberTextField.text = Globalconst.phonenumber;
    loginBloc.countrySelectionTextField.text = Globalconst.name;
    var localizations = AppLocalizations.of(context);
    return BlocBuilder<TimerBloc, TimerState>(
      builder: (context, timerState) {
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TextWidget().text14W500(
                timerState.formattedDuration,
                clr: AppColor.plainBlack,
              ),
              BlocConsumer<RegisterBloc, RegisterState>(
                listener: (context, state) {
                  if (state is RegisterLoading) {
                    Loader.loader(context);
                  } else if (state is RegisterSuccess) {
                    context.read<TimerBloc>().add(StartTimer()); // Reset timer
                    successToastMessage(context, "OTP Resend Sucessfully !");
                    goRouter.pop();
                  } else if (state is RegisterFailure) {
                    goRouter.pop();
                    errorToastMessage(
                      context,
                      state.error,
                    );
                  }
                },
                builder: (context, state) {
                  bool isButtonEnabled = false;

                  if (state is RegisterFieldsState) {
                    isButtonEnabled = state.areFieldsFilled;
                  } else if (state is RegisterSuccess ||
                      state is RegisterFailure) {
                    isButtonEnabled = true;
                  }
                  return GestureDetector(
                    onTap: () {
                      if (timerState is TimerRunComplete) {
                        loginBloc.add(
                          Resendotp(token),
                        );
                      } else {
                        // errorToastMessage(context,
                        //     "Please wait ${timerState.formattedDuration} Seconds before resending OTP");
                      }
                    },
                    child: TextWidget().text14W500(
                      localizations.translate(AppText.resendSms),
                      clr: timerState is TimerRunComplete
                          ? AppColor.plainBlack
                          : AppColor.indicatorInactiveColor,
                      textDecoration: TextDecoration.underline,
                    ),
                  );
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
