import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/global/global_state/global_state.dart';
import 'package:qadirneyriz/utils/enums/status.dart';
import 'package:qadirneyriz/utils/tools/tools.dart';
import 'package:qadirneyriz/widgets/custom_button.dart';
import 'package:qadirneyriz/widgets/custom_textfield.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class AddLocationDiolog extends StatelessWidget {
  AddLocationDiolog({
    super.key,
  });
  final TextEditingController farsiAddressController = TextEditingController();
  final TextEditingController englishAddressController =
      TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Consumer<GlobalState>(
      builder: (context, value, child) {
        return Dialog(
          child: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Column(
              mainAxisSize: MainAxisSize.min, // برای اندازه‌گیری درست دیالوگ
              children: [
                Text(
                  AppLocalizations.of(context)!.newlocation,
                ),
                CustomTextField(
                    label: AppLocalizations.of(context)!.farsi,
                    hintText: '',
                    textEditingController: farsiAddressController,
                    textInputType: TextInputType.text),
                CustomTextField(
                    label: AppLocalizations.of(context)!.english,
                    hintText: '',
                    textEditingController: englishAddressController,
                    textInputType: TextInputType.text),
                SizedBox(
                  height: 20,
                ),
                submit(value, context),
              ],
            ),
          ),
        );
      },
    );
  }

  CustomButton submit(GlobalState state, BuildContext context) {
    switch (state.statusAddNewAddress) {
      case Status.loading:
        return CustomButton(
          hieght: 40,
          width: double.infinity,
          text: AppLocalizations.of(context)!.loading,
          fontSize: 13,
          onPressed: null,
          borderRadius: 10,
        );

      default:
        return CustomButton(
          hieght: 40,
          width: double.infinity,
          text: AppLocalizations.of(context)!.add,
          fontSize: 13,
          onPressed: () async {
            if (farsiAddressController.text != '' &&
                englishAddressController.text != '') {
              // call add new subject
              final status = await state.addNewAddress(
                  address: farsiAddressController.text,
                  addressEn: englishAddressController.text);

              if (status == Status.ready) {
                // call refrresh subjects
                await state.getLocations(refresh: true);
                Tools.showCustomSnackBar(
                  text: AppLocalizations.of(context)!.addressadded,
                  isError: false,
                  context,
                );
                context.pop();
              } else {
                Tools.showCustomSnackBar(
                  text: state.errorsAddNewAddress == null
                      ? state.messageAddNewAddress ??
                          AppLocalizations.of(context)!.haserror
                      : Tools.combineErrorMessages(
                          state.errorsAddNewAddress ?? {}),
                  isError: true,
                  context,
                );
              }
            } else {
              Tools.showCustomSnackBar(
                text: AppLocalizations.of(context)!.erroraddress,
                isError: true,
                context,
              );
            }
          },
          borderRadius: 10,
        );
    }
  }
}
