import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:qadirneyriz/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 addressController = 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('آدرس جدید'),
                CustomTextField(
                    label: 'آدرس',
                    hintText: '',
                    textEditingController: addressController,
                    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,
          topRightRadius: 10,
          topLeftRadius: 10,
          bottomLeftRadius: 10,
          bottomRightRadius: 10,
        );

      default:
        return CustomButton(
          hieght: 40,
          width: double.infinity,
          text: 'اظافه کردن',
          fontSize: 13,
          onPressed: () async {
            if (addressController.text != '') {
              // call add new subject
              final status =
                  await state.addNewAddress(address: addressController.text);

              if (status == Status.ready) {
                // call refrresh subjects
                await state.getLocations(refresh: true);
                Tools.showCustomSnackBar(
                  text: 'آدرس اظافه شد!',
                  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: 'آدرس وارد کنید!',
                isError: true,
                context,
              );
            }
          },
          topRightRadius: 10,
          topLeftRadius: 10,
          bottomLeftRadius: 10,
          bottomRightRadius: 10,
        );
    }
  }
}
