import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../api/login_api_client.dart';
import '../../../controllers/localization/localization.dart';
import '../../../controllers/providers/user_provider.dart';
import '../../../models/pantry/calibration_tools.dart';
import '../../app_style/sizing.dart';
import '../buttons/app_button.dart';
import '../buttons/app_small_button.dart';
import '../date_and_time/date_picker.dart';
import '../titles/app_sub_title.dart';
import 'auto_complete_fields/auto_complete_devices_field.dart';

class PentryCalibrationToolForm extends StatefulWidget {
  final List<CalibrationTool>? models;
  final bool? enableValidate;
  const PentryCalibrationToolForm({
    Key? key,
    this.models,
    this.enableValidate,
  }) : super(key: key);

  @override
  State<PentryCalibrationToolForm> createState() => _PentryCalibrationToolFormState();
}

class _PentryCalibrationToolFormState extends State<PentryCalibrationToolForm> {
  @override
  Widget build(BuildContext context) {
    final subtitle = AppLocalization.of(context)?.subtitle;
    final userProvider = Provider.of<UserProvider>(context);
    return ListView.builder(
        padding: EdgeInsets.only(
            top: 12 * AppStyle.getScaleFactor(context), left: 12 * AppStyle.getScaleFactor(context), right: 12 * AppStyle.getScaleFactor(context), bottom: 80 * AppStyle.getScaleFactor(context)),
        itemCount: widget.models!.length + 1,
        itemBuilder: (context, index) {
          if (index == widget.models?.length) {
            return AButton(
              text: subtitle?.add ?? "",
              onPressed: () {
                widget.models?.add(CalibrationTool());
                setState(() {});
              },
            );
          }
          final model = widget.models![index];
          return ListView(
            shrinkWrap: true,
            physics: const ClampingScrollPhysics(),
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  ASubTitle("#${index + 1}"),
                  if (index != 0)
                    ASmallButton(
                      color: Theme.of(context).colorScheme.error,
                      text: subtitle?.delete ?? "",
                      onPressed: () {
                        widget.models?.remove(model);
                        setState(() {});
                      },
                    ),
                ],
              ),
              const SizedBox(
                height: 8,
              ),
              const ASubTitle("Asset Number"),
              const SizedBox(
                height: 4,
              ),
              AutoCompleteDeviceNumberField(
                initialValue: model.assetsNumber,
                hospitalId: UserApiClient().user?.hospital?.id,
                onPick: (number) {
                  model.assetsNumber = number;
                },
              ),
              const SizedBox(
                height: 8,
              ),
              const ASubTitle("Date of Testing"),
              const SizedBox(
                height: 4,
              ),
              ADatePicker(
                date: model.dataOfTesting,
                onDatePicker: (date) {
                  model.dataOfTesting = date;
                  setState(() {});
                },
              ),
              const SizedBox(
                height: 8,
              ),
              Divider(
                color: Theme.of(context).textTheme.titleMedium?.color,
              ),
            ],
          );
        });
  }
}
