import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:go_router/go_router.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:pharmacy_mobile/functions/checkexisting_function.dart';
import 'package:pharmacy_mobile/tables/ref_suppliers.dart';
import 'package:pharmacy_mobile/widgets/buttonwithprogress_widget.dart';
import 'package:pharmacy_mobile/widgets/form_border_widget2.dart';
import 'package:pharmacy_mobile/widgets/input_form_widget.dart';
import 'package:pharmacy_mobile/widgets/page_background_widget.dart';
import 'package:pharmacy_mobile/widgets/snackbar_widget.dart';
import 'package:pharmacy_mobile/widgets/text_widget.dart';
import 'package:pharmacy_mobile/widgets/title_widget.dart';

class AddSupplierPage extends StatefulWidget {
  const AddSupplierPage({super.key});

  @override
  State<AddSupplierPage> createState() => _AddSupplierPageState();
}

class _AddSupplierPageState extends State<AddSupplierPage> {
  final _formKey = GlobalKey<FormState>();
  final _nameController = TextEditingController();
  final _refsuppliers = RefSuppliers();

  late bool _isLoading = false;

  void _saveSupplier() async {
    setState(() => _isLoading = true);

    try {
      if (await InternetConnectionChecker.instance.hasConnection) {
        final existing = await checkExisting(_refsuppliers, _nameController);

        if (existing && mounted) {
          showNotification(context, 'Supplier already listed', false);
          return;
        }

        final post = await _refsuppliers.postSupplier(_nameController.text);
        print('post: $post');

        if (post && mounted) {
          showNotification(context, 'Supplier added to list', true);

          WidgetsBinding.instance.addPostFrameCallback((_) {
            if (mounted) {
              context.pop();
            }
          });
        }
      } else {
        if (mounted) {
          showNotification(context, 'Error: No Internet Connection', false);
        }
      }
    } catch (e) {
      if (mounted) {
        showNotification(context, 'Error: $e', false);
      }
    } finally {
      setState(() => _isLoading = false);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageBackgroundWidget(
          child: Center(
        child: Column(
          children: [
            const Gap(96),
            const TitleWidget(
              firstTextSize: 14,
              secondTextSize: 24,
              logoSize: 90,
            ),
            const Gap(32),
            const TextWidget(
              text: 'Add Supplier',
              title: true,
            ),
            const Gap(16),
            FormBorderWidget2(
                color: 'blue',
                child: Form(
                    key: _formKey,
                    child: Column(
                      children: [
                        InputFormWidget(label: 'Name', controller: _nameController),
                        const Gap(32),
                        ButtonWithProgressWidget(
                            trigger: _isLoading,
                            progressText: 'Adding Supplier',
                            buttonText: 'Save',
                            onPressed: _saveSupplier)
                      ],
                    )))
          ],
        ),
      )),
    );
  }
}
