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 AddSubjectDiolog extends StatelessWidget {
  AddSubjectDiolog({
    super.key,
  });
  final TextEditingController subjectController = 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: subjectController,
                    textInputType: TextInputType.text),
                SizedBox(
                  height: 20,
                ),
                submit(value, context),
              ],
            ),
          ),
        );
      },
    );
  }

  CustomButton submit(GlobalState state, BuildContext context) {
    switch (state.statusAddNewSubject) {
      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 (subjectController.text != '') {
              // call add new subject
              final status =
                  await state.addNewSubject(subject: subjectController.text);

              if (status == Status.ready) {
                // call refrresh subjects
                await state.getSubjects(refresh: true);
                Tools.showCustomSnackBar(
                  text: 'موضوع اظافه شد!',
                  isError: false,
                  context,
                );
                context.pop();
              } else {
                Tools.showCustomSnackBar(
                  text: state.errorsAddNewSubject == null
                      ? state.messageAddNewSubject ??
                          AppLocalizations.of(context)!.haserror
                      : Tools.combineErrorMessages(
                          state.errorsAddNewSubject ?? {}),
                  isError: true,
                  context,
                );
              }
            } else {
              Tools.showCustomSnackBar(
                text: 'موضوع وارد کنید!',
                isError: true,
                context,
              );
            }
          },
          topRightRadius: 10,
          topLeftRadius: 10,
          bottomLeftRadius: 10,
          bottomRightRadius: 10,
        );
    }
  }
}
