import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gap/gap.dart';

import '../../../../Globalconst.dart';
import '../../../../core/routes/routes.dart';
import '../../../../core/styles/app_color.dart';
import '../../../../core/styles/app_text.dart';
import '../../../../core/utils/language/localizations_delegate.dart';
import '../../../../core/utils/secure/secure_storage_service.dart';
import '../../../../shared/components/bloc/language/lng_bloc.dart';
import '../../../../shared/components/bloc/language/lng_event.dart';
import '../../../../shared/components/button_widget.dart';
import '../../../../shared/components/text_widget.dart';
import '../bloc/choose_language_bloc.dart';

Widget bottomSection(BuildContext context) {
  var localizations = AppLocalizations.of(context);
  final radioBloc = context.read<ChooseLanguageBloc>();
  SecureStorageService secureStorageService = SecureStorageService();
  return Column(
    mainAxisSize: MainAxisSize.min,
    children: [
      Container(
        margin: const EdgeInsets.symmetric(
          horizontal: 16,
          vertical: 10,
        ),
        width: 1.sw,
        height: 56.h,
        child: ButtonWidget().elevatedBtn(
          txtClr: AppColor.plainWhite,
          function: () async {
            radioBloc
                .langaugeChangeApiCall(
                    radioBloc.selectedCountry == 1 ? "AR" : "EN")
                .then(
              (value) async {
                if (value == "success") {
                  var newLocale = radioBloc.selectedCountry == 1
                      ? const Locale('ar')
                      : const Locale('en');
                  context
                      .read<LocalizationBloc>()
                      .add(ChangeLanguage(newLocale));
                  if (radioBloc.selectedCountry == 1) {
                    await secureStorageService.write('languageSelected', "ar");
                    Globalconst.languageSelected = "ar";
                  } else {
                    await secureStorageService.write('languageSelected', "en");
                    Globalconst.languageSelected = "en";
                  }
                  goRouter.pop();
                } else {
                  log(value.toString());
                  //goRouter.pop();
                }
              },
            );
          },
          text: localizations.translate(AppText.submitText),
          clr: AppColor.primaryColor2,
        ),
      ),
      ButtonWidget().textBtn(
          function: () {
            goRouter.pop();
          },
          text: TextWidget().text14W700(
              localizations.translate(AppText.backText),
              clr: AppColor.textLabelColor,
              textDecoration: TextDecoration.underline)),
      const Gap(20),
    ],
  );
}
